summaryrefslogtreecommitdiff
path: root/n/line-follower/src/main.c
blob: 3e7d84e1a02b831e3b003b8bb103042e68b3f03e (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
/* 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 <avr/interrupt.h>
#include <avr/io.h>

#include "pwm.c"
#include "timer.c"
#include "counter.c"
#include "speed.c"
#include "postrack.c"
#include "linesensor.c"

/** Motor mode :
 * 0 - pwm setup;
 * 1 - speed control;
 * 2 - position control;
 * 3 - linefol mode;
 * 4 - linefol calibration mode. */
uint8_t motor_mode;

/** Main loop counter. */
uint8_t motor_loop_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;

/** Statistics about pid components. */
uint8_t motor_stat_pid, motor_stat_pid_cpt;
int16_t pid_pid,pid_p,pid_i,pid_d

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

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

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

/** 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;

/* +AutoDec */

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

/** Handle incoming messages. */
static void
proto_callback (uint8_t cmd, uint8_t argc, proto_arg_t argv[]);

/* -AutoDec */

/* Entry point. */
int
main (void)
{
    DDRD = 0x60;
    pwm_init (0);
    timer_init ();
    counter_init ();
    speed_init ();
    postrack_init ();
    rs232_init ();
    proto_init (proto_callback, rs232_putc);
    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_3 = timer_read ();
	postrack_update ();
	motor_timer_2 = timer_read ();
	/* Speed control. */
	switch (motor_mode)
	  {
	  case 1:
	  case 2:
	    speed_update ();
	    speed_compute_left_pwm ();
	    speed_compute_right_pwm ();
	    break;
	  case 3:
	    linesensor_poll ();
	    speed_compute_linefol_pwm ();
	    speed_compute_left_pwm ();
	    speed_compute_right_pwm ();
	    break;
	  case 4:
	    delay_ms (5L);
	    linesensor_print ();
	    break;
	  }
	motor_timer_1 = timer_read ();
	/* Pwm setup. */
	pwm_update ();
	/* Stats. */
	if (motor_stat_speed && !--motor_stat_speed_cpt)
	  {
	    proto_send4 ('U',
			 speed_left_e_old >> 8, speed_left_e_old,
			 speed_left_int >> 8, speed_left_int);
	    proto_send4 ('V',
			 speed_right_e_old >> 8, speed_right_e_old,
			 speed_right_int >> 8, speed_right_int);
	    motor_stat_speed_cpt = motor_stat_speed;
	  }
	// displays stats concerning pid components
	if (motor_stat_pid && !--motor_stat_pid_cpt)
	  {
	    proto_send4 ('C',
			 pid_pid >> 8, pid_pid,
			 pid_p >> 8, pid_p);
	    proto_send4 ('.',
			 pid_i >> 8, pid_i,
			 pid_d >> 8, pid_d);
	    motor_stat_pid_cpt = motor_stat_pid;
	  }
	if (motor_stat_pwm && !--motor_stat_pwm_cpt)
	  {
	    proto_send4 ('W',
			 pwm_left >> 8, pwm_left,
			 pwm_right >> 8, pwm_right);
	    motor_stat_pwm_cpt = motor_stat_pwm;
	  }
	if (motor_stat_timer && !--motor_stat_timer_cpt)
	  {
	    proto_send4 ('T', motor_timer_3, motor_timer_2, motor_timer_1,
			 motor_timer_0);
	    motor_stat_timer_cpt = motor_stat_timer;
	  }
	if (motor_stat_counter && !--motor_stat_counter_cpt)
	  {
	    proto_send4 ('C',
			 counter_left >> 8, counter_left,
			 counter_right >> 8, counter_right);
	    motor_stat_counter_cpt = motor_stat_counter;
	  }
	if (motor_stat_postrack && !--motor_stat_postrack_cpt)
	  {
	    proto_send4 ('X',
			 postrack_x >> 24, postrack_x >> 16,
			 postrack_x >> 8, postrack_x);
	    proto_send4 ('Y',
			 postrack_y >> 24, postrack_y >> 16,
			 postrack_y >> 8, postrack_y);
	    proto_send4 ('A',
			 postrack_a >> 24, postrack_a >> 16,
			 postrack_a >> 8, postrack_a);
	    motor_stat_postrack_cpt = motor_stat_postrack;
	  }
	/* Misc. */
	if ((motor_loop_cpt & 7) == 0)
	  {
	    if (rs232_poll ())
		proto_accept (rs232_getc ());
	  }
	motor_loop_cpt++;
      }
}

/** Handle incoming messages. */
static void
proto_callback (uint8_t cmd, uint8_t argc, proto_arg_t argv[])
{
#define c(cmd, argc) (cmd << 8 | argc)
    switch (c (cmd, argc))
      {
      case c ('z', 0):
	reset ();
	break;
      case c ('w', 0):
	speed_restart ();
	motor_mode = 0;
	pwm_left = 0;
	pwm_right = 0;
	break;
      case c ('w', 4):
	speed_restart ();
	motor_mode = 0;
	pwm_left = argv[0] << 8 | argv[1];
	pwm_right = argv[2] << 8 | argv[3];
	break;
      case c ('s', 2):
	motor_mode = 1;
	speed_left_aim = argv[0];
	speed_right_aim = argv[1];
	break;
      case c ('L', 0):
	motor_mode = 4;
	linesensor_reset ();
	break;
      case c ('l', 0):
	motor_mode = 3;
	linesensor_reset ();
	break;
      case c ('l', 1):
	motor_mode = 3;
	linesensor_reset ();
	linefol_max_pwm = argv[0];
	break;
      case c ('l', 5):
	motor_mode = 3;
	linesensor_reset ();
	linefol_max_pwm = argv[0];
	speed_kp = argv[1];
	speed_ki = argv[2];
	speed_kd = argv[3];
	K_ATT_LINEPOS_DER = argv[4];
	break;
      case c ('a', 1):
	speed_acc_cpt = speed_acc = argv[0];
	break;
      case c ('p', 2):
	speed_kp = argv[0] << 8 | argv[1];
	break;
      case c ('i', 2):
	speed_ki = argv[0] << 8 | argv[1];
	break;
      case c ('d', 2):
	speed_kd = argv[0] << 8 | argv[1];
	break;
      case c ('f', 2):
	postrack_set_footing (argv[0] << 8 | argv[1]);
	break;
      case c ('m', 1):
	motor_stat_speed_cpt = motor_stat_speed = argv[0];
	motor_stat_pwm_cpt = motor_stat_pwm = argv[0];
	break;
      case c ('C', 1):
	motor_stat_pid_cpt = motor_stat_pid = argv[0];
	break;
      case c ('c', 1):
	motor_stat_counter_cpt = motor_stat_counter = argv[0];
	break;
      case c ('t', 1):
	motor_stat_timer_cpt = motor_stat_timer = argv[0];
	break;
      case c ('T', 1):
	motor_stat_postrack_cpt = motor_stat_postrack = argv[0];
	break;
      case c ('P', 1):
	pwm_init (argv[0]);
	break;
      default:
	proto_send0 ('e');
	return;
      }
    proto_send (cmd, argc, argv);
#undef c
}