summaryrefslogtreecommitdiff
path: root/digital/asserv/src/asserv/simu.host.c
blob: 07e441ea2c4dae74d0b5d59204988ca292796c00 (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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* simu.host.c */
/* asserv - Position & speed motor control on AVR. {{{
 *
 * Copyright (C) 2006 Nicolas Schodet
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * 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 "simu.host.h"

#include "modules/host/host.h"
#include "modules/host/mex.h"
#include "modules/utils/utils.h"
#include "modules/math/fixed/fixed.h"

#include "io.h"

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "pwm.h"
#include "aux.h"

#include "contacts.h"

#include "motor_model.host.h"
#include "models.host.h"

/** Simulate some AVR regs. */
uint8_t DDRF, PORTC, PORTD, PORTE, PORTF, PORTG, PINC;

/** Overall counter values. */
uint16_t counter_left, counter_right,
	 counter_aux[AC_ASSERV_AUX_NB];
/** Counter differences since last update.
 * Maximum of 9 significant bits, sign included. */
int16_t counter_left_diff, counter_right_diff,
	counter_aux_diff[AC_ASSERV_AUX_NB];
/** Overall uncorrected counter values. */
static int32_t counter_right_raw;
/** Correction factor (f8.24). */
uint32_t counter_right_correction = 1L << 24;

/** PWM control states. */
struct pwm_t pwm_left = PWM_INIT_FOR (pwm_left);
struct pwm_t pwm_right = PWM_INIT_FOR (pwm_right);
struct pwm_t pwm_aux[AC_ASSERV_AUX_NB] = {
    PWM_INIT_FOR (pwm_aux0), PWM_INIT_FOR (pwm_aux1)
};
/** PWM reverse directions. */
uint8_t pwm_reverse;

/* Robot model. */
const struct robot_t *simu_robot;

/** Motor models. */
struct motor_t simu_left_model, simu_right_model,
	       simu_aux_model[AC_ASSERV_AUX_NB];

/** Computed simulated position (mm, rad). */
double simu_pos_x, simu_pos_y, simu_pos_a;

/** Full counter values. */
uint32_t simu_counter_left, simu_counter_right,
	 simu_counter_aux[AC_ASSERV_AUX_NB];
double simu_counter_left_th, simu_counter_right_th;

/** Use mex. */
int simu_mex;

/** Counter to limit the interval between information is sent. */
int simu_send_cpt;

/** Initialise simulation. */
static void
simu_init (void)
{
    int argc;
    char **argv;
    host_get_program_arguments (&argc, &argv);
    if (argc == 2 && strncmp (argv[0], "-m", 2) == 0)
      {
	simu_mex = atoi (argv[0] + 2);
	if (!simu_mex) simu_mex = 1;
	simu_send_cpt = simu_mex;
	mex_node_connect ();
	argc--; argv++;
      }
    if (argc != 1)
      {
	fprintf (stderr, "Syntax: asserv.host [-m[interval]] model\n");
	exit (1);
      }
    simu_robot = models_get (argv[0]);
    if (!simu_robot)
      {
	fprintf (stderr, "unknown model name: %s\n", argv[0]);
	exit (1);
      }
    models_init (simu_robot, &simu_left_model, &simu_right_model,
		 simu_aux_model);
    simu_pos_x = simu_pos_y = simu_pos_a = 0;
}

/** Update simulation position. */
static void
simu_pos_update (double dl, double dr, double footing)
{
    double d = 0.5 * (dl + dr);
    double da = (dr - dl) / footing;
    double na = simu_pos_a + da;
    if (da < 0.0001 && da > -0.0001)
      {
	/* Avoid a division by zero when angle is too small. */
	double a = simu_pos_a + da * 0.5;
	simu_pos_x += d * cos (a);
	simu_pos_y += d * sin (a);
      }
    else
      {
	/* Radius of turn is d / da. */
	simu_pos_x += (sin (na) - sin (simu_pos_a)) * d / da;
	simu_pos_y += (cos (simu_pos_a) - cos (na)) * d / da;
      }
    simu_pos_a = na;
}

/** Update sensors for Giboulee. */
void
simu_sensor_update_giboulee (void)
{
    /** Micro-switch sensors. */
    static const double sensors[][2] =
      {
	  { -70.0, 200.0 },
	  { -70.0, -200.0 },
	  { 170.0, 0.0 },
      };
    static const uint8_t sensors_bit[] =
      { IO_BV (CONTACT_BACK_LEFT_IO), IO_BV (CONTACT_BACK_RIGHT_IO),
	IO_BV (CONTACT_CENTER_IO), };
    static const double table_width = 3000.0, table_height = 2100.0;
    PINC = 0;
    unsigned int i;
    double x, y;
    for (i = 0; i < UTILS_COUNT (sensors); i++)
      {
	/* Compute absolute position. */
	x = simu_pos_x + cos (simu_pos_a) * sensors[i][0]
	    - sin (simu_pos_a) * sensors[i][1];
	y = simu_pos_y + sin (simu_pos_a) * sensors[i][0]
	    + cos (simu_pos_a) * sensors[i][1];
	if (x >= 0.0 && x < table_width && y >= 0.0 && y < table_height)
	    PINC |= sensors_bit[i];
      }
    /** Top zero sensor. */
    double aa;
    for (i = 0; i < AC_ASSERV_AUX_NB; i++)
      {
	aa = simu_aux_model[i].th / simu_aux_model[i].m.i_G * 3;
	if (!(cos (aa) > 0 && fabs (sin (aa)) * 80.0 < 7.5))
	    *aux[i].zero_pin |= aux[i].zero_bv;
      }
}

/** Update sensors for AquaJim. */
void
simu_sensor_update_aquajim (void)
{
    /** Micro-switch sensors. */
    static const double sensors[][2] =
      {
	  { -150.0, 70.0 },
	  { -150.0, -70.0 },
	  { 150.0, 130.0 },
	  { 150.0, -130.0 },
	  { 150.0, 0.0 },
      };
    static const uint8_t sensors_bit[] =
      { IO_BV (CONTACT_BACK_LEFT_IO), IO_BV (CONTACT_BACK_RIGHT_IO),
	IO_BV (CONTACT_FRONT_LEFT_IO), IO_BV (CONTACT_FRONT_RIGHT_IO),
	IO_BV (CONTACT_CENTER_IO), };
    static const double table_width = 3000.0, table_height = 2100.0;
    static const double center_zone_radius = 150.0;
    PINC = 0;
    unsigned int i;
    double x, y, cx, cy, ds;
    for (i = 0; i < UTILS_COUNT (sensors); i++)
      {
	/* Compute absolute position. */
	x = simu_pos_x + cos (simu_pos_a) * sensors[i][0]
	    - sin (simu_pos_a) * sensors[i][1];
	y = simu_pos_y + sin (simu_pos_a) * sensors[i][0]
	    + cos (simu_pos_a) * sensors[i][1];
	cx = table_width / 2 - x;
	cy = table_height / 2 - y;
	ds = cx * cx + cy * cy;
	if (x >= 0.0 && x < table_width && y >= 0.0 && y < table_height
	    && ds > center_zone_radius * center_zone_radius)
	    PINC |= sensors_bit[i];
      }
    /** Top zero sensors. */
    double aa = simu_aux_model[0].th / simu_aux_model[0].m.i_G
	/* Almost open. */
	+ 2 * M_PI / 6 / 10
	/* Turn at the next hole. */
	- 2 * M_PI / 3
	/* Mechanical offset. */
	+ 2 * M_PI * 0x43e / simu_robot->aux_encoder_steps[0] /
	simu_aux_model[0].m.i_G;
    double apos = aa / (2 * M_PI / 3);
    if (apos - floor (apos) > 0.5)
	PINC |= IO_BV (CONTACT_AUX0_ZERO_IO);
    if (simu_aux_model[1].th >= 0)
	PINC |= IO_BV (CONTACT_AUX1_ZERO_IO);
}

/** Do a simulation step. */
static void
simu_step (void)
{
    int i;
    double old_left_th, old_right_th, old_aux_th[AC_ASSERV_AUX_NB];
    /* Convert pwm value into voltage. */
    simu_left_model.u = simu_left_model.m.u_max
	* ((double) pwm_left.cur / (PWM_MAX + 1));
    simu_right_model.u = simu_right_model.m.u_max
	* ((double) pwm_right.cur / (PWM_MAX + 1));
    for (i = 0; i < AC_ASSERV_AUX_NB; i++)
	simu_aux_model[i].u = simu_aux_model[i].m.u_max
	    * ((double) pwm_aux[i].cur / (PWM_MAX + 1));
    /* Make one step. */
    old_left_th = simu_left_model.th;
    old_right_th = simu_right_model.th;
    motor_model_step (&simu_left_model);
    motor_model_step (&simu_right_model);
    for (i = 0; i < AC_ASSERV_AUX_NB; i++)
      {
	old_aux_th[i] = simu_aux_model[i].th;
	if (simu_robot->aux_motor[i])
	    motor_model_step (&simu_aux_model[i]);
      }
    /* Modify counters. */
    uint32_t counter_left_new;
    uint32_t counter_right_new;
    if (!simu_robot->encoder_separated)
      {
	counter_left_new = simu_left_model.th / (2*M_PI)
	    * simu_robot->main_encoder_steps;
	counter_right_new = simu_right_model.th / (2*M_PI)
	    * simu_robot->main_encoder_steps;
      }
    else
      {
	/* Thanks Thalès. */
	double left_diff = (simu_left_model.th - old_left_th)
	    / simu_left_model.m.i_G * simu_robot->wheel_r;
	double right_diff = (simu_right_model.th - old_right_th)
	    / simu_right_model.m.i_G * simu_robot->wheel_r;
	double sum = left_diff + right_diff;
	double diff = (left_diff - right_diff)
	    * (simu_robot->encoder_footing / simu_robot->footing);
	double left_enc_diff = 0.5 * (sum + diff);
	double right_enc_diff = 0.5 * (sum - diff);
	simu_counter_left_th += left_enc_diff / simu_robot->encoder_wheel_r;
	simu_counter_right_th += right_enc_diff / simu_robot->encoder_wheel_r;
	counter_left_new = simu_counter_left_th / (2*M_PI)
	    * simu_robot->main_encoder_steps;
	counter_right_new = simu_counter_right_th / (2*M_PI)
	    * simu_robot->main_encoder_steps;
      }
    /* Update an integer counter. */
    counter_left_diff = counter_left_new - simu_counter_left;
    counter_left += counter_left_diff;
    simu_counter_left = counter_left_new;
    counter_right_diff = counter_right_new - simu_counter_right;
    counter_right_raw += counter_right_diff;
    uint16_t right_new = fixed_mul_f824 (counter_right_raw,
					 counter_right_correction);
    counter_right_diff = (int16_t) (right_new - counter_right);
    counter_right = right_new;
    simu_counter_right = counter_right_new;
    /* Update auxiliary counter. */
    for (i = 0; i < AC_ASSERV_AUX_NB; i++)
      {
	if (simu_robot->aux_motor[i])
	  {
	    uint32_t counter_aux_new = simu_aux_model[i].th / (2*M_PI)
		* simu_robot->aux_encoder_steps[i];
	    counter_aux_diff[i] = counter_aux_new - simu_counter_aux[i];
	    counter_aux[i] += counter_aux_diff[i];
	    simu_counter_aux[i] = counter_aux_new;
	  }
	else
	  {
	    counter_aux_diff[i] = 0;
	    counter_aux[i] = 0;
	    simu_counter_aux[i] = 0;
	  }
      }
    /* Update position. */
    simu_pos_update ((simu_left_model.th - old_left_th)
		     / simu_left_model.m.i_G * simu_robot->wheel_r * 1000,
		     (simu_right_model.th - old_right_th)
		     / simu_right_model.m.i_G * simu_robot->wheel_r * 1000,
		     simu_robot->footing * 1000);
    /* Update sensors. */
    if (simu_robot->sensor_update)
	simu_robot->sensor_update ();
}

/** Send information to the other nodes. */
static void
simu_send (void)
{
    static int first = 1;
    int i;
    mex_msg_t *m;
    /* Send position. */
    static int16_t simu_pos_x_sent, simu_pos_y_sent;
    static int32_t simu_pos_a_sent;
    int16_t simu_pos_x_to_send = simu_pos_x;
    int16_t simu_pos_y_to_send = simu_pos_y;
    int32_t simu_pos_a_to_send = 1024.0 * simu_pos_a;
    if (first
	|| simu_pos_x_to_send != simu_pos_x_sent
	|| simu_pos_y_to_send != simu_pos_y_sent
	|| simu_pos_a_to_send != simu_pos_a_sent)
      {
	m = mex_msg_new (0xa0);
	mex_msg_push (m, "hhl", simu_pos_x_to_send, simu_pos_y_to_send,
		      simu_pos_a_to_send);
	mex_node_send (m);
	simu_pos_x_sent = simu_pos_x_to_send;
	simu_pos_y_sent = simu_pos_y_to_send;
	simu_pos_a_sent = simu_pos_a_to_send;
      }
    /* Send PWM. */
    static int16_t pwm_left_sent, pwm_right_sent;
    if (first
	|| pwm_left_sent == pwm_left.cur
	|| pwm_right_sent == pwm_right.cur)
      {
	m = mex_msg_new (0xa1);
	mex_msg_push (m, "hh", pwm_left.cur, pwm_right.cur);
	for (i = 0; i < AC_ASSERV_AUX_NB; i++)
	    mex_msg_push (m, "h", pwm_aux[i].cur);
	mex_node_send (m);
	pwm_left_sent = pwm_left.cur;
	pwm_right_sent = pwm_right.cur;
      }
    /* Send Aux position. */
    static int32_t simu_aux_model_sent[AC_ASSERV_AUX_NB];
    int32_t simu_aux_model_to_send[AC_ASSERV_AUX_NB];
    int simu_aux_model_changed = 0;
    for (i = 0; i < AC_ASSERV_AUX_NB; i++)
      {
	simu_aux_model_to_send[i] = 1024.0 * simu_aux_model[i].th
	    / simu_aux_model[i].m.i_G;
	if (!first && simu_aux_model_to_send[i] != simu_aux_model_sent[i])
	    simu_aux_model_changed = 1;
      }
    if (first || simu_aux_model_changed)
      {
	m = mex_msg_new (0xa8);
	for (i = 0; i < AC_ASSERV_AUX_NB; i++)
	  {
	    if (simu_robot->aux_motor[i])
		mex_msg_push (m, "l", simu_aux_model_to_send[i]);
	    else
		mex_msg_push (m, "l", 0);
	  }
	mex_node_send (m);
      }
    /* First send done. */
    first = 0;
}

/** Initialise the timer. */
void
timer_init (void)
{
    simu_init ();
}

/** Wait for timer overflow. */
void
timer_wait (void)
{
    if (simu_mex)
	mex_node_wait_date (mex_node_date () + 4);
    simu_step ();
    if (simu_mex && !--simu_send_cpt)
      {
	simu_send_cpt = simu_mex;
	simu_send ();
      }
}

/** Read timer value. Used for performance analysis. */
uint8_t
timer_read (void)
{
    return 0;
}

/** Initialize the counters. */
void
counter_init (void)
{
}

/** Update overall counter values and compute diffs. */
void
counter_update (void)
{
}

/** Initialise PWM generator. */
void
pwm_init (void)
{
}

/** Update the hardware PWM values. */
void
pwm_update (void)
{
}

void
eeprom_read_params (void)
{
}

void
eeprom_write_params (void)
{
}

void
eeprom_clear_params (void)
{
}

void
pwm_set_reverse (uint8_t reverse)
{
    pwm_reverse = reverse;
}