2 * Copyright Droids Corporation (2009)
3 * Olivier Matz <zer0@droids-corp.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Revision : $Id: main.c,v 1.8 2009-05-02 10:08:09 zer0 Exp $
26 #include <aversive/wait.h>
32 * Photodiodes: PC0 PC1
37 * hfuse: RSTDISBL=1 WTDON=1 SPIEN=0 CKOPT=0 EESAVE=1 BOOTSZ1=0 BOOTSZ0=0 BOOTRST=1
38 * lfuse: BODLEVEL=1 BODEN=1 SUT1=1 SUT0=1 CKSEL3=1 CKSEL2=1 CKSEL1=1 CKSEL0=1
41 //#define NO_MODULATION
43 //#define MODUL_455KHZ
49 /* beacon identifier: must be odd, 3 bits */
51 #define BEACON_ID_MASK 0x7
52 #define BEACON_ID_SHIFT 0
55 #define FRAME_DATA_MASK 0x0FF8
56 #define FRAME_DATA_SHIFT 3
58 #if (defined MODUL_455KHZ)
62 #elif (defined MODUL_56KHZ)
64 #define N_CYCLES_0 143
65 #define N_CYCLES_1 143
66 #elif (defined MODUL_38KHZ)
68 #define N_CYCLES_0 210
69 #define N_CYCLES_1 210
71 #error "no freq defined"
75 #define MAX_DIST 3600.
76 #define LASER_DIST 25.
78 #define N_CYCLES_PERIOD (N_CYCLES_0 + N_CYCLES_1)
80 #define LED_PORT PORTD
86 #define LED_TOGGLE(port, bit) do { \
87 if (port & _BV(bit)) \
93 #define LED1_ON() sbi(LED_PORT, LED1_BIT)
94 #define LED1_OFF() cbi(LED_PORT, LED1_BIT)
95 #define LED1_TOGGLE() LED_TOGGLE(LED_PORT, LED1_BIT)
96 #define LED2_ON() sbi(LED_PORT, LED2_BIT)
97 #define LED2_OFF() cbi(LED_PORT, LED2_BIT)
98 #define LED2_TOGGLE() LED_TOGGLE(LED_PORT, LED2_BIT)
99 #define LED3_ON() sbi(LED_PORT, LED3_BIT)
100 #define LED3_OFF() cbi(LED_PORT, LED3_BIT)
101 #define LED3_TOGGLE() LED_TOGGLE(LED_PORT, LED3_BIT)
103 #define IR_PORT PORTB
107 /* FRAME must be odd */
108 /* #define FRAME 0x0B /\* in little endian 1-1-0-1 *\/ */
109 /* #define FRAME_LEN 4 */
110 #define FRAME 0xAA5B /* in little endian */
113 /* pin returns !0 when nothing, and 0 when laser is on photodiode */
114 #define PHOTO_PIN PINB
116 #define READ_PHOTO() (!!(PHOTO_PIN & (_BV(PHOTO_BIT))))
118 /* IR_DELAY **must** be < 32768 */
119 #if (defined SPEED_10RPS)
120 #define MIN_INTER_TIME ((uint16_t)(160*2)) /* t~=160us dist=350cm */
121 #define MAX_INTER_TIME ((uint16_t)(8000*2)) /* t=8ms dist=10cm */
122 #define IR_DELAY ((uint16_t)(8000*2))
123 #define INTER_LASER_TIME 30 /* in ms */
124 #elif (defined SPEED_40RPS)
125 #define MIN_INTER_TIME ((uint16_t)(40*16)) /* t~=40us dist=350cm */
126 #define MAX_INTER_TIME ((uint16_t)(2000*16)) /* t=2ms dist=10cm */
127 #define IR_DELAY ((uint16_t)(2000*16))
128 #define INTER_LASER_TIME 10 /* in ms */
130 #error "speed not defined"
133 /* basic functions to transmit on IR */
135 static inline void xmit_0(void)
137 uint16_t t = ((N_CYCLES_PERIOD * N_PERIODS) / 4);
139 cbi(IR_PORT, IR_BIT);
144 _delay_loop_2(t); /* 4 cycles per loop */
147 static inline void xmit_1(void)
149 uint16_t t = ((N_CYCLES_PERIOD * N_PERIODS) / 4);
151 sbi(IR_PORT, IR_BIT);
153 TCCR1B = _BV(WGM13) | _BV(WGM12);
154 TCNT1 = N_CYCLES_PERIOD-1;
155 TCCR1A = _BV(COM1A1) | _BV(WGM11);
156 ICR1 = N_CYCLES_PERIOD;
157 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
159 _delay_loop_2(t); /* 4 cycles per loop */
162 /* transmit in manchester code */
164 static inline void xmit_manchester_0(void)
170 static inline void xmit_manchester_1(void)
176 /* transmit a full frame. Each byte is lsb first. */
178 static void xmit_bits(uint32_t frame, uint8_t nbit)
182 for (i=0; i<nbit; i++) {
195 /* val is 12 bits. Return the 16 bits value that includes the 4 bits
197 static uint16_t do_cksum(uint16_t val)
202 /* add the three 4-bits blocks of x together */
206 cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
209 cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
210 cksum = (~cksum) & 0xf;
211 return (cksum << 12) + (val & 0xfff);
214 /* get the frame from laser time difference, return 0 on error (this
215 * frame cannot be sent). */
216 static uint32_t get_frame(uint16_t laserdiff)
222 frame |= ((uint32_t)BEACON_ID << BEACON_ID_SHIFT);
224 /* process angle from laserdiff time */
227 a = ((double)laserdiff / (2000000./10.)) * 2. * M_PI;
230 a = ((double)laserdiff / (16000000./40.)) * 2. * M_PI;
232 /* get distance from angle */
233 d = LASER_DIST / sin(a/2);
235 /* scale it between 0 and 511 */
241 d /= (MAX_DIST-MIN_DIST);
243 frame_dist = (uint16_t)d;
244 if (frame_dist >= 512) /* should not happen... */
247 frame |= ((uint32_t)(frame_dist & FRAME_DATA_MASK) << FRAME_DATA_SHIFT);
249 /* process cksum and return */
250 return do_cksum(frame);
253 /* Wait 2 consecutive rising edges on photodiode. Return 0 on success,
254 * in this case, the 'when' pointed area is assigned to the time when
255 * IR signal should be sent. The 'laserdiff' pointer is the time
256 * between the 2 lasers, in timer unit. */
257 static inline int8_t wait_laser(uint16_t *when, uint16_t *laserdiff)
259 uint16_t time1, time2;
263 /* set timer to 16Mhz, we will use ICP */
267 /* set timer to 2Mhz, we will use ICP */
272 /* wait until all is off (inverted logic) */
273 while (READ_PHOTO() == 0);
276 /* wait falling edge */
277 while ((TIFR & _BV(ICF1)) == 0);
282 /* wait a bit to avoid oscillations */
283 while ((uint16_t)(TCNT1-time1) < MIN_INTER_TIME);
286 /* wait next falling edge + timeout */
287 while ((TIFR & _BV(ICF1)) == 0) {
288 diff = TCNT1 - time1;
289 if (diff > MAX_INTER_TIME)
295 /* get time of 2nd laser */
299 /* process time difference */
300 diff = time2 - time1;
301 if (diff < MIN_INTER_TIME)
304 *when = time1 + (diff/2) + IR_DELAY;
317 uint32_t frame = FRAME;
324 LED_DDR = _BV(LED1_BIT) | _BV(LED2_BIT) | _BV(LED3_BIT);
325 IR_DDR |= _BV(IR_BIT);
328 fdevopen(uart0_dev_send, uart0_dev_recv);
334 c = uart_recv_nowait(0);
336 printf("%c", (char)(c+1));
353 #ifndef NO_MODULATION
355 ICR1 = N_CYCLES_PERIOD;
357 TCCR1A = _BV(COM1A1) | _BV(WGM11);
358 TCCR1B = _BV(WGM13) | _BV(WGM12);
363 ICR1 = N_CYCLES_PERIOD;
365 TCCR1B = _BV(WGM13) | _BV(WGM12);
366 TCNT1 = N_CYCLES_PERIOD-1;
367 TCCR1A = _BV(COM1A1) | _BV(WGM11);
368 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
373 /* configure timer 0, prescaler = 64 */
374 TCCR0 = _BV(CS01) | _BV(CS00);
379 ret = wait_laser(&when, &diff);
387 frame = get_frame(diff);
388 /* cannot convert into frame... skip it */
390 wait_ms(INTER_LASER_TIME);
394 /* wait before IR xmit */
395 while ((int16_t)(when-TCNT1) > 0);
399 /* ok, transmit frame */
400 xmit_bits(frame, FRAME_LEN);
404 /* don't watch a laser during this time */
405 wait_ms(INTER_LASER_TIME);