|
|
@@ -7,12 +7,14 @@ |
|
|
|
#include "timer.h"
|
|
|
|
#include "uart.h"
|
|
|
|
|
|
|
|
#define DEBUG_CALIBRATION
|
|
|
|
|
|
|
|
float g_scale; /* delta(DAC) / delta(ADC) */
|
|
|
|
|
|
|
|
/* Initialize. Assume some relatively-safe scaling if no calibration is run */
|
|
|
|
void calibrate_init(void)
|
|
|
|
{
|
|
|
|
g_scale = 0.5 * (DAC_RANGE / 65536); /* 1 count at 16-bit DAC means 2 counts at ADC */
|
|
|
|
g_scale = (DAC_RANGE / 65536.0); /* 1 count at 16-bit DAC means 1 counts at ADC */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Given the current DAC and ADC values d1 and a1,
|
|
|
@@ -34,15 +36,15 @@ float calculate_scale(uint16_t d1, float a1, uint16_t d2, float a2) |
|
|
|
float a = a2 - a1;
|
|
|
|
|
|
|
|
/* Correct for known errors */
|
|
|
|
d1 = dac_get_actual(d1);
|
|
|
|
d2 = dac_get_actual(d2);
|
|
|
|
// d1 = dac_get_actual(d1);
|
|
|
|
// d2 = dac_get_actual(d2);
|
|
|
|
|
|
|
|
if (a < 0.1 && a > -0.1)
|
|
|
|
scale = 1.0;
|
|
|
|
else {
|
|
|
|
scale = ((float)d2 - d1) / a;
|
|
|
|
if (scale < 0.05)
|
|
|
|
scale = 0.05;
|
|
|
|
if (scale < 0.01)
|
|
|
|
scale = 0.01;
|
|
|
|
if (scale > 20)
|
|
|
|
scale = 20;
|
|
|
|
}
|
|
|
@@ -69,6 +71,7 @@ uint16_t seek(uint16_t starting_dac, int16_t desired_adc) |
|
|
|
{
|
|
|
|
/* give up if we're not making progress */
|
|
|
|
if (steps++ > SEEK_MAX_STEPS) {
|
|
|
|
// 2 flashes, delay, repeat
|
|
|
|
led_pattern(0b00101000);
|
|
|
|
break;
|
|
|
|
}
|
|
|
@@ -84,7 +87,7 @@ uint16_t seek(uint16_t starting_dac, int16_t desired_adc) |
|
|
|
msleep(1);
|
|
|
|
adc = adc_get();
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#ifdef DEBUG_CALIBRATION
|
|
|
|
uart1_put_hex16(dac);
|
|
|
|
uart1_put(' ');
|
|
|
|
uart1_put_hex16(adc);
|
|
|
@@ -141,7 +144,7 @@ uint16_t do_calibrate(void) |
|
|
|
/* Calculate scale */
|
|
|
|
g_scale = calculate_scale(x1, y1, x2, y2);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#ifdef DEBUG_CALIBRATION
|
|
|
|
uart1_put_string("calibrate x1=");
|
|
|
|
uart1_put_dec(x1);
|
|
|
|
uart1_put_string(" y1=");
|
|
|
|