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>
27 #include <aversive/pgmspace.h>
33 * Photodiodes: PC0 PC1
38 * hfuse: RSTDISBL=1 WTDON=1 SPIEN=0 CKOPT=0 EESAVE=1 BOOTSZ1=0 BOOTSZ0=0 BOOTRST=1
39 * lfuse: BODLEVEL=1 BODEN=1 SUT1=1 SUT0=1 CKSEL3=1 CKSEL2=1 CKSEL1=1 CKSEL0=1
42 //#define NO_MODULATION
51 /* beacon identifier: must be odd, 3 bits */
53 #define BEACON_ID_MASK 0x7
54 #define BEACON_ID_SHIFT 0
57 #define FRAME_DATA_MASK 0x0FF8
58 #define FRAME_DATA_SHIFT 3
60 #if (defined MODUL_455KHZ)
64 #elif (defined MODUL_56KHZ)
66 #define N_CYCLES_0 143
67 #define N_CYCLES_1 143
68 #elif (defined MODUL_38KHZ)
70 #define N_CYCLES_0 210
71 #define N_CYCLES_1 210
73 #error "no freq defined"
77 #define MAX_DIST 3600.
78 #define LASER_DIST 25.
80 #define N_CYCLES_PERIOD (N_CYCLES_0 + N_CYCLES_1)
82 #define LED_PORT PORTD
88 #define LED_TOGGLE(port, bit) do { \
89 if (port & _BV(bit)) \
95 #define LED1_ON() sbi(LED_PORT, LED1_BIT)
96 #define LED1_OFF() cbi(LED_PORT, LED1_BIT)
97 #define LED1_TOGGLE() LED_TOGGLE(LED_PORT, LED1_BIT)
98 #define LED2_ON() sbi(LED_PORT, LED2_BIT)
99 #define LED2_OFF() cbi(LED_PORT, LED2_BIT)
100 #define LED2_TOGGLE() LED_TOGGLE(LED_PORT, LED2_BIT)
101 #define LED3_ON() sbi(LED_PORT, LED3_BIT)
102 #define LED3_OFF() cbi(LED_PORT, LED3_BIT)
103 #define LED3_TOGGLE() LED_TOGGLE(LED_PORT, LED3_BIT)
105 #define IR_PORT PORTB
109 /* FRAME must be odd */
110 /* #define FRAME 0x0B /\* in little endian 1-1-0-1 *\/ */
111 /* #define FRAME_LEN 4 */
112 #define FRAME 0x4A5B /* in little endian */
115 /* pin returns !0 when nothing, and 0 when laser is on photodiode */
116 #define PHOTO_PIN PINB
118 #define READ_PHOTO() (!!(PHOTO_PIN & (_BV(PHOTO_BIT))))
120 /* IR_DELAY **must** be < 32768 */
121 #if (defined SPEED_10RPS)
122 #define MIN_INTER_TIME ((uint16_t)(160*2)) /* t~=160us dist=350cm */
123 #define MAX_INTER_TIME ((uint16_t)(8000*2)) /* t=8ms dist=10cm */
124 #define IR_DELAY ((uint16_t)(8000*2))
125 #define INTER_LASER_TIME 30 /* in ms */
126 #elif (defined SPEED_20RPS)
127 #define MIN_INTER_TIME ((uint16_t)(40*16)) /* t~=80us dist=350cm */
128 #define MAX_INTER_TIME ((uint16_t)(2000*16)) /* t=2ms dist=? >10cm */
129 #define IR_DELAY ((uint16_t)(2000*16))
130 #define INTER_LASER_TIME 10 /* in ms */
131 #elif (defined SPEED_40RPS)
132 #define MIN_INTER_TIME ((uint16_t)(40*16)) /* t~=40us dist=350cm */
133 #define MAX_INTER_TIME ((uint16_t)(2000*16)) /* t=2ms dist=10cm */
134 #define IR_DELAY ((uint16_t)(2000*16))
135 #define INTER_LASER_TIME 10 /* in ms */
137 #error "speed not defined"
140 extern prog_uint16_t framedist_table[];
141 /* basic functions to transmit on IR */
143 static inline void xmit_0(void)
145 uint16_t t = ((N_CYCLES_PERIOD * N_PERIODS) / 4);
147 cbi(IR_PORT, IR_BIT);
152 _delay_loop_2(t); /* 4 cycles per loop */
155 static inline void xmit_1(void)
157 uint16_t t = ((N_CYCLES_PERIOD * N_PERIODS) / 4);
159 sbi(IR_PORT, IR_BIT);
161 TCCR1B = _BV(WGM13) | _BV(WGM12);
162 TCNT1 = N_CYCLES_PERIOD-1;
163 TCCR1A = _BV(COM1A1) | _BV(WGM11);
164 ICR1 = N_CYCLES_PERIOD;
165 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
167 _delay_loop_2(t); /* 4 cycles per loop */
170 /* transmit in manchester code */
172 static inline void xmit_manchester_0(void)
178 static inline void xmit_manchester_1(void)
184 /* transmit a full frame. Each byte is lsb first. */
186 static void xmit_bits(uint32_t frame, uint8_t nbit)
190 for (i=0; i<nbit; i++) {
203 /* val is 12 bits. Return the 16 bits value that includes the 4 bits
205 static uint16_t do_cksum(uint16_t val)
210 /* add the three 4-bits blocks of x together */
214 cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
217 cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
218 cksum = (~cksum) & 0xf;
219 return (cksum << 12) + (val & 0xfff);
222 /* get the frame from laser time difference, return 0 on error (this
223 * frame cannot be sent). */
224 static uint32_t get_frame(uint16_t laserdiff)
227 uint16_t frame_dist = 256;
230 /* for calibration, return the time */
234 for (step = 128; step != 0; step /= 2) {
235 val = pgm_read_word(&framedist_table[frame_dist]);
242 frame |= ((uint32_t)(frame_dist & FRAME_DATA_MASK) << FRAME_DATA_SHIFT);
244 /* process cksum and return */
245 return do_cksum(frame);
248 /* Wait 2 consecutive rising edges on photodiode. Return 0 on success,
249 * in this case, the 'when' pointed area is assigned to the time when
250 * IR signal should be sent. The 'laserdiff' pointer is the time
251 * between the 2 lasers, in timer unit. */
252 static inline int8_t wait_laser(uint16_t *when, uint16_t *laserdiff)
254 uint16_t time1, time2;
257 #if (defined SPEED_40RPS) || (defined SPEED_20RPS)
258 /* set timer to 16Mhz, we will use ICP */
262 /* set timer to 2Mhz, we will use ICP */
267 /* wait until all is off (inverted logic) */
268 while (READ_PHOTO() == 0);
271 /* wait falling edge */
272 while ((TIFR & _BV(ICF1)) == 0);
277 /* wait a bit to avoid oscillations */
278 while ((uint16_t)(TCNT1-time1) < MIN_INTER_TIME);
281 /* wait next falling edge + timeout */
282 while ((TIFR & _BV(ICF1)) == 0) {
283 diff = TCNT1 - time1;
284 if (diff > MAX_INTER_TIME)
290 /* get time of 2nd laser */
294 /* process time difference */
295 diff = time2 - time1;
296 if (diff < MIN_INTER_TIME)
299 *when = time1 + (diff/2) + IR_DELAY;
312 uint32_t frame = FRAME;
319 LED_DDR = _BV(LED1_BIT) | _BV(LED2_BIT) | _BV(LED3_BIT);
320 IR_DDR |= _BV(IR_BIT);
323 fdevopen(uart0_dev_send, uart0_dev_recv);
329 c = uart_recv_nowait(0);
331 printf("%c", (char)(c+1));
348 #ifndef NO_MODULATION
350 ICR1 = N_CYCLES_PERIOD;
352 TCCR1A = _BV(COM1A1) | _BV(WGM11);
353 TCCR1B = _BV(WGM13) | _BV(WGM12);
358 ICR1 = N_CYCLES_PERIOD;
360 TCCR1B = _BV(WGM13) | _BV(WGM12);
361 TCNT1 = N_CYCLES_PERIOD-1;
362 TCCR1A = _BV(COM1A1) | _BV(WGM11);
363 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
368 /* test freq ~ 400khz */
371 TCCR1B = _BV(WGM13) | _BV(WGM12);
373 TCCR1A = _BV(COM1A1) | _BV(WGM11);
374 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
376 /* motor PWM 50%, 8khz */
379 TCCR2 = _BV(WGM21) | _BV(WGM20) | _BV(COM21) | _BV(CS21) ;
385 /* configure timer 0, prescaler = 64 */
386 TCCR0 = _BV(CS01) | _BV(CS00);
391 ret = wait_laser(&when, &diff);
399 frame = get_frame(diff);
400 /* cannot convert into frame... skip it */
402 wait_ms(INTER_LASER_TIME);
406 /* wait before IR xmit */
407 while ((int16_t)(when-TCNT1) > 0);
411 /* ok, transmit frame */
412 xmit_bits(frame, FRAME_LEN);
416 /* don't watch a laser during this time */
417 wait_ms(INTER_LASER_TIME);