MotorController
position.c
1 /******************************************************************************/
2 /* Includes */
3 /******************************************************************************/
4 #include "position.h"
5 #include "orientation.h"
6 #include "math.h"
7 #include "settings.h"
8 #include "coding_wheels.h"
9 #include "RTT/SEGGER_RTT.h"
10 
11 #include "RTT/SEGGER_RTT.h"
12 
13 #define ABS(x) (x > 0 ? x : -x)
14 
15 /******************************************************************************/
16 /* Public variables */
17 /******************************************************************************/
19 
21 
23 
24 
25 /*****************************************************************************/
26 /* Local constant */
27 /*****************************************************************************/
28 #define EPSILON (0.1f)
29 
30 
31 /******************************************************************************/
32 /* Local macros */
33 /******************************************************************************/
34 #define M_TO_MM(x) ((x) / 1000)
35 
36 /******************************************************************************/
37 /* Public functions */
38 /******************************************************************************/
39 extern void compute_movement(void)
40 {
41  delta_ticks.left = left_ticks - previous_ticks.left;
42  previous_ticks.left = left_ticks;
43 
44  delta_ticks.right = right_ticks - previous_ticks.right;
45  previous_ticks.right = right_ticks;
46 }
47 
48 extern void update_position(void)
49 {
50  float d; /* Distance travelled by the robot center, in ticks */
51  float delta_alpha; /* Orientation variation, in radian */
52  float R = 0.0f; /* Radius, in ticks */
53 
54  position_t O; /* coordinates of local circle center, in mm */
55 
56  float orientation_f = (float)orientation / ANGLE_MULT_RAD;
57 
58  /**
59  * Coding wheels ticks, at previous cur_pos update.
60  * A separate variable is used because cur_pos update frequency can be different
61  * from the compute_movement one.
62  */
63  static ticks_t cur_pos_previous_ticks = {0, 0};
64 
65  /* Coding wheels ticks variation since last cur_pos update (last call to this function) */
66  ticks_t cur_pos_delta_ticks;
67 
68  cur_pos_delta_ticks.right = right_ticks - cur_pos_previous_ticks.right;
69  cur_pos_delta_ticks.left = left_ticks - cur_pos_previous_ticks.left;
70 
71  cur_pos_previous_ticks.right = right_ticks;
72  cur_pos_previous_ticks.left = left_ticks;
73 
74  d = (cur_pos_delta_ticks.right + cur_pos_delta_ticks.left) / 2.0f;
75  delta_alpha = (cur_pos_delta_ticks.left - cur_pos_delta_ticks.right) * 1000.0f / (settings.wheels_gap * settings.ticks_per_m);
76 
77  if (fabs((double) delta_alpha) > EPSILON){ // curve
78 
79  R = d / (2 * delta_alpha);
80  O.x = cur_pos.x - (R / M_TO_MM(settings.ticks_per_m)) * cos(orientation_f - delta_alpha);
81  O.y = cur_pos.y - (R / M_TO_MM(settings.ticks_per_m)) * sin(orientation_f - delta_alpha);
82 
83  cur_pos.x = O.x + (R / M_TO_MM(settings.ticks_per_m)) * cos(orientation_f);
84  cur_pos.y = O.y + (R / M_TO_MM(settings.ticks_per_m)) * sin(orientation_f);
85 
86  } else { // line
87  cur_pos.x += (((d * 1000) / settings.ticks_per_m) * cos(orientation_f));
88  cur_pos.y += (((d * 1000) / settings.ticks_per_m) * sin(orientation_f));
89  }
90 }
ticks_t delta_ticks
Definition: position.c:20
ticks_t previous_ticks
Definition: position.c:18
void update_position(void)
Update the position according to the information given by the coding wheels.
Definition: position.c:48
position_t cur_pos
Definition: position.c:22
int16_t orientation
Definition: orientation.c:32
uint16_t wheels_gap
Definition: settings.h:41
uint16_t ticks_per_m
Definition: settings.h:42
void compute_movement(void)
Compute the coding wheels movements.
Definition: position.c:39
volatile robot_settings_t settings
Global variable used to store the configuration in use.
Definition: settings.c:9