summaryrefslogtreecommitdiff
path: root/n/asserv/src/asserv/main.c
blob: f56c662501bd5a6be35a50b4dc6ccd169165176d (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
/* main.c */
/* asserv - Position & speed motor control on AVR. {{{
 *
 * Copyright (C) 2005 Nicolas Schodet
 *
 * Robot APB Team/Efrei 2006.
 *        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 "common.h"
#include "modules/uart/uart.h"
#include "modules/proto/proto.h"
#include "modules/utils/utils.h"
#include "modules/utils/byte.h"
#include "io.h"

#include "misc.h"

/* This is implementation include. */
#include "timer.c"
#include "counter.c"
#include "pwm.c"
#include "pos.c"
#include "speed.c"
#include "eeprom.c"

/** Motor control mode:
 * 0: pwm setup.
 * 1: shaft position control.
 * 2: speed control.
 * 3: trajectory control. */
int8_t main_mode;

/** Report of counters. */
uint8_t main_stat_counter, main_stat_counter_cpt;

/** Statistics about shaft position control. */
uint8_t main_stat_pos, main_stat_pos_cpt;

/** Statistics about pwm values. */
uint8_t main_stat_pwm, main_stat_pwm_cpt;

/** Report of timer. */
uint8_t main_stat_timer, main_stat_timer_cpt;

/** Print input port. */
uint8_t main_print_pin, main_print_pin_cpt;

/** Record timer value at different stage of computing. Used for performance
 * analisys. */
uint8_t main_timer_0, main_timer_1, main_timer_2, main_timer_3, main_timer_4,
	main_timer_5;

/* +AutoDec */

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

/* -AutoDec */

/** Entry point. */
int
main (int argc, char **argv)
{
    DDRD = 0x60;
    /* Pull-ups. */
    PORTA = 0xff;
    PORTC = 0xff;
    eeprom_read_params ();
    pwm_init ();
    timer_init ();
    counter_init ();
    uart0_init ();
    proto_send0 ('z');
    sei ();
    while (1)
	main_loop ();
    return 0;
}

/** Main loop. */
static void
main_loop (void)
{
    main_timer_5 = timer_read ();
    timer_wait ();
    /* Counter update. */
    counter_update ();
    main_timer_0 = timer_read ();
    /* Postion control. */
    if (main_mode >= 1)
	pos_update ();
    main_timer_1 = timer_read ();
    /* Pwm setup. */
    pwm_update ();
    main_timer_2 = timer_read ();
    /* Speed control. */
    if (main_mode >= 2)
	speed_update ();
    main_timer_3 = timer_read ();
    /* Stats. */
    if (main_stat_counter && !--main_stat_counter_cpt)
      {
	proto_send2w ('C', counter_left, counter_right);
	main_stat_counter_cpt = main_stat_counter;
      }
    if (main_stat_pos && !--main_stat_pos_cpt)
      {
	proto_send4w ('P', pos_theta_e_old, pos_theta_int,
		      pos_alpha_e_old, pos_alpha_int);
	main_stat_pos_cpt = main_stat_pos;
      }
    if (main_stat_pwm && !--main_stat_pwm_cpt)
      {
	proto_send2w ('W', pwm_left, pwm_right);
	main_stat_pwm_cpt = main_stat_pwm;
      }
    if (main_stat_timer && !--main_stat_timer_cpt)
      {
	proto_send6b ('T', main_timer_0, main_timer_2, main_timer_3,
		      main_timer_4, main_timer_4, main_timer_5);
	main_stat_timer_cpt = main_stat_timer;
      }
    if (main_print_pin && !--main_print_pin_cpt)
      {
	proto_send1b ('I', PINA);
	main_print_pin_cpt = main_print_pin;
      }
    /* Misc. */
    while (uart0_poll ())
	proto_accept (uart0_getc ());
    main_timer_4 = timer_read ();
}

/** 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):
	utils_reset ();
	break;
    /* Commands. */
      case c ('w', 0):
	/* Set zero pwm. */
	pos_reset ();
	main_mode = 0;
	pwm_left = 0;
	pwm_right = 0;
	break;
      case c ('w', 4):
	/* Set pwm.
	 * - w: left pwm.
	 * - w: right pwm. */
	pos_reset ();
	main_mode = 0;
	pwm_left = v8_to_v16 (args[0], args[1]);
	pwm_right = v8_to_v16 (args[2], args[3]);
	break;
      case c ('c', 4):
	/* Add to position consign.
	 * - w: theta consign offset.
	 * - w: alpha consign offset. */
	main_mode = 1;
	pos_theta_cons += v8_to_v16 (args[0], args[1]);
	pos_alpha_cons += v8_to_v16 (args[2], args[3]);
	break;
      case c ('s', 0):
	/* Stop (set zero speed). */
	main_mode = 2;
	speed_pos = 0;
	speed_theta_cons = 0;
	speed_alpha_cons = 0;
	break;
      case c ('s', 2):
	/* Set speed.
	 * - b: theta speed.
	 * - b: alpha speed. */
	main_mode = 2;
	speed_pos = 0;
	speed_theta_cons = args[0] << 8;
	speed_alpha_cons = args[1] << 8;
	break;
    /* Stats.
     * - b: interval between stats. */
      case c ('C', 1):
	/* Counter stats. */
	main_stat_counter_cpt = main_stat_counter = args[0];
	break;
      case c ('P', 1):
	/* Motor position control stats. */
	main_stat_pos_cpt = main_stat_pos = args[0];
	break;
      case c ('W', 1):
	/* Pwm stats. */
	main_stat_pwm_cpt = main_stat_pwm = args[0];
	break;
      case c ('T', 1):
	/* Timing stats. */
	main_stat_timer_cpt = main_stat_timer = args[0];
	break;
      case c ('I', 1):
	/* Input port stats. */
	main_print_pin_cpt = main_print_pin = args[0];
	break;
      default:
	/* Params. */
	if (cmd == 'p')
	  {
	    switch (c (args[0], size))
	      {
	      case c ('a', 5):
		speed_theta_acc = v8_to_v16 (args[1], args[2]);
		speed_alpha_acc = v8_to_v16 (args[3], args[4]);
		break;
	      case c ('p', 3):
		pos_theta_kp = pos_alpha_kp = v8_to_v16 (args[1], args[2]);
		break;
	      case c ('p', 5):
		pos_theta_kp = v8_to_v16 (args[1], args[2]);
		pos_alpha_kp = v8_to_v16 (args[3], args[4]);
		break;
	      case c ('i', 3):
		pos_theta_ki = pos_alpha_ki = v8_to_v16 (args[1], args[2]);
		break;
	      case c ('i', 5):
		pos_theta_ki = v8_to_v16 (args[1], args[2]);
		pos_alpha_ki = v8_to_v16 (args[3], args[4]);
		break;
	      case c ('d', 3):
		pos_theta_kd = pos_alpha_kd = v8_to_v16 (args[1], args[2]);
		break;
	      case c ('d', 5):
		pos_theta_kd = v8_to_v16 (args[1], args[2]);
		pos_alpha_kd = v8_to_v16 (args[3], args[4]);
		break;
	      case c ('E', 3):
		pos_e_sat = v8_to_v16 (args[1], args[2]);
		break;
	      case c ('I', 3):
		pos_int_sat = v8_to_v16 (args[1], args[2]);
		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;
	      case c ('P', 1):
		/* Print current settings. */
		proto_send1b ('E', EEPROM_KEY);
		proto_send2w ('p', pos_theta_kp, pos_alpha_kp);
		proto_send2w ('i', pos_theta_ki, pos_alpha_ki);
		proto_send2w ('d', pos_theta_kd, pos_alpha_kd);
		proto_send1w ('E', pos_e_sat);
		proto_send1w ('I', pos_int_sat);
		proto_send1b ('w', pwm_dir);
		break;
	      default:
		proto_send0 ('?');
		return;
	      }
	  }
	else
	  {
	    proto_send0 ('?');
	    return;
	  }
	break;
      }
    proto_send (cmd, size, args);
#undef c
}