summaryrefslogtreecommitdiff
path: root/n/asserv/src/main.c
blob: 7650014d2cf8bdb983639b112401100c7c9085af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* main.c */
/* asserv - Position & speed motor control on a ATmega128. {{{
 *
 * Copyright (C) 2004 Nicolas Schodet
 *
 * Robot APB Team/Efrei 2005.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: robot AT efrei DOT fr
 *
 * 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
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "dsp.h"
#include <n/avr/rs232/rs232.h>
#include <n/avr/proto/proto.h>
#include <n/avr/utils/utils.h>
#include <n/avr/utils/byte.h>
#include <n/avr/twi-master/twi_master.h>
#include <avr/interrupt.h>
#include <avr/io.h>

#include "pwm.c"
#include "timer.c"
#include "counter.c"
#include "speed.c"
#include "postrack.c"
#include "goto.c"
#include "eeprom.c"

/** Use Taz. */
uint8_t motor_taz;

/** Motor mode :
 * 0 - pwm setup.
 * 1 - speed control.
 * 2 - position control. */
int8_t motor_mode;

/** Main loop counter. */
uint8_t motor_loop_cpt;

/** Report of counters. */
uint8_t motor_stat_counter, motor_stat_counter_cpt;

/** Report position. */
uint8_t motor_stat_postrack, motor_stat_postrack_cpt;

/** Goto debug. */
uint8_t motor_stat_goto_debug, motor_stat_goto_debug_cpt;

/** Statistics about speed control. */
uint8_t motor_stat_speed, motor_stat_speed_cpt;

/** Statistics about pwm values. */
uint8_t motor_stat_pwm, motor_stat_pwm_cpt;

/** Report of timer. */
uint8_t motor_stat_timer, motor_stat_timer_cpt;

/** Print PINC. */
uint8_t motor_print_pinc, motor_print_pinc_cpt;

/** Print Sharps. */
uint8_t motor_print_sharps, motor_print_sharps_cpt;
uint16_t motor_sharps[3];

/** Record timer value at different stage of computing. Used for performance
 * analisys. */
uint8_t motor_timer_0, motor_timer_1, motor_timer_2, motor_timer_3,
	motor_timer_4;

#include "taz.c"

/* +AutoDec */

/* Main loop. */
static void
main_loop (void);

/* -AutoDec */

/** Entry point. */
int
main (void)
{
    DDRD = 0x60;
    pwm_init ();
    timer_init ();
    counter_init ();
    speed_init ();
    postrack_init ();
    rs232_init ();
    twi_master_init ();
    taz_init ();
    eeprom_read_params ();
    proto_send0 ('z');
    sei ();
    main_loop ();
    return 0;
}

/** Main loop. */
static void
main_loop (void)
{
    while (1)
      {
	motor_timer_0 = timer_read ();
	while (!timer_pending ())
	    counter_poll ();
	counter_update ();
	motor_timer_4 = timer_read ();
	/* Position tracking. */
	postrack_update ();
	motor_timer_3 = timer_read ();
	/* Taz! */
	if (motor_taz)
	    taz_update ();
	/* Position control. */
	if (motor_mode >= 2)
	  {
	    goto_update ();
	    if (goto_finish)
		proto_send0 ('F');
	  }
	motor_timer_2 = timer_read ();
	/* Speed control. */
	if (motor_mode >= 1)
	  {
	    speed_update ();
	    speed_compute_left_pwm ();
	    speed_compute_right_pwm ();
	  }
	motor_timer_1 = timer_read ();
	/* Pwm setup. */
	pwm_update ();
	/* Stats. */
	if (motor_stat_counter && !--motor_stat_counter_cpt)
	  {
	    proto_send2w ('C', counter_left, counter_right);
	    motor_stat_counter_cpt = motor_stat_counter;
	  }
	if (motor_stat_postrack && !--motor_stat_postrack_cpt)
	  {
	    proto_send1d ('X', postrack_x);
	    proto_send1d ('Y', postrack_y);
	    proto_send1d ('A', postrack_a);
	    motor_stat_postrack_cpt = motor_stat_postrack;
	  }
	if (motor_stat_goto_debug && !--motor_stat_goto_debug_cpt)
	  {
	    proto_send4d ('G', goto_dx, goto_dy, goto_dl, goto_da);
	    motor_stat_goto_debug_cpt = motor_stat_goto_debug;
	  }
	if (motor_stat_speed && !--motor_stat_speed_cpt)
	  {
	    proto_send4w ('S', speed_left_e_old, speed_left_int,
			  speed_right_e_old, speed_right_int);
	    motor_stat_speed_cpt = motor_stat_speed;
	  }
	if (motor_stat_pwm && !--motor_stat_pwm_cpt)
	  {
	    proto_send2w ('W', pwm_left, pwm_right);
	    motor_stat_pwm_cpt = motor_stat_pwm;
	  }
	if (motor_stat_timer && !--motor_stat_timer_cpt)
	  {
	    proto_send5b ('T', motor_timer_4, motor_timer_3, motor_timer_2,
			  motor_timer_1, motor_timer_0);
	    motor_stat_timer_cpt = motor_stat_timer;
	  }
	if (motor_print_pinc && !--motor_print_pinc_cpt)
	  {
	    proto_send1b ('P', PINA);
	    motor_print_pinc_cpt = motor_print_pinc;
	  }
	if (motor_print_sharps && !--motor_print_sharps_cpt)
	  {
	    if (twi_master_is_finished ())
	      {
		proto_send3w ('H', motor_sharps[0], motor_sharps[1],
			      motor_sharps[2]);
		twi_master_read (0x02, (uint8_t *) motor_sharps, 6);
	      }
	    motor_print_sharps_cpt = motor_print_sharps;
	  }
	/* Misc. */
	while (rs232_poll ())
	    proto_accept (rs232_getc ());
	motor_loop_cpt++;
      }
}

/** Handle incoming messages. */
void
proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
{
#define c(cmd, size) (cmd << 8 | size)
    switch (c (cmd, size))
      {
      case c ('z', 0):
	reset ();
	break;
    /* Commands. */
      case c ('t', 1):
	motor_taz = args[0];
	break;
      case c ('l', 2):
	/* Linear move.
	 * - w: distance (negative to go backward). */
	motor_mode = 2;
	goto_mode = 0;
	goto_sign = args[0] >> 7;
	goto_d = v8_to_v16 (args[0], args[1]);
	if (goto_sign) goto_d = -goto_d;
	goto_x = postrack_x;
	goto_y = postrack_y;
	goto_finish = 0;
	break;
      case c ('a', 1):
	/* Angular move.
	 * - b: absolute angle. */
	motor_mode = 2;
	goto_mode = 1;
	goto_a = args[0];
	goto_finish = 0;
	break;
      case c ('g', 8):
	/* Go to position move.
	 * - d: f24.8, x position.
	 * - d: f24.8, y position. */
	motor_mode = 2;
	goto_mode = 2;
	goto_x = v8_to_v32 (args[0], args[1], args[2], args[3]);
	goto_y = v8_to_v32 (args[4], args[5], args[6], args[7]);
	goto_finish = 0;
	break;
      case c ('f', 1):
	/* Fuck the wall move.
	 * - b: speed. */
	motor_mode = 2;
	goto_mode = 10;
	goto_s = args[0];
	goto_finish = 0;
	break;
      case c ('F', 1):
	/* Setup finish flag.
	 * - b: new value. */
	goto_finish = args[0];
	break;
      case c ('s', 0):
	/* Set zero speed (stop). */
	motor_mode = 1;
	speed_left_aim = 0;
	speed_right_aim = 0;
	break;
      case c ('s', 2):
	/* Set speed.
	 * - b: left speed.
	 * - b: right speed. */
	motor_mode = 1;
	speed_left_aim = args[0];
	speed_right_aim = args[1];
	break;
      case c ('w', 0):
	/* Set zero pwm. */
	speed_restart ();
	motor_mode = 0;
	pwm_left = 0;
	pwm_right = 0;
	break;
      case c ('w', 4):
	/* Set pwm.
	 * - w: left pwm.
	 * - w: right pwm. */
	speed_restart ();
	motor_mode = 0;
	pwm_left = v8_to_v16 (args[0], args[1]);
	pwm_right = v8_to_v16 (args[2], args[3]);
	break;
    /* Stats.
     * - b: interval between stats. */
      case c ('C', 1):
	/* Counter stats. */
	motor_stat_counter_cpt = motor_stat_counter = args[0];
	break;
      case c ('X', 1):
	/* Position report. */
	motor_stat_postrack_cpt = motor_stat_postrack = args[0];
	break;
      case c ('G', 1):
	motor_stat_goto_debug_cpt = motor_stat_goto_debug = args[0];
	break;
      case c ('S', 1):
	/* Motor control stats. */
	motor_stat_speed_cpt = motor_stat_speed = args[0];
	break;
      case c ('W', 1):
	/* Pwm stats. */
	motor_stat_pwm_cpt = motor_stat_pwm = args[0];
	break;
      case c ('T', 1):
	/* Timing stats. */
	motor_stat_timer_cpt = motor_stat_timer = args[0];
	break;
      case c ('P', 1):
	/* Input port stats. */
	motor_print_pinc_cpt = motor_print_pinc = args[0];
	break;
      case c ('H', 1):
	/* Sharps stats. */
	twi_master_read (0x02, (uint8_t *) motor_sharps, 6);
	motor_print_sharps_cpt = motor_print_sharps = args[0];
	break;
      default:
	/* Params. */
	if (cmd == 'p')
	  {
	    switch (c (args[0], size))
	      {
	      case c ('x', 5):
		/* Set current x position. */
		postrack_x = v8_to_v32 (args[1], args[2], args[3], args[4]);
		break;
	      case c ('y', 5):
		/* Set current y position. */
		postrack_y = v8_to_v32 (args[1], args[2], args[3], args[4]);
		break;
	      case c ('a', 5):
		/* Set current angle. */
		postrack_a = v8_to_v32 (args[1], args[2], args[3], args[4]);
		break;
	      case c ('f', 3):
		/* Set footing. */
		postrack_set_footing (v8_to_v16 (args[1], args[2]));
		break;
	      case c ('e', 5):
		/* Set epsillon. */
		goto_eps = v8_to_v32 (args[1], args[2], args[3], args[4]);
		break;
	      case c ('p', 3):
		speed_kp = v8_to_v16 (args[1], args[2]);
		break;
	      case c ('i', 3):
		speed_ki = v8_to_v16 (args[1], args[2]);
		break;
	      case c ('d', 3):
		speed_kd = v8_to_v16 (args[1], args[2]);
		break;
	      case c ('I', 3):
		speed_int_max = v8_to_v16 (args[1], args[2]);
		break;
	      case c ('a', 2):
		/* Set acceleration. */
		speed_acc_cpt = speed_acc = args[1];
		break;
	      case c ('m', 2):
		/* Set maximum speed for automatic movements. */
		speed_max = args[1];
		break;
	      case c ('w', 3):
		/* Set PWM direction.
		 * - b: inverse left direction.
		 * - b: inverse right direction. */
		pwm_dir = 0;
		if (args[1]) pwm_dir |= _BV (PWM_LEFT_DIR);
		if (args[2]) pwm_dir |= _BV (PWM_RIGHT_DIR);
		break;
	      case c ('E', 2):
		/* Write to eeprom.
		 * - b: 00: clear config, 01: write config. */
		if (args[1])
		    eeprom_write_params ();
		else
		    eeprom_clear_params ();
		break;
	      default:
		proto_send0 ('?');
		return;
	      }
	  }
	else
	  {
	    proto_send0 ('?');
	    return;
	  }
	break;
      }
    proto_send (cmd, size, args);
#undef c
}