summaryrefslogtreecommitdiff
path: root/digital/mimot/src/asserv/models.host.c
blob: bdcba555b00c31c434964b126b6e2bbf54bc5976 (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
/* models.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.
 *
 * }}} */
#define _GNU_SOURCE 1 /* Need ISO C99 features as well. */
#include "common.h"

#include "modules/utils/utils.h"

#include "io.h"

#include "models.host.h"
#include "simu.host.h"

#include "aux_traj.h"

#include AC_ASSERV_CONTACTS_H

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

#define NO_CORNER { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }

static int
simu_table_test_guybrush (double p_x, double p_y);

static int
simu_table_test_apbirthday (double p_x, double p_y);

/* Guybrush, APBTeam 2012. */
static const struct robot_t guybrush_robot =
{
    /* Main motors. */
    &motor_model_def_faulhaber_2657_x9_7,
    /* Motors voltage (V). */
    24.0,
    /* Number of steps on the main motors encoders. */
    2500,
    /* Wheel radius (m). */
    0.065 / 2,
    /* Distance between the wheels (m). */
    0.16,
    /* Weight of the robot (kg). */
    10.0,
    /* Distance of the gravity center from the center of motors axis (m). */
    0.0,
    /* Whether the encoder is mounted on the main motor (false) or not (true). */
    1,
    /** Encoder wheel radius (m). */
    0.063 / 2,
    /** Distance between the encoders wheels (m). */
    0.28,
    /** No auxiliary motors. */
    { }, { }, { },
    /** Sensor update function. */
    NULL,
    /** Table test function, return false if given robot point is not in
     * table. */
    simu_table_test_guybrush,
    /** Robot corners, from front left, then clockwise. */
    { { 150, 171.5 }, { 150, -171.5 }, { -135, -121.5 }, { -135, 121.5 },
	{ 150, 0 } },
    /** Initialisation function. */
    NULL,
};

/* APBirthday, APBTeam 2013. TODO: update base dimensions. */
static const struct robot_t apbirthday_robot =
{
    /* Main motors. */
    &motor_model_def_faulhaber_2657_x9_7,
    /* Motors voltage (V). */
    24.0,
    /* Number of steps on the main motors encoders. */
    2500,
    /* Wheel radius (m). */
    0.065 / 2,
    /* Distance between the wheels (m). */
    0.16,
    /* Weight of the robot (kg). */
    10.0,
    /* Distance of the gravity center from the center of motors axis (m). */
    0.0,
    /* Whether the encoder is mounted on the main motor (false) or not (true). */
    1,
    /** Encoder wheel radius (m). */
    0.063 / 2,
    /** Distance between the encoders wheels (m). */
    0.28,
    /** No auxiliary motors. */
    { }, { }, { },
    /** Sensor update function. */
    NULL,
    /** Table test function, return false if given robot point is not in
     * table. */
    simu_table_test_apbirthday,
    /** Robot corners, from front left, then clockwise. */
    { { 102, 140 }, { 102, -140 }, { -108, -140 }, { -108, 70 },
	{ -58, 140 } },
    /** Initialisation function. */
    NULL,
};

/* Table of models. */
static const struct
{
    const char *name;
    const struct robot_t *robot;
} models[] = {
      { "guybrush", &guybrush_robot },
      { "apbirthday", &apbirthday_robot },
      { 0, 0 }
};

/** Get a pointer to a model by name, or return 0. */
const struct robot_t *
models_get (const char *name)
{
    int i;
    for (i = 0; models[i].name; i++)
      {
	if (strcmp (models[i].name, name) == 0)
	    return models[i].robot;
      }
    return 0;
}

/** Initialise simulation models. */
void
models_init (const struct robot_t *robot, motor_model_t *main_motor_left,
	     motor_model_t *main_motor_right, motor_model_t aux_motor[])
{
    int i;
    if (main_motor_left)
      {
	main_motor_left->m = *robot->main_motor;
	main_motor_left->m.J = robot->weight * robot->wheel_r
	    * robot->wheel_r / 2;
	main_motor_left->h = ECHANT_PERIOD;
	main_motor_left->d = 1000;
      }
    if (main_motor_right)
      {
	main_motor_right->m = *robot->main_motor;
	main_motor_right->m.J = robot->weight * robot->wheel_r
	    * robot->wheel_r / 2;
	main_motor_right->h = ECHANT_PERIOD;
	main_motor_right->d = 1000;
      }
    if (aux_motor)
      {
	for (i = 0; i < AC_ASSERV_AUX_NB; i++)
	  {
	    if (robot->aux_motor[i])
	      {
		aux_motor[i].m = *robot->aux_motor[i];
		aux_motor[i].m.J = robot->aux_load[i];
		aux_motor[i].h = ECHANT_PERIOD;
		aux_motor[i].d = 1000;
	      }
	  }
      }
    if (robot->init)
	robot->init (robot, main_motor_left, main_motor_right, aux_motor);
}

/* Table test for Guybrush. */
int
simu_table_test_guybrush (double p_x, double p_y)
{
    static const double table_width = 3000.0, table_height = 2000.0;
    double x, y;
    simu_compute_absolute_position (p_x, p_y, &x, &y);
    if (x < 0 || y < 0 || x >= table_width || y >= table_height)
	return 0;
    /* Totems. */
    if (((x >= 1500 - 400 - 125 && x < 1500 - 400 + 125)
	 || (x >= 1500 + 400 - 125 && x < 1500 + 400 + 125))
	&& (y >= 1000 - 125 && y < 1000 + 125))
	return 0;
    /* Boats. */
    double bx = 325 + (400 - 325) * y / 1500. + 22;
    if (((x >= bx - 40 && x < bx) || (x >= 3000 - bx && x < 3000 - bx + 40))
	&& y < 740)
	return 0;
    return 1;
}

/* Table test for APBirthday. */
int
simu_table_test_apbirthday (double p_x, double p_y)
{
    static const double table_width = 3000.0, table_height = 2000.0;
    double x, y;
    simu_compute_absolute_position (p_x, p_y, &x, &y);
    if (x < 0 || y < 0 || x >= table_width || y >= table_height)
	return 0;
    /* Sideboards. */
    if ((x < 400 || x >= table_width - 400)
	&& (y < 100 || y >= table_height - 100))
	return 0;
    /* Cake. */
    double cake_dx = table_width / 2 - x;
    double cake_dy = table_height - y;
    if (cake_dx * cake_dx + cake_dy * cake_dy < 500 * 500)
	return 0;
    return 1;
}