-/*
+/*
* Copyright Droids Corporation (2009)
* Olivier Matz <zer0@droids-corp.org>
- *
+ *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
*
*/
+#include <math.h>
+
#include <aversive.h>
#include <aversive/wait.h>
+#include <aversive/pgmspace.h>
#include <uart.h>
/*
* hfuse: RSTDISBL=1 WTDON=1 SPIEN=0 CKOPT=0 EESAVE=1 BOOTSZ1=0 BOOTSZ0=0 BOOTRST=1
- * lfuse: BODLEVEL=1 BODEN=1 SUT1=1 SUT0=1 CKSEL3=1 CKSEL2=1 CKSEL1=1 CKSEL0=1
+ * lfuse: BODLEVEL=1 BODEN=1 SUT1=1 SUT0=1 CKSEL3=1 CKSEL2=1 CKSEL1=1 CKSEL0=1
*/
-#define N_PERIODS 10
+//#define NO_MODULATION
+#define WAIT_LASER
+//#define MODUL_455KHZ
+#define MODUL_56KHZ
+//#define MODUL_38KHZ
+//#define INC_FRAME
+//#define FIXED_FRAME
+//#define SEND_RAWTIME
+#define LOCK_BUS
+#define SPEED_20RPS
+
+/* beacon identifier: must be odd, 3 bits */
+#define BEACON_ID 0x1
+#define BEACON_ID_MASK 0x7
+#define BEACON_ID_SHIFT 0
+
+/* */
+#define FRAME_DATA_MASK 0x0FF8
+#define FRAME_DATA_SHIFT 3
+
+#if (defined MODUL_455KHZ)
+#define N_PERIODS 15
#define N_CYCLES_0 17
#define N_CYCLES_1 17
+#elif (defined MODUL_56KHZ)
+#define N_PERIODS 15
+#define N_CYCLES_0 143
+#define N_CYCLES_1 143
+#elif (defined MODUL_38KHZ)
+#define N_PERIODS 15
+#define N_CYCLES_0 210
+#define N_CYCLES_1 210
+#else
+#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
#define LED3_BIT 7
#define LED_TOGGLE(port, bit) do { \
- if (port & _BV(bit)) \
- port &= ~_BV(bit); \
- else \
- port |= _BV(bit); \
- } while(0)
+ if (port & _BV(bit)) \
+ port &= ~_BV(bit); \
+ else \
+ port |= _BV(bit); \
+ } while(0)
#define LED1_ON() sbi(LED_PORT, LED1_BIT)
#define LED1_OFF() cbi(LED_PORT, LED1_BIT)
#define IR_DDR DDRB
#define IR_BIT 1
+#define LOCKBUS_PORT PORTD
+#define LOCKBUS_PIN PIND
+#define LOCKBUS_DDR DDRD
+#define LOCKBUS_BIT 3
+
/* FRAME must be odd */
/* #define FRAME 0x0B /\* in little endian 1-1-0-1 *\/ */
/* #define FRAME_LEN 4 */
-#define FRAME 0xAA5B /* in little endian */
+#define FRAME 0x621 /* in little endian */
#define FRAME_LEN 16
-/* pin returns 1 when nothing, and 0 when laser is on photodiode */
-#define PHOTO_PIN PINC
-#define PHOTO1_BIT 0
-#define PHOTO2_BIT 1
-#define READ_PHOTOS() (PHOTO_PIN & (_BV(PHOTO1_BIT) | _BV(PHOTO2_BIT)))
-#define READ_PHOTO1() (PHOTO_PIN & _BV(PHOTO1_BIT))
-#define READ_PHOTO2() (PHOTO_PIN & _BV(PHOTO2_BIT))
+/* pin returns !0 when nothing, and 0 when laser is on photodiode */
+#define PHOTO_PIN PINB
+#define PHOTO_BIT 0
+#define READ_PHOTO() (!!(PHOTO_PIN & (_BV(PHOTO_BIT))))
-#define PHOTOS_ALL_OFF (_BV(PHOTO1_BIT) | _BV(PHOTO2_BIT))
-#define PHOTOS_ALL_ON (0)
-#define PHOTOS_1ON_2OFF (_BV(PHOTO2_BIT))
-#define PHOTOS_1OFF_2ON (_BV(PHOTO1_BIT))
-
-
-/* in cycles/64 (unit is 4 us at 16Mhz) */
-#define MAX_PHOTO_TIME ((uint8_t)25) /* t=100us len=5mm rps=40Hz dist=20cm */
-
-#define MIN_INTER_TIME ((uint8_t)12) /* t=50us len=50mm rps=40Hz dist=350cm */
-#define MAX_INTER_TIME ((uint8_t)250) /* t=1000us len=50mm rps=40Hz dist=20cm */
-
-/* in ms */
-#define INTER_LASER_TIME 10
-
-#define NO_MODULATION
-//#define WAIT_LASER
+/* IR_DELAY must be < 32768 */
+#define MIN_INTER_TIME ((uint16_t)(100*16U)) /* t~=160us dist=400cm */
+#define MAX_INTER_TIME ((uint16_t)(4000*16U)) /* t=4ms dist=16cm */
+#define IR_DELAY ((uint16_t)(MAX_INTER_TIME/2))
+#define INTER_LASER_TIME 10 /* in ms */
+extern prog_uint16_t framedist_table[];
/* basic functions to transmit on IR */
static inline void xmit_0(void)
{
- uint8_t t = ((N_CYCLES_PERIOD * N_PERIODS) / 3);
+ uint16_t t = ((N_CYCLES_PERIOD * N_PERIODS) / 4);
#ifdef NO_MODULATION
cbi(IR_PORT, IR_BIT);
#else
TCCR1B = 0;
TCCR1A = 0;
#endif
- _delay_loop_1(t); /* 3 cycles per loop */
+ _delay_loop_2(t); /* 4 cycles per loop */
}
static inline void xmit_1(void)
{
- uint8_t t = ((N_CYCLES_PERIOD * N_PERIODS) / 3);
+ uint16_t t = ((N_CYCLES_PERIOD * N_PERIODS) / 4);
#ifdef NO_MODULATION
sbi(IR_PORT, IR_BIT);
#else
TCCR1B = _BV(WGM13) | _BV(WGM12);
TCNT1 = N_CYCLES_PERIOD-1;
TCCR1A = _BV(COM1A1) | _BV(WGM11);
+ ICR1 = N_CYCLES_PERIOD;
TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
#endif
- _delay_loop_1(t); /* 3 cycles per loop */
+ _delay_loop_2(t); /* 4 cycles per loop */
}
/* transmit in manchester code */
xmit_0();
}
-/* */
-static inline int8_t wait_laser(void)
+/* 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)
{
- uint8_t photos;
- uint8_t time;
- uint8_t diff;
+ uint32_t frame = 0;
+ uint16_t val, mid, min, max;
+
+ /* for calibration, return the time */
+#ifdef INC_FRAME
+ static uint16_t fr = 1;
+ fr += 2;
+ return fr;
+#endif
+#ifdef SEND_RAWTIME
+ return laserdiff | 1; /* frame must be odd */
+#endif
+
+ min = 0;
+ max = 511;
+ while (min != max) {
+ mid = (max + min + 1) / 2;
+ val = pgm_read_word(&framedist_table[mid]);
- /* wait until all is off */
- while (READ_PHOTOS() != PHOTOS_ALL_OFF);
+ if (laserdiff > val)
+ max = mid - 1;
+ else
+ min = mid;
+ }
- /* wait an event, it must be photo1 on */
- while ((photos = READ_PHOTOS()) == PHOTOS_ALL_OFF);
+ frame |= ((uint32_t)((min << FRAME_DATA_SHIFT)
+ & FRAME_DATA_MASK));
+ frame |= BEACON_ID;
+
+ /* process cksum and return */
+ return do_cksum(frame);
+}
+
+/* Wait 2 consecutive rising edges on photodiode. Return 0 on success,
+ * in this case, the 'when' pointed area is assigned to the time when
+ * IR signal should be sent. The 'laserdiff' pointer is the time
+ * between the 2 lasers, in timer unit. */
+static inline int8_t wait_laser(uint16_t *when, uint16_t *laserdiff)
+{
+ uint16_t time1, time2;
+ uint16_t diff;
+
+ /* set timer to 16Mhz, we will use ICP */
+ TCCR1A = 0;
+ TCCR1B = _BV(CS10);
+
+ /* wait until all is off (inverted logic) */
+ while (READ_PHOTO() == 0);
+ TIFR = _BV(ICF1);
+
+ /* wait falling edge */
+ while ((TIFR & _BV(ICF1)) == 0);
+ time1 = ICR1;
- if (photos != PHOTOS_1ON_2OFF)
- return -1;
- time = TCNT0;
LED1_ON();
- /* wait an event, it must be photo1 off */
- while ((photos = READ_PHOTOS()) == PHOTOS_1ON_2OFF) {
- diff = TCNT0 - time;
- if (diff > MAX_PHOTO_TIME)
- return -1;
- }
- if (photos != PHOTOS_ALL_OFF)
- return -1;
- //time = TCNT0;
+ /* wait a bit to avoid oscillations */
+ while ((uint16_t)(TCNT1-time1) < MIN_INTER_TIME);
+ TIFR = _BV(ICF1);
- /* wait an event, it must be photo2 on */
- while ((photos = READ_PHOTOS()) == PHOTOS_ALL_OFF) {
- diff = TCNT0 - time;
+ /* wait next falling edge + timeout */
+ while ((TIFR & _BV(ICF1)) == 0) {
+ diff = TCNT1 - time1;
if (diff > MAX_INTER_TIME)
return -1;
}
+
LED2_ON();
-
- diff = TCNT0 - time;
- if (diff < MIN_INTER_TIME)
- return -1;
- if (photos != PHOTOS_1OFF_2ON)
- return -1;
-#if 0
- time = TCNT0;
+ /* get time of 2nd laser */
+ time2 = ICR1;
+ TIFR = _BV(ICF1);
- /* wait an event, it must be photo2 off */
- while ((photos = READ_PHOTOS()) == PHOTOS_1OFF_2ON) {
- diff = TCNT0 - time;
- if (diff > MAX_PHOTO_TIME)
- return -1;
- }
- if (photos != PHOTOS_ALL_OFF)
+ /* process time difference */
+ diff = time2 - time1;
+ if (diff > MAX_INTER_TIME)
return -1;
-#endif
+
+ /*
+ * diff/2 is < 32768
+ * ir_delay is < 32768
+ * current time is ~time1 + diff
+ */
+ *when = time1 + (diff/2) + IR_DELAY;
+ *laserdiff = diff;
/* laser ok */
return 0;
/* must be odd */
uint32_t frame = FRAME;
int8_t ret;
- int16_t c;
+ uint16_t when = 0;
+ uint16_t diff = 0;
/* LEDS */
LED_DDR = _BV(LED1_BIT) | _BV(LED2_BIT) | _BV(LED3_BIT);
#if 0
while (1) {
+ int16_t c;
c = uart_recv_nowait(0);
- if (c != -1)
+ if (c != -1)
printf("%c", (char)(c+1));
LED1_ON();
wait_ms(500);
#if 0
while (1) {
- if (READ_PHOTO() & _BV(PHOTO1_BIT))
+ if (READ_PHOTO())
LED1_ON();
else
LED1_OFF();
TCCR1B = _BV(WGM13) | _BV(WGM12);
#endif
+#if 0
+ /* test freq */
+ ICR1 = N_CYCLES_PERIOD;
+ OCR1A = N_CYCLES_1;
+ TCCR1B = _BV(WGM13) | _BV(WGM12);
+ TCNT1 = N_CYCLES_PERIOD-1;
+ TCCR1A = _BV(COM1A1) | _BV(WGM11);
+ TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
+ while (1);
+#endif
+
+#if 0
+ /* 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
+
+#ifdef LOCK_BUS
+ LOCKBUS_PORT &= (~_BV(LOCKBUS_BIT));
+#endif
+
/* configure timer 0, prescaler = 64 */
TCCR0 = _BV(CS01) | _BV(CS00);
while (1) {
#ifdef WAIT_LASER
- ret = wait_laser();
-
+ ret = wait_laser(&when, &diff);
+
LED1_OFF();
LED2_OFF();
if (ret)
continue;
+#endif /* WAIT_LASER */
+
+#ifdef LOCK_BUS
+ if ((LOCKBUS_PIN & _BV(LOCKBUS_BIT)) == 0)
+ continue;
+ LOCKBUS_DDR |= _BV(LOCKBUS_BIT);
#endif
-
-#if 1
+
+#ifdef FIXED_FRAME
+ frame = FRAME;
+#else
+ frame = get_frame(diff);
+#endif /* FIXED_FRAME */
+ /* cannot convert into frame... skip it */
+ if (frame == 0) {
+ wait_ms(INTER_LASER_TIME);
+ continue;
+ }
+
+
+#ifdef WAIT_LASER
+ /* wait before IR xmit */
+ while ((int16_t)(when-TCNT1) > 0);
+#endif /* WAIT_LASER */
+
LED3_ON();
/* ok, transmit frame */
xmit_bits(frame, FRAME_LEN);
-
- /* don't watch a laser during this time */
- wait_ms(INTER_LASER_TIME);
+
LED3_OFF();
-#else
- LED1_ON();
- wait_ms(1);
- LED1_OFF();
+
+#ifdef LOCK_BUS
+ LOCKBUS_DDR &= (~_BV(LOCKBUS_BIT));
#endif
+
+ /* don't watch a laser during this time */
+ wait_ms(INTER_LASER_TIME);
}
return 0;
}