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 $
24 #include <aversive/wait.h>
35 /******************* TSOP */
37 #define EICRx_TSOP EICRB /* EICRA is not ok, cannot do intr on any edge */
39 #define INTx_TSOP INT6
40 #define ISCx0_TSOP ISC60
41 #define ISCx1_TSOP ISC61
42 #define SIG_TSOP SIG_INTERRUPT6
43 #define TSOP_READ() (PINE & 0x40)
45 #define INTx_TSOP INT4
46 #define ISCx0_TSOP ISC40
47 #define ISCx1_TSOP ISC41
48 #define SIG_TSOP SIG_INTERRUPT4
49 #define TSOP_READ() (PINE & 0x10)
52 #define TSOP_FREQ_MHZ 0.455
53 #define TSOP_PERIOD_US (1./TSOP_FREQ_MHZ)
56 #define TSOP_TIME_SHORT_US (1.5 * N_PERIODS * TSOP_PERIOD_US)
57 #define TSOP_TIME_LONG_US (2.5 * N_PERIODS * TSOP_PERIOD_US)
59 #define TSOP_TIME_SHORT ((uint16_t)(TSOP_TIME_SHORT_US*2))
60 #define TSOP_TIME_LONG ((uint16_t)(TSOP_TIME_LONG_US*2))
65 static uint16_t start_angle_time;
66 static uint16_t frame;
71 struct detected_frame {
76 #define FRAME_RING_ORDER 4
77 #define FRAME_RING_SIZE (1<<FRAME_RING_ORDER)
78 #define FRAME_RING_MASK (FRAME_RING_SIZE-1)
79 static uint8_t frame_ring_head = 0;
80 static uint8_t frame_ring_tail = 0;
81 static struct detected_frame frame_ring[FRAME_RING_SIZE];
83 /********************** CS */
85 /* 8ms, easier if it's a pow of 2 */
86 #define CS_PERIOD_US (8192)
87 #define CS_PERIOD ((uint16_t)(CS_PERIOD_US*2))
88 #define CPT_ICR_MAX (uint8_t)((1000000UL/(uint32_t)CS_PERIOD_US)) /* too slow = 1 tr/s */
89 #define CPT_ICR_MIN (uint8_t)((10000UL/(uint32_t)CS_PERIOD_US)) /* too fast = 100 tr/s */
92 #define CS_CONSIGN (25 * 1000L)
95 * - clear on timer compare (CTC)
96 * - Toggle OC0 on compare match
98 #define LASER_ON() do { TCCR0 = _BV(WGM01) | _BV(COM00) | _BV(CS00); } while (0)
99 #define LASER_OFF() do { TCCR0 = 0; } while (0)
101 struct beacon_tsop beacon_tsop;
103 void debug_serial(void)
108 c = uart_recv_nowait(0);
110 printf("%c", (char)(c+1));
119 void debug_tsop(void)
135 static uint8_t led_cpt = 0;
138 static uint8_t prev_tsop = 0;
142 static uint16_t prev_time;
149 cur_tsop = TSOP_READ();
150 diff_time = cur_time - prev_time;
152 /* first rising edge */
153 if (cur_tsop && diff_time > TSOP_TIME_LONG) {
157 start_angle_time = cur_time - ref_time;
161 else if (diff_time < TSOP_TIME_SHORT) {
170 else if (diff_time < TSOP_TIME_LONG) {
179 if (len == FRAME_LEN*2) {
180 uint8_t tail_next = (frame_ring_tail+1) & FRAME_RING_MASK;
181 if (tail_next != frame_ring_head) {
182 frame_ring[frame_ring_tail].frame = frame;
183 frame_ring[frame_ring_tail].time = start_angle_time;
184 frame_ring_tail = tail_next;
191 prev_time = cur_time;
192 prev_tsop = cur_tsop;
196 static inline int32_t AbS(int32_t x)
204 /* Get the speed of motor (tr / 1000s)
205 * - icr_cpt is the number of CS period between 2 ICR updates
206 * - icr_diff is the difference of ICR values between the ICR updates
207 * (modulo 65536 obviously) */
208 static inline int32_t get_speed(uint8_t icr_cpt, uint16_t icr_diff)
210 int32_t best_diff = 65536L;
211 int8_t best_cpt = -2;
215 /* too slow (less than 1 tr/s) */
216 if (icr_cpt > CPT_ICR_MAX)
219 /* too fast (more than 100 tr/s) */
220 if (icr_cpt < CPT_ICR_MIN)
223 /* try to get the real time knowning icr_cpt and icr_diff */
224 for (i=-1; i<2; i++) {
225 diff = ((icr_cpt+i)&3) * 16384L;
226 diff += (icr_diff & 0x3fff);
233 if (AbS(diff) < AbS(best_diff)) {
235 best_cpt = icr_cpt + i;
239 /* real time difference in 1/2 us */
240 diff = (best_cpt * 16384L) + (icr_diff & 0x3fff);
241 return 2000000000L/diff;
246 uint16_t prev_cs = 0;
247 uint16_t prev_icr = 0;
249 uint16_t diff_icr = 0;
252 int32_t speed, out, err;
257 LED1_DDR |= _BV(LED1_BIT);
258 LED2_DDR |= _BV(LED2_BIT);
259 LED3_DDR |= _BV(LED3_BIT);
260 DDRB |= 0x10; /* OC0 (laser pwm) */
263 pid_init(&beacon_tsop.pid);
264 pid_set_gains(&beacon_tsop.pid, 500, 0, 0);
265 pid_set_maximums(&beacon_tsop.pid, 0, 20000, 4095);
266 pid_set_out_shift(&beacon_tsop.pid, 10);
267 pid_set_derivate_filter(&beacon_tsop.pid, 4);
270 fdevopen(uart0_dev_send, uart0_dev_recv);
272 rdline_init(&beacon_tsop.rdl, write_char, valid_buffer, complete_buffer);
273 snprintf(beacon_tsop.prompt, sizeof(beacon_tsop.prompt), "beacon > ");
274 rdline_newline(&beacon_tsop.rdl, beacon_tsop.prompt);
279 /* configure external interrupt for TSOP */
280 EICRx_TSOP |= _BV(ISCx0_TSOP);
281 EIMSK |= _BV(INTx_TSOP);
284 PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10,
285 TIMER1_PRESCALER_DIV_1);
286 PWM_NG_INIT16(&beacon_tsop.pwm_motor, 1, A, 10, 0, NULL, 0);
289 * - clear on timer compare (CTC)
290 * - Toggle OC0 on compare match
292 TCCR0 = _BV(WGM01) | _BV(COM00) | _BV(CS00);
293 OCR0 = 80; /* f = 100 khz at 16 Mhz */
295 /* configure timer 3: CLK/8
296 * it is used as a reference time
297 * enable noise canceller for ICP3 */
298 TCCR3B = _BV(ICNC3) | _BV(CS11);
302 /* Control system will be done in main loop */
305 /* process pending bytes on uart */
308 /* monitor the value of ICR (which is modified
309 * automatically on TT rising edge). If the value
310 * changed, process the time difference. */
311 if (ETIFR & _BV(ICF3)) {
318 diff_icr = (icr - prev_icr);
322 speed = get_speed(cpt_icr, diff_icr);
325 /* read time reference */
331 if (tcnt3 - prev_cs < CS_PERIOD)
341 /* process CS... maybe we don't need to use
342 * control_system_manager, just PID is enough */
343 if (cpt == CPT_ICR_MAX)
346 speed = get_speed(cpt_icr, diff_icr);
348 /* enabled laser when rotation speed if at least 5tr/s */
354 err = CS_CONSIGN - speed;
355 out = pid_do_filter(&beacon_tsop.pid, err);
356 if (x == 0 && beacon_tsop.debug_speed)
357 printf("%ld %ld\n", speed, out);
364 pwm_ng_set(&beacon_tsop.pwm_motor, out);
368 /* count the number of CS period between 2 ICR
370 if (cpt < CPT_ICR_MAX)
373 /* after CS, check if we have a new frame in ring */
374 if (frame_ring_head != frame_ring_tail) {
376 head_next = (frame_ring_head+1) & FRAME_RING_MASK;
377 if (beacon_tsop.debug_frame)
378 printf("%x %d\n", frame_ring[frame_ring_head].frame,
379 frame_ring[frame_ring_head].time);
380 frame_ring_head = head_next;