summaryrefslogtreecommitdiffhomepage
path: root/digital/beacon/src/formula.c
blob: f58c1b8be86305a262c1d8c01981bb795f9bfbb4 (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
/* formula.c */
/* Beacon triangulation algorithms. {{{
 *
 * Copyright (C) 2011 Florent Duchon
 *
 * 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 <math.h>
#include "debug_simu.h"
#include "position.h"
#include "formula.h"
#include "misc.h"

/* This function computes coords using formula 3 mode ie for beacon 1 + 2*/
TFormulaStatus formula3_compute_coord(coord_s * position, float angle_beacon1, float angle_beacon2)
{
 	DEBUG_FORMULA_INFO("Formula 3 : angle_beacon1 = %f angle_beacon2 = %f\n",angle_beacon1,angle_beacon2);
	if((angle_beacon1 < BLIND_ZONE_BEACON1_ANGLE12_MAX) && (angle_beacon2 < BLIND_ZONE_BEACON2_ANGLE12_MAX))
	{
		DEBUG_FORMULA_ERROR("BLIND Zone detected\n");
		return FORMULA_BLIND_ZONE_DETECTED;
	}
	else
	{
		position->x = LARGEUR_TABLE * tan(angle_beacon2) * tan(angle_beacon1) / (tan(angle_beacon2)+tan(angle_beacon1));
 		position->y = LARGEUR_TABLE * tan(angle_beacon1) / (tan(angle_beacon2)+tan(angle_beacon1));
		
		if( (position->x < 0) || (position->x > LONGUEUR_TABLE) || (position->y < 0) || (position->y > LARGEUR_TABLE))
		{
			DEBUG_FORMULA_ERROR("OUT of the Table\n");
			return FORMULA_OUT_OF_TABLE;
		}
		else
		{
			DEBUG_FORMULA_INFO("Formula 3 : X = %d  Y = %d\n",position->x,position->y);
			return FORMULA_VALID_POSITION;
		}
	}
}

/* This function computes coords using formula 4 mode ie for beacon 1 + 3*/
TFormulaStatus formula4_compute_coord(coord_s * position, float angle_beacon1, float angle_beacon3)
{
 	DEBUG_FORMULA_INFO("Formula 4 : angle_beacon1 = %f angle_beacon3 = %f\n",angle_beacon1,angle_beacon3);

	if(angle_beacon3 > M_PI/2)
	{
		if((angle_beacon1 > BLIND_ZONE_BEACON1_ANGLE13_MIN) &&  (angle_beacon1 < BLIND_ZONE_BEACON1_ANGLE13_MAX) && ((M_PI - angle_beacon3) > BLIND_ZONE_BEACON3_ANGLE13_MIN) && ((M_PI - angle_beacon3) < BLIND_ZONE_BEACON3_ANGLE13_MAX))
		{
			DEBUG_FORMULA_ERROR("BLIND Zone detected\n");
			return FORMULA_BLIND_ZONE_DETECTED;
		}
		position->y = (LARGEUR_DEMI_TABLE*tan(M_PI - angle_beacon3) - LARGEUR_TABLE*tan(angle_beacon1) + LONGUEUR_TABLE) / (tan(M_PI - angle_beacon3) - tan(angle_beacon1));
	}
	else
	{
		position->y = (LARGEUR_DEMI_TABLE*tan(angle_beacon3) + LARGEUR_TABLE*tan(angle_beacon1)-LONGUEUR_TABLE) / (tan(angle_beacon1) + tan(angle_beacon3));
	}
	position->x = (LARGEUR_TABLE - position->y)*tan(angle_beacon1);
	
	if( (position->x < 0) || (position->x > LONGUEUR_TABLE) || (position->y < 0) || (position->y > LARGEUR_TABLE))
	{
		DEBUG_FORMULA_ERROR("OUT of the Table\n");
		return FORMULA_OUT_OF_TABLE;
	}
	else
	{
		DEBUG_FORMULA_INFO("Formula 4 : X = %d  Y = %d\n",position->x,position->y);
		return FORMULA_VALID_POSITION;
	}
}

/* This function computes coords using formula 5 mode ie for beacon 2 + 3*/
TFormulaStatus formula5_compute_coord(coord_s * position, float angle_beacon2, float angle_beacon3)
{
	DEBUG_FORMULA_INFO("Formula 5 : angle_beacon2 = %f angle_beacon3 = %f\n",angle_beacon2,angle_beacon3);
	
	if(angle_beacon3 > M_PI/2)
	{
		position->y = (LONGUEUR_TABLE + LARGEUR_DEMI_TABLE * tan(M_PI - angle_beacon3)) / (tan(angle_beacon2) + tan(M_PI - angle_beacon3));
	}
	else
	{
		if((angle_beacon2 > BLIND_ZONE_BEACON2_ANGLE23_MIN) &&  (angle_beacon2 < BLIND_ZONE_BEACON2_ANGLE23_MAX) && (angle_beacon3 > BLIND_ZONE_BEACON3_ANGLE23_MIN) && (angle_beacon3 < BLIND_ZONE_BEACON3_ANGLE23_MAX))
		{
			DEBUG_FORMULA_ERROR("BLIND Zone detected\n");
			return FORMULA_BLIND_ZONE_DETECTED;
		}
		position->y = (LARGEUR_DEMI_TABLE*tan(angle_beacon3) - LONGUEUR_TABLE) / (tan(angle_beacon3) - tan(angle_beacon2));
	}
	position->x = tan(angle_beacon2) * position->y;
	
	if( (position->x < 0) || (position->x > LONGUEUR_TABLE) || (position->y < 0) || (position->y > LARGEUR_TABLE))
	{
		DEBUG_FORMULA_ERROR("OUT of the Table\n");
		return FORMULA_OUT_OF_TABLE;
	}
	else
	{
		DEBUG_FORMULA_INFO("Formula 5 : X = %d  Y = %d\n",position->x,position->y);
		return FORMULA_VALID_POSITION;
	}
}


/* This function computes an absolute position using 2 angles from 2 beacons */
TFormulaStatus formula_compute_position(int formula, int current_beacon_ID, float latest_angle, float current_angle, coord_s * result)
{
	int error = FORMULA_VALID_POSITION;
	switch(formula)
	{
		case 3:
			if(current_beacon_ID == 1)
			{
				error = formula3_compute_coord(result, current_angle, latest_angle);
			}
			else
			{
				error = formula3_compute_coord(result, latest_angle, current_angle);
			}
			break;
		case 4:
			if(current_beacon_ID == 1)
			{
				error = formula4_compute_coord(result, current_angle, latest_angle);
			}
			else
			{
				error = formula4_compute_coord(result, latest_angle, current_angle);
			}
			break;
		case 5:
			if(current_beacon_ID == 2)
			{
				error = formula5_compute_coord(result, current_angle,latest_angle);
			}
			else
			{
				error = formula5_compute_coord(result, latest_angle, current_angle);
			}
			break;
		default:
			DEBUG_FORMULA_ERROR("Unknown Formula = %d\r\n",formula);
			error = FORMULA_UNKNOWN_FORMULA;
	}
	return error;
}


/* This function udpates robot position from given x y */
TFormulaStatus formula_update_robot_from_xy(robot_s * robot, uint16_t x, uint16_t y)
{
	if(color_get_value() == COLOR_RIGHT)
	{
		robot->x = LONGUEUR_TABLE - x;
		robot->y = LARGEUR_TABLE - y;
	}
	else
	{
		robot->x = x;
		robot->y = y;
	}
	
	robot->angle[POV_BEACON_1] = atan(robot->x / (LARGEUR_TABLE - robot->y));
	robot->angle[POV_BEACON_2] = atan(robot->x/robot->y);
	
	if(y <= LARGEUR_DEMI_TABLE)
	{
		robot->angle[POV_BEACON_3] = atan((LONGUEUR_TABLE - robot->x) / (LARGEUR_DEMI_TABLE - robot->y));
	}
	else
	{
		robot->angle[POV_BEACON_3] = M_PI/2 + atan((robot->y - LARGEUR_DEMI_TABLE) / (LONGUEUR_TABLE-robot->x));
	}
	
	return FORMULA_VALID_POSITION;
}