*
*/
+#include <math.h>
+
#include <aversive.h>
#include <aversive/wait.h>
+#include <aversive/pgmspace.h>
#include <uart.h>
#define BEACON_ID_SHIFT 0
/* */
-#define FRAME_DATA_MASK 0xFFF8
+#define FRAME_DATA_MASK 0x0FF8
#define FRAME_DATA_SHIFT 3
#if (defined MODUL_455KHZ)
#error "no freq defined"
#endif
+#define MIN_DIST 150.
+#define MAX_DIST 3600.
+#define LASER_DIST 25.
+
#define N_CYCLES_PERIOD (N_CYCLES_0 + N_CYCLES_1)
#define LED_PORT PORTD
#error "speed not defined"
#endif
+extern prog_uint16_t framedist_table[];
/* basic functions to transmit on IR */
static inline void xmit_0(void)
}
#ifdef WAIT_LASER
-/* get the frame from laser time difference */
+/* val is 12 bits. Return the 16 bits value that includes the 4 bits
+ * cksum in MSB. */
+static uint16_t do_cksum(uint16_t val)
+{
+ uint16_t x, cksum;
+
+ x = (val & 0xfff);
+ /* add the three 4-bits blocks of x together */
+ cksum = x & 0xf;
+ x = x >> 4;
+ cksum += x & 0xf;
+ cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
+ x = x >> 4;
+ cksum += x & 0xf;
+ cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
+ cksum = (~cksum) & 0xf;
+ return (cksum << 12) + (val & 0xfff);
+}
+
+/* get the frame from laser time difference, return 0 on error (this
+ * frame cannot be sent). */
static uint32_t get_frame(uint16_t laserdiff)
{
uint32_t frame = 0;
- frame |= ((uint32_t)BEACON_ID << BEACON_ID_SHIFT);
- frame |= ((uint32_t)(laserdiff & FRAME_DATA_MASK) << FRAME_DATA_SHIFT);
- return frame;
+ uint16_t frame_dist = 256;
+ uint16_t step, val;
+
+ /* for calibration, return the time */
+ if (0)
+ return laserdiff;
+
+ for (step = 128; step != 0; step /= 2) {
+ val = pgm_read_word(&framedist_table[frame_dist]);
+ if (laserdiff > val)
+ frame_dist -= step;
+ else
+ frame_dist += step;
+ }
+
+ frame |= ((uint32_t)(frame_dist & FRAME_DATA_MASK) << FRAME_DATA_SHIFT);
+
+ /* process cksum and return */
+ return do_cksum(frame);
}
/* Wait 2 consecutive rising edges on photodiode. Return 0 on success,
while (1);
#endif
+#if 1
+ /* test freq ~ 400khz */
+ ICR1 = 38;
+ OCR1A = 19;
+ TCCR1B = _BV(WGM13) | _BV(WGM12);
+ TCNT1 = 37;
+ TCCR1A = _BV(COM1A1) | _BV(WGM11);
+ TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
+
+ /* motor PWM 50%, 8khz */
+ DDRB |= 0x08;
+ OCR2 = 50;
+ TCCR2 = _BV(WGM21) | _BV(WGM20) | _BV(COM21) | _BV(CS21) ;
+
+ while (1);
+#endif
+
/* configure timer 0, prescaler = 64 */
TCCR0 = _BV(CS01) | _BV(CS00);
continue;
frame = get_frame(diff);
+ /* cannot convert into frame... skip it */
+ if (frame == 0) {
+ wait_ms(INTER_LASER_TIME);
+ continue;
+ }
/* wait before IR xmit */
while ((int16_t)(when-TCNT1) > 0);