2014 Eurobot fork

Dependencies:   mbed-rtos mbed QEI

Committer:
rsavitski
Date:
Sun Apr 14 20:23:07 2013 +0000
Revision:
69:4b7bb92916da
Parent:
67:be3ea5450cc7
Child:
70:0da6ca845762
test ai aux hack

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rsavitski 24:50805ef8c499 1 ////////////////////////////////////////////////////////////////////////////////
rsavitski 24:50805ef8c499 2 // Motion control unit
rsavitski 24:50805ef8c499 3 ////////////////////////////////////////////////////////////////////////////////
rsavitski 69:4b7bb92916da 4 // Takes current state and motion command (set via accessors) and actuates it via motor layer calls
rsavitski 24:50805ef8c499 5 ////////////////////////////////////////////////////////////////////////////////
rsavitski 24:50805ef8c499 6
rsavitski 24:50805ef8c499 7 #include "motion.h"
rsavitski 57:d434ceab6892 8 #include "math.h"
rsavitski 57:d434ceab6892 9 #include "Kalman.h"
rsavitski 57:d434ceab6892 10 #include "MotorControl.h"
rsavitski 57:d434ceab6892 11 #include "supportfuncs.h"
madcowswe 66:f1d75e51398d 12 #include "AvoidDstSensor.h"
rsavitski 57:d434ceab6892 13
rsavitski 57:d434ceab6892 14 namespace motion
rsavitski 57:d434ceab6892 15 {
rsavitski 57:d434ceab6892 16 // private function prototypes
rsavitski 57:d434ceab6892 17
rsavitski 57:d434ceab6892 18 void setWaypointReached();
rsavitski 57:d434ceab6892 19 void clearMotionCmd();
rsavitski 57:d434ceab6892 20 void clearWaypoint();
rsavitski 57:d434ceab6892 21 }
rsavitski 38:c9058a401410 22
rsavitski 24:50805ef8c499 23 namespace motion
rsavitski 24:50805ef8c499 24 {
rsavitski 24:50805ef8c499 25
madcowswe 66:f1d75e51398d 26 volatile int collavoiden = 0;
madcowswe 66:f1d75e51398d 27 AvoidDstSensor ADS(P_FWD_DISTANCE_SENSOR);
madcowswe 66:f1d75e51398d 28
rsavitski 57:d434ceab6892 29 // motion commands supported
rsavitski 57:d434ceab6892 30 enum motion_type_t { motion_waypoint };
rsavitski 57:d434ceab6892 31
rsavitski 57:d434ceab6892 32 struct motion_cmd
rsavitski 57:d434ceab6892 33 {
rsavitski 57:d434ceab6892 34 motion_type_t motion_type;
rsavitski 57:d434ceab6892 35 osThreadId setter_tid;
rsavitski 57:d434ceab6892 36
rsavitski 57:d434ceab6892 37 bool motion_done;
rsavitski 57:d434ceab6892 38
rsavitski 57:d434ceab6892 39 union
rsavitski 57:d434ceab6892 40 {
rsavitski 57:d434ceab6892 41 Waypoint *wp_ptr;
rsavitski 57:d434ceab6892 42 };
rsavitski 57:d434ceab6892 43
rsavitski 57:d434ceab6892 44 };
rsavitski 57:d434ceab6892 45
rsavitski 57:d434ceab6892 46 // local copy of the current active motion
rsavitski 57:d434ceab6892 47 motion_cmd current_motion;
rsavitski 57:d434ceab6892 48
rsavitski 39:44d3dea4adcc 49 Mutex waypoint_flag_mutex;
rsavitski 39:44d3dea4adcc 50
rsavitski 57:d434ceab6892 51 // local to waypoint motion handler
rsavitski 57:d434ceab6892 52 bool wp_d_reached = false;
rsavitski 57:d434ceab6892 53 bool wp_a_reached = false;
rsavitski 39:44d3dea4adcc 54
rsavitski 39:44d3dea4adcc 55
rsavitski 24:50805ef8c499 56 void motionlayer(void const *dummy)
rsavitski 57:d434ceab6892 57 {
rsavitski 57:d434ceab6892 58 switch (current_motion.motion_type)
rsavitski 57:d434ceab6892 59 {
rsavitski 57:d434ceab6892 60 case motion_waypoint:
rsavitski 57:d434ceab6892 61 waypoint_motion_handler();
rsavitski 57:d434ceab6892 62 break;
rsavitski 57:d434ceab6892 63 default:
rsavitski 57:d434ceab6892 64 break;
rsavitski 57:d434ceab6892 65 }
rsavitski 57:d434ceab6892 66 }
rsavitski 57:d434ceab6892 67
rsavitski 57:d434ceab6892 68 void waypoint_motion_handler()
rsavitski 57:d434ceab6892 69 {
rsavitski 39:44d3dea4adcc 70 // save target waypoint
rsavitski 57:d434ceab6892 71 Waypoint target_waypoint = *current_motion.wp_ptr;
rsavitski 24:50805ef8c499 72
rsavitski 24:50805ef8c499 73 // get current state from Kalman
rsavitski 24:50805ef8c499 74 State current_state = Kalman::getState();
rsavitski 24:50805ef8c499 75
rsavitski 24:50805ef8c499 76 float delta_x = target_waypoint.x - current_state.x;
rsavitski 24:50805ef8c499 77 float delta_y = target_waypoint.y - current_state.y;
rsavitski 24:50805ef8c499 78
madcowswe 25:b16f1045108f 79 //printf("motion sees deltax: %f deltay %f\r\n", delta_x, delta_y);
rsavitski 24:50805ef8c499 80
madcowswe 25:b16f1045108f 81 float distance_err = hypot(delta_x, delta_y);
madcowswe 25:b16f1045108f 82
madcowswe 25:b16f1045108f 83 float angle_err = constrainAngle(atan2(delta_y, delta_x) - current_state.theta);
rsavitski 24:50805ef8c499 84
rsavitski 57:d434ceab6892 85 // reversing if close to target and more optimal than forward movement
rsavitski 52:bffe5f7c39a3 86 bool reversing = false;
rsavitski 57:d434ceab6892 87 if ((abs(angle_err) > PI/2) && (distance_err < 0.2)) //TODO: parameterise and tune 0.2 meters hardcoded
rsavitski 52:bffe5f7c39a3 88 {
rsavitski 52:bffe5f7c39a3 89 reversing = true;
rsavitski 52:bffe5f7c39a3 90 angle_err = constrainAngle(angle_err + PI);
rsavitski 52:bffe5f7c39a3 91 distance_err = -distance_err;
rsavitski 52:bffe5f7c39a3 92 }
rsavitski 52:bffe5f7c39a3 93
rsavitski 52:bffe5f7c39a3 94 float angle_err_saved = angle_err; // actuated angle error can be overriden by the final turning code, but forward speed envelope should be controlled with the atan angle
rsavitski 52:bffe5f7c39a3 95
rsavitski 30:791739422122 96 // is the waypoint reached
rsavitski 57:d434ceab6892 97 waypoint_flag_mutex.lock(); //TODO: consider refactoring
rsavitski 57:d434ceab6892 98 if (abs(distance_err) < ((wp_d_reached) ? target_waypoint.pos_threshold+0.02 : target_waypoint.pos_threshold))
rsavitski 30:791739422122 99 {
rsavitski 57:d434ceab6892 100 wp_d_reached = true;
rsavitski 38:c9058a401410 101
rsavitski 52:bffe5f7c39a3 102 angle_err = constrainAngle(target_waypoint.theta - current_state.theta);
rsavitski 38:c9058a401410 103
rsavitski 38:c9058a401410 104 if (abs(angle_err) < target_waypoint.angle_threshold)
madcowswe 33:a49197572737 105 {
rsavitski 57:d434ceab6892 106 wp_a_reached = true;
madcowswe 33:a49197572737 107 }
rsavitski 35:f8e7f0a72a3d 108 }
rsavitski 65:4709ff6c753c 109 // proper way would be to construct a function to evaluate the condition and pass the function pointer to a conditional setter function for reached flag
rsavitski 57:d434ceab6892 110 if (wp_d_reached && wp_a_reached)
rsavitski 30:791739422122 111 {
rsavitski 39:44d3dea4adcc 112 setWaypointReached();
rsavitski 30:791739422122 113 }
rsavitski 39:44d3dea4adcc 114 waypoint_flag_mutex.unlock();
rsavitski 24:50805ef8c499 115
rsavitski 24:50805ef8c499 116 // angular velocity controller
madcowswe 67:be3ea5450cc7 117 const float p_gain_av = 0.7; //TODO: tune
rsavitski 24:50805ef8c499 118
madcowswe 64:c979fb1cd3b5 119 const float max_av = 0.5; // radians per sec //TODO: tune
rsavitski 24:50805ef8c499 120
rsavitski 24:50805ef8c499 121 // angle error [-pi, pi]
rsavitski 24:50805ef8c499 122 float angular_v = p_gain_av * angle_err;
rsavitski 24:50805ef8c499 123
rsavitski 24:50805ef8c499 124 // constrain range
rsavitski 24:50805ef8c499 125 if (angular_v > max_av)
rsavitski 24:50805ef8c499 126 angular_v = max_av;
rsavitski 24:50805ef8c499 127 else if (angular_v < -max_av)
rsavitski 24:50805ef8c499 128 angular_v = -max_av;
rsavitski 24:50805ef8c499 129
rsavitski 24:50805ef8c499 130
rsavitski 24:50805ef8c499 131 // forward velocity controller
madcowswe 67:be3ea5450cc7 132 const float p_gain_fv = 0.7; //TODO: tune
rsavitski 24:50805ef8c499 133
madcowswe 25:b16f1045108f 134 float max_fv = 0.2; // meters per sec //TODO: tune
rsavitski 52:bffe5f7c39a3 135 float max_fv_reverse = 0.05; //TODO: tune
madcowswe 67:be3ea5450cc7 136 const float angle_envelope_exponent = 32;//512; //8.0; //TODO: tune
rsavitski 24:50805ef8c499 137
rsavitski 24:50805ef8c499 138 // control, distance_err in meters
rsavitski 24:50805ef8c499 139 float forward_v = p_gain_fv * distance_err;
rsavitski 24:50805ef8c499 140
rsavitski 52:bffe5f7c39a3 141 // if reversing, robot is less stable hence a different cap is used
rsavitski 52:bffe5f7c39a3 142 if (reversing)
rsavitski 52:bffe5f7c39a3 143 max_fv = max_fv_reverse;
rsavitski 52:bffe5f7c39a3 144
rsavitski 24:50805ef8c499 145 // control the forward velocity envelope based on angular error
rsavitski 52:bffe5f7c39a3 146 max_fv = max_fv * pow(cos(angle_err_saved/2), angle_envelope_exponent);
rsavitski 24:50805ef8c499 147
rsavitski 24:50805ef8c499 148 // constrain range
rsavitski 24:50805ef8c499 149 if (forward_v > max_fv)
rsavitski 24:50805ef8c499 150 forward_v = max_fv;
rsavitski 24:50805ef8c499 151 else if (forward_v < -max_fv)
rsavitski 24:50805ef8c499 152 forward_v = -max_fv;
madcowswe 25:b16f1045108f 153
madcowswe 25:b16f1045108f 154 //printf("fwd: %f, omega: %f\r\n", forward_v, angular_v);
rsavitski 24:50805ef8c499 155
rsavitski 69:4b7bb92916da 156 //TODO: remove oskar's avoidance hack
rsavitski 69:4b7bb92916da 157 if(collavoiden)
rsavitski 69:4b7bb92916da 158 {
madcowswe 66:f1d75e51398d 159 float d = ADS.Distanceincm();
rsavitski 69:4b7bb92916da 160 if(d > 10)
rsavitski 69:4b7bb92916da 161 {
madcowswe 66:f1d75e51398d 162 forward_v *= max(min((d-15)*(1.0f/20.0f),1.0f),-0.1f);
madcowswe 66:f1d75e51398d 163 }
madcowswe 66:f1d75e51398d 164 }
rsavitski 69:4b7bb92916da 165 // end of Oskar hack
madcowswe 66:f1d75e51398d 166
rsavitski 30:791739422122 167 // pass values to the motor control
rsavitski 24:50805ef8c499 168 MotorControl::set_fwdcmd(forward_v);
madcowswe 25:b16f1045108f 169 MotorControl::set_omegacmd(angular_v);
rsavitski 24:50805ef8c499 170 }
rsavitski 24:50805ef8c499 171
rsavitski 57:d434ceab6892 172
rsavitski 57:d434ceab6892 173 bool checkMotionStatus()
rsavitski 57:d434ceab6892 174 {
rsavitski 57:d434ceab6892 175 return current_motion.motion_done;
rsavitski 57:d434ceab6892 176 }
rsavitski 57:d434ceab6892 177
rsavitski 57:d434ceab6892 178
rsavitski 57:d434ceab6892 179 void setNewWaypoint(osThreadId setter_tid_in, Waypoint *new_wp)
rsavitski 39:44d3dea4adcc 180 {
rsavitski 57:d434ceab6892 181 clearMotionCmd();
rsavitski 57:d434ceab6892 182
rsavitski 57:d434ceab6892 183 current_motion.setter_tid = setter_tid_in;
rsavitski 57:d434ceab6892 184 current_motion.motion_type = motion_waypoint;
rsavitski 57:d434ceab6892 185
rsavitski 57:d434ceab6892 186 current_motion.wp_ptr = new_wp;
rsavitski 39:44d3dea4adcc 187 }
rsavitski 39:44d3dea4adcc 188
rsavitski 57:d434ceab6892 189
rsavitski 39:44d3dea4adcc 190 void setWaypointReached()
rsavitski 39:44d3dea4adcc 191 {
rsavitski 57:d434ceab6892 192 current_motion.motion_done = true;
rsavitski 57:d434ceab6892 193 osSignalSet(current_motion.setter_tid,0x1);
rsavitski 39:44d3dea4adcc 194 }
rsavitski 39:44d3dea4adcc 195
rsavitski 57:d434ceab6892 196
rsavitski 57:d434ceab6892 197 void clearMotionCmd()
rsavitski 39:44d3dea4adcc 198 {
rsavitski 57:d434ceab6892 199 current_motion.motion_done = false;
rsavitski 57:d434ceab6892 200 osSignalClear(current_motion.setter_tid, 0x1);
rsavitski 57:d434ceab6892 201
rsavitski 57:d434ceab6892 202 switch (current_motion.motion_type)
rsavitski 57:d434ceab6892 203 {
rsavitski 57:d434ceab6892 204 case motion_waypoint:
rsavitski 57:d434ceab6892 205 clearWaypoint();
rsavitski 57:d434ceab6892 206 break;
rsavitski 57:d434ceab6892 207
rsavitski 57:d434ceab6892 208 default:
rsavitski 57:d434ceab6892 209 break;
rsavitski 57:d434ceab6892 210 }
rsavitski 39:44d3dea4adcc 211 }
rsavitski 39:44d3dea4adcc 212
rsavitski 57:d434ceab6892 213
rsavitski 57:d434ceab6892 214 void clearWaypoint()
rsavitski 39:44d3dea4adcc 215 {
rsavitski 57:d434ceab6892 216 wp_d_reached = false;
rsavitski 57:d434ceab6892 217 wp_a_reached = false;
rsavitski 39:44d3dea4adcc 218 }
rsavitski 39:44d3dea4adcc 219
rsavitski 24:50805ef8c499 220 } //namespace