2014 Eurobot fork

Dependencies:   mbed-rtos mbed QEI

Committer:
rsavitski
Date:
Wed Apr 10 22:30:09 2013 +0000
Revision:
38:c9058a401410
Parent:
37:6ecf0d21e492
Child:
39:44d3dea4adcc
moving to waypoints reliably

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 24:50805ef8c499 4 // Takes current state of the robot and target waypoint,
rsavitski 24:50805ef8c499 5 // calculates desired forward and angular velocities and requests those from the motor control layer.
rsavitski 24:50805ef8c499 6 ////////////////////////////////////////////////////////////////////////////////
rsavitski 24:50805ef8c499 7
rsavitski 24:50805ef8c499 8 #include "motion.h"
rsavitski 38:c9058a401410 9
rsavitski 24:50805ef8c499 10 namespace motion
rsavitski 24:50805ef8c499 11 {
rsavitski 24:50805ef8c499 12
rsavitski 24:50805ef8c499 13 void motionlayer(void const *dummy)
rsavitski 30:791739422122 14 {
rsavitski 24:50805ef8c499 15 // get target waypoint from AI
rsavitski 24:50805ef8c499 16 Waypoint target_waypoint = *AI::current_waypoint;
rsavitski 24:50805ef8c499 17
rsavitski 24:50805ef8c499 18 // get current state from Kalman
rsavitski 24:50805ef8c499 19 State current_state = Kalman::getState();
rsavitski 24:50805ef8c499 20
rsavitski 24:50805ef8c499 21 float delta_x = target_waypoint.x - current_state.x;
rsavitski 24:50805ef8c499 22 float delta_y = target_waypoint.y - current_state.y;
rsavitski 24:50805ef8c499 23
madcowswe 25:b16f1045108f 24 //printf("motion sees deltax: %f deltay %f\r\n", delta_x, delta_y);
rsavitski 24:50805ef8c499 25
madcowswe 25:b16f1045108f 26 float distance_err = hypot(delta_x, delta_y);
madcowswe 25:b16f1045108f 27
madcowswe 25:b16f1045108f 28 float angle_err = constrainAngle(atan2(delta_y, delta_x) - current_state.theta);
rsavitski 24:50805ef8c499 29
rsavitski 37:6ecf0d21e492 30 bool d_reached = false;
rsavitski 37:6ecf0d21e492 31 bool a_reached = false;
rsavitski 37:6ecf0d21e492 32
rsavitski 30:791739422122 33 // is the waypoint reached
rsavitski 30:791739422122 34 if (distance_err < target_waypoint.pos_threshold)
rsavitski 30:791739422122 35 {
rsavitski 37:6ecf0d21e492 36 d_reached = true;
rsavitski 30:791739422122 37 distance_err = 0;
rsavitski 38:c9058a401410 38
rsavitski 38:c9058a401410 39 angle_err = 0.2*constrainAngle(target_waypoint.theta - current_state.theta);
rsavitski 38:c9058a401410 40
rsavitski 38:c9058a401410 41 if (abs(angle_err) < target_waypoint.angle_threshold)
madcowswe 33:a49197572737 42 {
rsavitski 37:6ecf0d21e492 43 a_reached = true;
madcowswe 33:a49197572737 44 angle_err = 0;
madcowswe 33:a49197572737 45 }
rsavitski 35:f8e7f0a72a3d 46 }
rsavitski 30:791739422122 47
rsavitski 30:791739422122 48 AI::waypoint_flag_mutex.lock(); // 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 37:6ecf0d21e492 49 if (d_reached && a_reached)
rsavitski 30:791739422122 50 {
rsavitski 30:791739422122 51 AI::setWaypointReached();
rsavitski 30:791739422122 52 }
rsavitski 30:791739422122 53 AI::waypoint_flag_mutex.unlock();
rsavitski 24:50805ef8c499 54
rsavitski 24:50805ef8c499 55 // angular velocity controller
madcowswe 34:e1678450feec 56 const float p_gain_av = 0.5; //TODO: tune
rsavitski 24:50805ef8c499 57
madcowswe 33:a49197572737 58 const float max_av = 0.5*PI; // radians per sec //TODO: tune
rsavitski 24:50805ef8c499 59
rsavitski 24:50805ef8c499 60 // angle error [-pi, pi]
rsavitski 24:50805ef8c499 61 float angular_v = p_gain_av * angle_err;
rsavitski 24:50805ef8c499 62
rsavitski 24:50805ef8c499 63 // constrain range
rsavitski 24:50805ef8c499 64 if (angular_v > max_av)
rsavitski 24:50805ef8c499 65 angular_v = max_av;
rsavitski 24:50805ef8c499 66 else if (angular_v < -max_av)
rsavitski 24:50805ef8c499 67 angular_v = -max_av;
rsavitski 24:50805ef8c499 68
rsavitski 24:50805ef8c499 69
rsavitski 24:50805ef8c499 70 // forward velocity controller
madcowswe 34:e1678450feec 71 const float p_gain_fv = 0.5; //TODO: tune
rsavitski 24:50805ef8c499 72
madcowswe 25:b16f1045108f 73 float max_fv = 0.2; // meters per sec //TODO: tune
rsavitski 24:50805ef8c499 74 const float angle_envelope_exponent = 8.0; //TODO: tune
rsavitski 24:50805ef8c499 75
rsavitski 24:50805ef8c499 76 // control, distance_err in meters
rsavitski 24:50805ef8c499 77 float forward_v = p_gain_fv * distance_err;
rsavitski 24:50805ef8c499 78
rsavitski 24:50805ef8c499 79 // control the forward velocity envelope based on angular error
rsavitski 24:50805ef8c499 80 max_fv = max_fv * pow(cos(angle_err/2), angle_envelope_exponent);
rsavitski 24:50805ef8c499 81
rsavitski 24:50805ef8c499 82 // constrain range
rsavitski 24:50805ef8c499 83 if (forward_v > max_fv)
rsavitski 24:50805ef8c499 84 forward_v = max_fv;
rsavitski 24:50805ef8c499 85 else if (forward_v < -max_fv)
rsavitski 24:50805ef8c499 86 forward_v = -max_fv;
madcowswe 25:b16f1045108f 87
madcowswe 25:b16f1045108f 88 //printf("fwd: %f, omega: %f\r\n", forward_v, angular_v);
rsavitski 24:50805ef8c499 89
rsavitski 30:791739422122 90 // pass values to the motor control
rsavitski 24:50805ef8c499 91 MotorControl::set_fwdcmd(forward_v);
madcowswe 25:b16f1045108f 92 MotorControl::set_omegacmd(angular_v);
rsavitski 24:50805ef8c499 93 }
rsavitski 24:50805ef8c499 94
rsavitski 24:50805ef8c499 95 } //namespace