Working file for communication with ROS

Dependencies:   QEI2 chair_BNO055 PID VL53L1X_Filter

Dependents:   wheelchaircontrolrealRosCom

Committer:
JesiMiranda
Date:
Wed Jun 26 04:23:39 2019 +0000
Revision:
28:674ad4175a15
Parent:
27:302d03ee1ae0
Working file for communication with ROS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jvfausto 21:3489cffad196 1
ryanlin97 0:fc0c4a184482 2 #include "wheelchair.h"
jvfausto 21:3489cffad196 3
jvfausto 21:3489cffad196 4 bool manual_drive = false; // Variable changes between joystick and auto drive
jvfausto 21:3489cffad196 5 double encoder_distance; // Keeps distanse due to original position
jvfausto 21:3489cffad196 6
jvfausto 21:3489cffad196 7 volatile double Setpoint, Output, Input, Input2; // Variables for PID
jvfausto 21:3489cffad196 8 volatile double pid_yaw, Distance, Setpoint2, Output2, encoder_distance2; // Variables for PID
jvfausto 21:3489cffad196 9 volatile double vIn, vOut, vDesired; // Variables for PID Velosity
jvfausto 21:3489cffad196 10 volatile double vInS, vOutS, vDesiredS; // Variables for PID Slave Wheel
jvfausto 21:3489cffad196 11 volatile double yIn, yOut, yDesired; // Variables for PID turn velosity
ryanlin97 11:d14a1f7f1297 12
jvfausto 21:3489cffad196 13 double dist_old, curr_pos; // Variables for odometry position
jvfausto 19:71a6621ee5c3 14
jvfausto 21:3489cffad196 15
jvfausto 21:3489cffad196 16 PID myPID(&pid_yaw, &Output, &Setpoint, 5.5, .00, 0.0036, P_ON_E, DIRECT); // Angle PID object constructor
jvfausto 21:3489cffad196 17 PID myPIDDistance(&Input, &Output, &Setpoint, 5.5, .00, 0.002, P_ON_E, DIRECT); // Distance PID object constructor
jvfausto 21:3489cffad196 18 PID PIDVelosity(&vIn, &vOut, &vDesired, 5.5, .00, .002, P_ON_E, DIRECT); // Velosity PID Constructor
jvfausto 21:3489cffad196 19 PID PIDSlaveV(&vInS, &vOutS, &vDesiredS, 5.5, .00, .002, P_ON_E, DIRECT); // Slave Velosity PID Constructor
jvfausto 21:3489cffad196 20 PID PIDAngularV(&yIn, &yOut, &yDesired, 5.5, .00, .002, P_ON_E, DIRECT); // Angular Velosity PID Constructor
jvfausto 21:3489cffad196 21
jvfausto 19:71a6621ee5c3 22
jvfausto 21:3489cffad196 23 /* Thread measures current angular position */
jvfausto 21:3489cffad196 24 void Wheelchair::compass_thread()
jvfausto 21:3489cffad196 25 {
ryanlin97 11:d14a1f7f1297 26 curr_yaw = imu->yaw();
jvfausto 21:3489cffad196 27 z_angular = curr_yaw;
jvfausto 21:3489cffad196 28 }
jvfausto 17:7f3b69300bb6 29
jvfausto 21:3489cffad196 30 /* Thread measures velocity of wheels and distance traveled */
jvfausto 21:3489cffad196 31 void Wheelchair::velocity_thread()
ryanlin97 1:c0beadca1617 32 {
jvfausto 21:3489cffad196 33 curr_vel = wheel->getVelocity();
jvfausto 21:3489cffad196 34 curr_velS = wheelS->getVelocity();
jvfausto 21:3489cffad196 35 curr_pos = wheel->getDistance(53.975);
ryanlin97 1:c0beadca1617 36 }
ryanlin97 6:0cd57bdd8fbc 37
jvfausto 21:3489cffad196 38 void Wheelchair::assistSafe_thread()
ryanlin97 1:c0beadca1617 39 {
jvfausto 26:c842070aa0b8 40 /* int ToFV[12];
jvfausto 21:3489cffad196 41 for(int i = 0; i < 9; i++) // reads from the ToF Sensors
jvfausto 21:3489cffad196 42 {
jvfausto 21:3489cffad196 43 ToFV[i] = (*(ToF+i))->readFromOneSensor();
jvfausto 21:3489cffad196 44 //out->printf("%d ", ToFV[i]);
jvfausto 21:3489cffad196 45 }
jvfausto 26:c842070aa0b8 46 //out->printf("\r\n");
jvfausto 26:c842070aa0b8 47 int sensor1 = ToFV[1];
jvfausto 21:3489cffad196 48 int sensor4 = ToFV[5];
jvfausto 26:c842070aa0b8 49 //out->printf("%d, %d\r\n", sensor1, sensor4);
jvfausto 26:c842070aa0b8 50 if(curr_vel < 1 &&((2 * maxDecelerationSlow*sensor1 < curr_vel*curr_vel*1000*1000 ||
jvfausto 26:c842070aa0b8 51 2 * maxDecelerationSlow*sensor4 < curr_vel*curr_vel*1000*1000) &&
jvfausto 21:3489cffad196 52 (sensor1 < 1500 || sensor4 < 1500)) ||
jvfausto 21:3489cffad196 53 550 > sensor1 || 550 > sensor4)
jvfausto 21:3489cffad196 54 {
jvfausto 21:3489cffad196 55 //out->printf("i am in danger\r\n");
jvfausto 21:3489cffad196 56 if(x->read() > def)
jvfausto 21:3489cffad196 57 {
jvfausto 21:3489cffad196 58 x->write(def);
jvfausto 21:3489cffad196 59 forwardSafety = 1;
jvfausto 21:3489cffad196 60 }
jvfausto 21:3489cffad196 61 }
jvfausto 26:c842070aa0b8 62 else if(curr_vel > 1 &&((2 * maxDecelerationFast*sensor1 < curr_vel*curr_vel*1000*1000 ||
jvfausto 26:c842070aa0b8 63 2 * maxDecelerationFast*sensor4 < curr_vel*curr_vel*1000*1000) &&
jvfausto 26:c842070aa0b8 64 (sensor1 < 1500 || sensor4 < 1500)) ||
jvfausto 26:c842070aa0b8 65 550 > sensor1 || 550 > sensor4)
jvfausto 26:c842070aa0b8 66 {
jvfausto 26:c842070aa0b8 67 //out->printf("i am in danger\r\n");
jvfausto 26:c842070aa0b8 68 if(x->read() > def)
jvfausto 26:c842070aa0b8 69 {
jvfausto 26:c842070aa0b8 70 x->write(def);
jvfausto 26:c842070aa0b8 71 forwardSafety = 1;
jvfausto 26:c842070aa0b8 72 }
jvfausto 26:c842070aa0b8 73 }
jvfausto 26:c842070aa0b8 74 else*/
jvfausto 21:3489cffad196 75 forwardSafety = 0;
jvfausto 21:3489cffad196 76
jvfausto 21:3489cffad196 77 }
ryanlin97 6:0cd57bdd8fbc 78
jvfausto 21:3489cffad196 79 /* Constructor for Wheelchair class */
jvfausto 21:3489cffad196 80 Wheelchair::Wheelchair(PinName xPin, PinName yPin, Serial* pc, Timer* time, QEI* qei, QEI* qeiS,
jvfausto 21:3489cffad196 81 VL53L1X** ToFT)
jvfausto 21:3489cffad196 82 {
jvfausto 21:3489cffad196 83 x_position = 0;
jvfausto 21:3489cffad196 84 y_position = 0;
jvfausto 21:3489cffad196 85 forwardSafety = 0;
jvfausto 21:3489cffad196 86 /* Initializes X and Y variables to Pins */
jvfausto 21:3489cffad196 87 x = new PwmOut(xPin);
jvfausto 21:3489cffad196 88 y = new PwmOut(yPin);
jvfausto 21:3489cffad196 89 /* Initializes IMU Library */
jvfausto 21:3489cffad196 90 imu = new chair_BNO055(pc, time);
jvfausto 21:3489cffad196 91 Wheelchair::stop(); // Wheelchair is initially stationary
jvfausto 21:3489cffad196 92 imu->setup(); // turns on the IMU
jvfausto 21:3489cffad196 93 out = pc; // "out" is called for serial monitor
jvfausto 21:3489cffad196 94 wheelS = qeiS; // "wheel" is called for encoder
jvfausto 21:3489cffad196 95 wheel = qei;
jvfausto 21:3489cffad196 96 ToF = ToFT; // passes pointer with addresses of ToF sensors
jvfausto 21:3489cffad196 97
jvfausto 21:3489cffad196 98 for(int i = 0; i < 12; i++) // initializes the ToF Sensors
jvfausto 21:3489cffad196 99 {
jvfausto 26:c842070aa0b8 100 //(*(ToF+i))->initReading(0x31+((0x02)*i), 50000);
jvfausto 21:3489cffad196 101 }
jvfausto 21:3489cffad196 102
jvfausto 21:3489cffad196 103 out->printf("wheelchair setup done \r\n"); // Make sure it initialized; prints in serial monitor
jvfausto 21:3489cffad196 104 ti = time;
jvfausto 21:3489cffad196 105
jvfausto 21:3489cffad196 106
jvfausto 21:3489cffad196 107 myPID.SetMode(AUTOMATIC); // PID mode: Automatic
jvfausto 21:3489cffad196 108 }
jvfausto 21:3489cffad196 109
jvfausto 21:3489cffad196 110 /* Move wheelchair with joystick on manual mode */
jvfausto 21:3489cffad196 111 void Wheelchair::move(float x_coor, float y_coor)
jvfausto 21:3489cffad196 112 {
jvfausto 21:3489cffad196 113 /* Scales one joystick measurement to the chair's joystick measurement */
ryanlin97 4:29a27953fe70 114 float scaled_x = ((x_coor * 1.6f) + 1.7f)/3.3f;
ryanlin97 4:29a27953fe70 115 float scaled_y = (3.3f - (y_coor * 1.6f))/3.3f;
jvfausto 21:3489cffad196 116
jvfausto 21:3489cffad196 117 /* Sends the scaled joystic values to the chair */
jvfausto 21:3489cffad196 118 x->write(scaled_x);
ryanlin97 4:29a27953fe70 119 y->write(scaled_y);
ryanlin97 5:e0ccaab3959a 120 }
jvfausto 21:3489cffad196 121
jvfausto 21:3489cffad196 122 /* Automatic mode: move forward and update x,y coordinate sent to chair */
jvfausto 21:3489cffad196 123 void Wheelchair::forward()
ryanlin97 1:c0beadca1617 124 {
jvfausto 21:3489cffad196 125 if(forwardSafety == 0)
jvfausto 21:3489cffad196 126 {
ryanlin97 0:fc0c4a184482 127 x->write(high);
ryanlin97 3:a5e71bfdb492 128 y->write(def+offset);
jvfausto 21:3489cffad196 129 }
ryanlin97 0:fc0c4a184482 130 }
jvfausto 21:3489cffad196 131
jvfausto 21:3489cffad196 132 /* Automatic mode: move in reverse and update x,y coordinate sent to chair */
jvfausto 21:3489cffad196 133 void Wheelchair::backward()
ryanlin97 1:c0beadca1617 134 {
ryanlin97 0:fc0c4a184482 135 x->write(low);
ryanlin97 0:fc0c4a184482 136 y->write(def);
ryanlin97 0:fc0c4a184482 137 }
jvfausto 21:3489cffad196 138
jvfausto 21:3489cffad196 139 /* Automatic mode: move right and update x,y coordinate sent to chair */
jvfausto 21:3489cffad196 140 void Wheelchair::right()
ryanlin97 1:c0beadca1617 141 {
ryanlin97 0:fc0c4a184482 142 x->write(def);
ryanlin97 11:d14a1f7f1297 143 y->write(low);
ryanlin97 0:fc0c4a184482 144 }
ryanlin97 0:fc0c4a184482 145
jvfausto 21:3489cffad196 146 /* Automatic mode: move left and update x,y coordinate sent to chair */
jvfausto 21:3489cffad196 147 void Wheelchair::left()
ryanlin97 1:c0beadca1617 148 {
ryanlin97 0:fc0c4a184482 149 x->write(def);
ryanlin97 11:d14a1f7f1297 150 y->write(high);
ryanlin97 0:fc0c4a184482 151 }
jvfausto 21:3489cffad196 152
jvfausto 21:3489cffad196 153 /* Stop the wheelchair */
jvfausto 21:3489cffad196 154 void Wheelchair::stop()
ryanlin97 1:c0beadca1617 155 {
ryanlin97 0:fc0c4a184482 156 x->write(def);
ryanlin97 0:fc0c4a184482 157 y->write(def);
ryanlin97 6:0cd57bdd8fbc 158 }
jvfausto 21:3489cffad196 159
jvfausto 21:3489cffad196 160 /* Counter-clockwise is -
jvfausto 21:3489cffad196 161 * Clockwise is +
jvfausto 21:3489cffad196 162 * Range of deg: 0 to 360
jvfausto 21:3489cffad196 163 * This constructor takes in an angle from user and adjusts for turning right
jvfausto 21:3489cffad196 164 */
jvfausto 21:3489cffad196 165 void Wheelchair::pid_right(int deg)
ryanlin97 12:921488918749 166 {
jvfausto 21:3489cffad196 167 bool overturn = false; //Boolean if angle over 360˚
ryanlin97 12:921488918749 168
jvfausto 21:3489cffad196 169 out->printf("pid right\r\r\n");
jvfausto 21:3489cffad196 170 x->write(def); // Update x sent to chair to be stationary
jvfausto 21:3489cffad196 171 Setpoint = curr_yaw + deg; // Relative angle we want to turn
jvfausto 21:3489cffad196 172 pid_yaw = curr_yaw; // Sets pid_yaw to angle input from user
jvfausto 21:3489cffad196 173
jvfausto 21:3489cffad196 174 /* Turns on overturn boolean if setpoint over 360˚ */
jvfausto 21:3489cffad196 175 if(Setpoint > 360)
jvfausto 21:3489cffad196 176 {
ryanlin97 12:921488918749 177 overturn = true;
ryanlin97 12:921488918749 178 }
jvfausto 21:3489cffad196 179
jvfausto 21:3489cffad196 180 myPID.SetTunings(5.5,0, 0.0035); // Sets the constants for P and D
jvfausto 21:3489cffad196 181 myPID.SetOutputLimits(0, def-low-.15); // Limit is set to the differnce between def and low
jvfausto 21:3489cffad196 182 myPID.SetControllerDirection(DIRECT); // PID mode: Direct
jvfausto 21:3489cffad196 183
jvfausto 21:3489cffad196 184 /* PID stops when approaching a litte less than desired angle */
jvfausto 21:3489cffad196 185 while(pid_yaw < Setpoint - 3)
jvfausto 21:3489cffad196 186 {
jvfausto 21:3489cffad196 187 /* PID is set to correct angle range if angle greater than 360˚*/
jvfausto 17:7f3b69300bb6 188 if(overturn && curr_yaw < Setpoint-deg-1)
jvfausto 17:7f3b69300bb6 189 {
jvfausto 21:3489cffad196 190 pid_yaw = curr_yaw + 360;
jvfausto 21:3489cffad196 191 }
jvfausto 21:3489cffad196 192 else
jvfausto 21:3489cffad196 193 {
jvfausto 17:7f3b69300bb6 194 pid_yaw = curr_yaw;
ryanlin97 12:921488918749 195 }
jvfausto 21:3489cffad196 196
jvfausto 21:3489cffad196 197 myPID.Compute(); // Does PID calculations
jvfausto 21:3489cffad196 198 double tempor = -Output+def; // Temporary value with the voltage output
jvfausto 21:3489cffad196 199 y->write(tempor); // Update y sent to chair
jvfausto 21:3489cffad196 200
jvfausto 21:3489cffad196 201 /* Prints to serial monitor the current angle and setpoint */
jvfausto 21:3489cffad196 202 out->printf("curr_yaw %f\r\r\n", curr_yaw);
jvfausto 21:3489cffad196 203 out->printf("Setpoint = %f \r\n", Setpoint);
jvfausto 21:3489cffad196 204
jvfausto 21:3489cffad196 205 wait(.05); // Small delay (milliseconds)
ryanlin97 12:921488918749 206 }
jvfausto 21:3489cffad196 207
jvfausto 21:3489cffad196 208 /* Saftey stop for wheelchair */
jvfausto 21:3489cffad196 209 Wheelchair::stop();
jvfausto 21:3489cffad196 210 out->printf("done \r\n");
jvfausto 21:3489cffad196 211 }
jvfausto 21:3489cffad196 212
jvfausto 21:3489cffad196 213 /* Counter-clockwise is -
jvfausto 21:3489cffad196 214 * Clockwise is +
jvfausto 21:3489cffad196 215 * Range of deg: 0 to 360
jvfausto 21:3489cffad196 216 * This constructor takes in an angle from user and adjusts for turning left
jvfausto 21:3489cffad196 217 */
jvfausto 21:3489cffad196 218 void Wheelchair::pid_left(int deg)
ryanlin97 12:921488918749 219 {
jvfausto 21:3489cffad196 220 bool overturn = false; //Boolean if angle under 0˚
ryanlin97 12:921488918749 221
jvfausto 21:3489cffad196 222 out->printf("pid Left\r\r\n");
jvfausto 21:3489cffad196 223 x->write(def); // Update x sent to chair to be stationary
jvfausto 21:3489cffad196 224 Setpoint = curr_yaw - deg; // Relative angle we want to turn
jvfausto 21:3489cffad196 225 pid_yaw = curr_yaw; // Sets pid_yaw to angle input from user
jvfausto 21:3489cffad196 226
jvfausto 21:3489cffad196 227 /* Turns on overturn boolean if setpoint less than 0˚ */
jvfausto 21:3489cffad196 228 if(Setpoint < 0)
jvfausto 21:3489cffad196 229 {
ryanlin97 12:921488918749 230 overturn = true;
ryanlin97 12:921488918749 231 }
jvfausto 21:3489cffad196 232
jvfausto 21:3489cffad196 233 myPID.SetTunings(5,0, 0.004); // Sets the constants for P and D
jvfausto 21:3489cffad196 234 myPID.SetOutputLimits(0,high-def-.12); //Limit is set to the differnce between def and low
jvfausto 21:3489cffad196 235 myPID.SetControllerDirection(REVERSE); // PID mode: Reverse
jvfausto 21:3489cffad196 236
jvfausto 21:3489cffad196 237 /* PID stops when approaching a litte more than desired angle */
jvfausto 21:3489cffad196 238 while(pid_yaw > Setpoint+3)
jvfausto 21:3489cffad196 239 {
jvfausto 21:3489cffad196 240 /* PID is set to correct angle range if angle less than 0˚ */
jvfausto 21:3489cffad196 241 if(overturn && curr_yaw > Setpoint+deg+1)
jvfausto 17:7f3b69300bb6 242 {
jvfausto 17:7f3b69300bb6 243 pid_yaw = curr_yaw - 360;
jvfausto 21:3489cffad196 244 }
jvfausto 21:3489cffad196 245 else
jvfausto 21:3489cffad196 246 {
jvfausto 21:3489cffad196 247 pid_yaw = curr_yaw;
jvfausto 21:3489cffad196 248 }
jvfausto 21:3489cffad196 249
jvfausto 21:3489cffad196 250 myPID.Compute(); // Does PID calculations
jvfausto 21:3489cffad196 251 double tempor = Output+def; // Temporary value with the voltage output
jvfausto 21:3489cffad196 252 y->write(tempor); // Update y sent to chair
jvfausto 21:3489cffad196 253
jvfausto 21:3489cffad196 254 /* Prints to serial monitor the current angle and setpoint */
jvfausto 17:7f3b69300bb6 255 out->printf("curr_yaw %f\r\n", curr_yaw);
jvfausto 21:3489cffad196 256 out->printf("Setpoint = %f \r\n", Setpoint);
jvfausto 21:3489cffad196 257
jvfausto 21:3489cffad196 258 wait(.05); // Small delay (milliseconds)
ryanlin97 12:921488918749 259 }
jvfausto 21:3489cffad196 260
jvfausto 21:3489cffad196 261 /* Saftey stop for wheelchair */
jvfausto 21:3489cffad196 262 Wheelchair::stop();
jvfausto 21:3489cffad196 263 out->printf("done \r\n");
ryanlin97 12:921488918749 264
jvfausto 21:3489cffad196 265 }
jvfausto 21:3489cffad196 266
jvfausto 21:3489cffad196 267 /* This constructor determines whether to turn left or right */
jvfausto 21:3489cffad196 268 void Wheelchair::pid_turn(int deg)
jvfausto 21:3489cffad196 269 {
jvfausto 21:3489cffad196 270
jvfausto 21:3489cffad196 271 /* Sets angle to coterminal angle for left turn if deg > 180
jvfausto 21:3489cffad196 272 * Sets angle to coterminal angle for right turn if deg < -180
jvfausto 21:3489cffad196 273 */
jvfausto 21:3489cffad196 274 if(deg > 180)
jvfausto 21:3489cffad196 275 {
ryanlin97 12:921488918749 276 deg -= 360;
ryanlin97 12:921488918749 277 }
jvfausto 21:3489cffad196 278 else if(deg < -180)
jvfausto 21:3489cffad196 279 {
jvfausto 21:3489cffad196 280 deg +=360;
ryanlin97 12:921488918749 281 }
ryanlin97 12:921488918749 282
jvfausto 21:3489cffad196 283 /* Makes sure angle inputted to function is positive */
ryanlin97 12:921488918749 284 int turnAmt = abs(deg);
jvfausto 21:3489cffad196 285
jvfausto 21:3489cffad196 286 /* Calls PID_right if deg > 0, else calls PID_left if deg < 0 */
jvfausto 21:3489cffad196 287 if(deg >= 0)
jvfausto 21:3489cffad196 288 {
jvfausto 21:3489cffad196 289 Wheelchair::pid_right(turnAmt);
jvfausto 21:3489cffad196 290 }
jvfausto 21:3489cffad196 291 else
jvfausto 21:3489cffad196 292 {
jvfausto 21:3489cffad196 293 Wheelchair::pid_left(turnAmt);
jvfausto 21:3489cffad196 294 }
ryanlin97 12:921488918749 295
jvfausto 21:3489cffad196 296 }
jvfausto 21:3489cffad196 297
jvfausto 21:3489cffad196 298 /* This constructor takes in distance to travel and adjust to move forward */
jvfausto 19:71a6621ee5c3 299 void Wheelchair::pid_forward(double mm)
jvfausto 17:7f3b69300bb6 300 {
jvfausto 21:3489cffad196 301 mm -= 20; // Makes sure distance does not overshoot
jvfausto 21:3489cffad196 302 Input = 0; // Initializes input to zero: Test latter w/o
jvfausto 21:3489cffad196 303 wheel->reset(); // Resets encoders so that they start at 0
jvfausto 21:3489cffad196 304
jvfausto 17:7f3b69300bb6 305 out->printf("pid foward\r\n");
jvfausto 21:3489cffad196 306
jvfausto 21:3489cffad196 307 double tempor; // Initializes Temporary variable for x input
jvfausto 21:3489cffad196 308 Setpoint = mm; // Initializes the setpoint to desired value
jvfausto 21:3489cffad196 309
jvfausto 21:3489cffad196 310 myPIDDistance.SetTunings(5.5,0, 0.0015); // Sets constants for P and D
jvfausto 21:3489cffad196 311 myPIDDistance.SetOutputLimits(0,high-def-.15); // Limit set to difference between high and def
jvfausto 21:3489cffad196 312 myPIDDistance.SetControllerDirection(DIRECT); // PID mode: Direct
jvfausto 21:3489cffad196 313
jvfausto 21:3489cffad196 314 y->write(def+offset); // Update y to make chair stationary
jvfausto 21:3489cffad196 315
jvfausto 21:3489cffad196 316 /* Chair stops moving when Setpoint is reached */
jvfausto 21:3489cffad196 317 while(Input < Setpoint){
jvfausto 21:3489cffad196 318
jvfausto 21:3489cffad196 319 if(out->readable()) // Emergency Break
jvfausto 21:3489cffad196 320 {
jvfausto 21:3489cffad196 321 break;
jvfausto 21:3489cffad196 322 }
jvfausto 17:7f3b69300bb6 323
jvfausto 21:3489cffad196 324 Input = wheel->getDistance(53.975); // Gets distance from Encoder into PID
jvfausto 21:3489cffad196 325 wait(.05); // Slight Delay: *****Test without
jvfausto 21:3489cffad196 326 myPIDDistance.Compute(); // Compute distance traveled by chair
jvfausto 21:3489cffad196 327
jvfausto 21:3489cffad196 328 tempor = Output + def; // Temporary output variable
jvfausto 21:3489cffad196 329 x->write(tempor); // Update x sent to chair
jvfausto 17:7f3b69300bb6 330
jvfausto 21:3489cffad196 331 /* Prints to serial monitor the distance traveled by chair */
jvfausto 19:71a6621ee5c3 332 out->printf("distance %f\r\n", Input);
jvfausto 17:7f3b69300bb6 333 }
ryanlin97 12:921488918749 334
jvfausto 17:7f3b69300bb6 335 }
jvfausto 21:3489cffad196 336
jvfausto 21:3489cffad196 337 /* This constructor returns the relative angular position of chair */
jvfausto 21:3489cffad196 338 double Wheelchair::getTwistZ()
jvfausto 17:7f3b69300bb6 339 {
jvfausto 21:3489cffad196 340 return imu->gyro_z();
jvfausto 21:3489cffad196 341 }
jvfausto 18:663b6d693252 342
jvfausto 21:3489cffad196 343 /* This constructor computes the relative angle for Twist message in ROS */
jvfausto 21:3489cffad196 344 void Wheelchair::pid_twistA()
jvfausto 21:3489cffad196 345 {
jvfausto 21:3489cffad196 346 /* Initialize variables for angle and update x,y sent to chair */
jvfausto 21:3489cffad196 347 char c;
jvfausto 21:3489cffad196 348 double temporA = def;
jvfausto 21:3489cffad196 349 y->write(def);
jvfausto 21:3489cffad196 350 x->write(def);
jvfausto 21:3489cffad196 351
jvfausto 21:3489cffad196 352 PIDAngularV.SetTunings(.00015,0, 0.00); // Sets the constants for P and D
jvfausto 21:3489cffad196 353 PIDAngularV.SetOutputLimits(-.1, .1); // Limit set to be in range specified
jvfausto 21:3489cffad196 354 PIDAngularV.SetControllerDirection(DIRECT); // PID mode: Direct
jvfausto 21:3489cffad196 355
jvfausto 21:3489cffad196 356 /* Computes angular position of wheelchair while turning */
jvfausto 21:3489cffad196 357 while(1)
jvfausto 21:3489cffad196 358 {
jvfausto 21:3489cffad196 359 yDesired = angularV;
jvfausto 21:3489cffad196 360
jvfausto 21:3489cffad196 361 /* Update and set all variable so that the chair is stationary
jvfausto 21:3489cffad196 362 * if the desired angle is zero
jvfausto 21:3489cffad196 363 */
jvfausto 21:3489cffad196 364 if(yDesired == 0)
jvfausto 18:663b6d693252 365 {
jvfausto 21:3489cffad196 366 x->write(def);
jvfausto 21:3489cffad196 367 y->write(def);
jvfausto 21:3489cffad196 368 yDesired = 0;
ryanlin97 8:381a4ec3fef8 369 return;
ryanlin97 7:5e38d43fbce3 370 }
jvfausto 21:3489cffad196 371
jvfausto 21:3489cffad196 372 /* Continuously updates with current angle measured by IMU */
jvfausto 21:3489cffad196 373 yIn = imu->gyro_z();
jvfausto 21:3489cffad196 374 PIDAngularV.Compute();
jvfausto 21:3489cffad196 375 temporA += yOut; // Temporary value with the voltage output
jvfausto 21:3489cffad196 376 y->write(temporA); // Update y sent to chair
jvfausto 21:3489cffad196 377
jvfausto 21:3489cffad196 378 //out->printf("temporA: %f, yDesired %f, angle: %f\r\n", temporA, yDesired, imu->gyro_z());
jvfausto 21:3489cffad196 379 wait(.05); // Small delay (milliseconds)
ryanlin97 6:0cd57bdd8fbc 380 }
jvfausto 21:3489cffad196 381
jvfausto 21:3489cffad196 382 }
ryanlin97 6:0cd57bdd8fbc 383
jvfausto 21:3489cffad196 384 /* This constructor computes the relative velocity for Twist message in ROS */
jvfausto 21:3489cffad196 385 void Wheelchair::pid_twistV()
ryanlin97 6:0cd57bdd8fbc 386 {
jvfausto 21:3489cffad196 387 /* Initializes variables as default */
jvfausto 21:3489cffad196 388 double temporV = def;
jvfausto 26:c842070aa0b8 389 double temporS = def+offset;
jvfausto 21:3489cffad196 390 vDesiredS = 0;
jvfausto 21:3489cffad196 391 x->write(def);
jvfausto 27:302d03ee1ae0 392 y->write(def);
jvfausto 21:3489cffad196 393 wheel->reset(); // Resets the encoders
jvfausto 21:3489cffad196 394 /* Sets the constants for P and D */
jvfausto 21:3489cffad196 395 PIDVelosity.SetTunings(.0005,0, 0.00);
jvfausto 26:c842070aa0b8 396 PIDSlaveV.SetTunings(.005,0.000001, 0.000001);
jvfausto 21:3489cffad196 397
jvfausto 21:3489cffad196 398 /* Limits to the range specified */
jvfausto 21:3489cffad196 399 PIDVelosity.SetOutputLimits(-.005, .005);
jvfausto 21:3489cffad196 400 PIDSlaveV.SetOutputLimits(-.002, .002);
jvfausto 21:3489cffad196 401
jvfausto 21:3489cffad196 402 /* PID mode: Direct */
jvfausto 21:3489cffad196 403 PIDVelosity.SetControllerDirection(DIRECT);
jvfausto 21:3489cffad196 404 PIDSlaveV.SetControllerDirection(DIRECT);
jvfausto 21:3489cffad196 405
jvfausto 21:3489cffad196 406 while(1)
jvfausto 21:3489cffad196 407 {
jvfausto 27:302d03ee1ae0 408 //commandRead.linear.x = .7;
jvfausto 21:3489cffad196 409 test1 = linearV*100;
jvfausto 21:3489cffad196 410 vel = curr_vel;
jvfausto 21:3489cffad196 411 vDesired = linearV*100;
jvfausto 21:3489cffad196 412 if(out->readable())
jvfausto 21:3489cffad196 413 return;
jvfausto 21:3489cffad196 414 /* Update and set all variable so that the chair is stationary
jvfausto 21:3489cffad196 415 * if the velocity is zero
jvfausto 21:3489cffad196 416 */
jvfausto 21:3489cffad196 417 if(linearV == 0)
jvfausto 21:3489cffad196 418 {
jvfausto 21:3489cffad196 419 x->write(def);
jvfausto 21:3489cffad196 420 y->write(def);
ryanlin97 8:381a4ec3fef8 421
jvfausto 21:3489cffad196 422 vel = 0;
jvfausto 21:3489cffad196 423 vDesired = 0;
jvfausto 21:3489cffad196 424 dist_old = 0;
ryanlin97 8:381a4ec3fef8 425 return;
ryanlin97 8:381a4ec3fef8 426 }
jvfausto 21:3489cffad196 427
jvfausto 21:3489cffad196 428 if(vDesired >= 0)
jvfausto 21:3489cffad196 429 {
jvfausto 21:3489cffad196 430 PIDVelosity.SetTunings(.000004,0, 0.00); // Sets the constants for P and D
jvfausto 21:3489cffad196 431 PIDVelosity.SetOutputLimits(-.002, .002); // Limits to the range specified
jvfausto 21:3489cffad196 432 }
jvfausto 21:3489cffad196 433 else
jvfausto 21:3489cffad196 434 {
jvfausto 21:3489cffad196 435 PIDVelosity.SetTunings(.000015,0, 0.00); // Sets the constants for P and D
jvfausto 21:3489cffad196 436 PIDVelosity.SetOutputLimits(-.0005, .0005); // Limits to range specified
jvfausto 21:3489cffad196 437 }
jvfausto 21:3489cffad196 438
jvfausto 21:3489cffad196 439 /* Sets maximum value of variable to 1 */
jvfausto 21:3489cffad196 440 if(temporV >= 1.5)
jvfausto 21:3489cffad196 441 {
jvfausto 21:3489cffad196 442 temporV = 1.5;
ryanlin97 8:381a4ec3fef8 443 }
jvfausto 21:3489cffad196 444 /* Scales and makes some adjustments to velocity */
jvfausto 21:3489cffad196 445 vIn = curr_vel*100;
jvfausto 21:3489cffad196 446 vInS = curr_vel-curr_velS;
jvfausto 21:3489cffad196 447 PIDVelosity.Compute();
jvfausto 21:3489cffad196 448 PIDSlaveV.Compute();
jvfausto 21:3489cffad196 449 if(forwardSafety == 0)
jvfausto 21:3489cffad196 450 {
jvfausto 21:3489cffad196 451 temporV += vOut;
jvfausto 21:3489cffad196 452 temporS += vOutS;
jvfausto 21:3489cffad196 453
jvfausto 21:3489cffad196 454 /* Updates x,y sent to Wheelchair and for Odometry message in ROS */
jvfausto 21:3489cffad196 455 x->write(temporV);
jvfausto 21:3489cffad196 456 test2 = temporV;
jvfausto 21:3489cffad196 457 y->write(temporS);
jvfausto 21:3489cffad196 458 }
jvfausto 21:3489cffad196 459 else
jvfausto 21:3489cffad196 460 {
jvfausto 21:3489cffad196 461 x->write(def);
jvfausto 21:3489cffad196 462 y->write(def);
jvfausto 21:3489cffad196 463 }
jvfausto 21:3489cffad196 464 //out->printf("Velosity: %f, Velosity2: %f, temporV %f, temporS %f\r\n", curr_vel, curr_velS, temporV, temporS);
jvfausto 21:3489cffad196 465 Wheelchair::odomMsg();
jvfausto 21:3489cffad196 466 wait(.01); // Small delay (milliseconds)
ryanlin97 6:0cd57bdd8fbc 467 }
ryanlin97 11:d14a1f7f1297 468 }
ryanlin97 11:d14a1f7f1297 469
jvfausto 21:3489cffad196 470 /* This constructor calculates the relative position of the chair everytime the encoders reset
jvfausto 21:3489cffad196 471 * by setting its old position as the origin to calculate the new position
jvfausto 21:3489cffad196 472 */
jvfausto 21:3489cffad196 473 void Wheelchair::odomMsg()
ryanlin97 11:d14a1f7f1297 474 {
jvfausto 21:3489cffad196 475 double dist_new = curr_pos;
jvfausto 21:3489cffad196 476 double dist = dist_new-dist_old;
jvfausto 21:3489cffad196 477 double temp_x = dist*sin(z_angular*3.14159/180);
jvfausto 21:3489cffad196 478 double temp_y = dist*cos(z_angular*3.14159/180);
jvfausto 21:3489cffad196 479
jvfausto 21:3489cffad196 480 x_position += temp_x;
jvfausto 21:3489cffad196 481 y_position += temp_y;
ryanlin97 11:d14a1f7f1297 482
jvfausto 21:3489cffad196 483 dist_old = dist_new;
jvfausto 21:3489cffad196 484 }
jvfausto 21:3489cffad196 485
jvfausto 21:3489cffad196 486 /* This constructor prints the Odometry message to the serial monitor */
jvfausto 21:3489cffad196 487 void Wheelchair::showOdom()
jvfausto 21:3489cffad196 488 {
jvfausto 21:3489cffad196 489 out->printf("x %f, y %f, angle %f", x_position, y_position, z_angular);
jvfausto 21:3489cffad196 490 }
jvfausto 21:3489cffad196 491
jvfausto 21:3489cffad196 492 /* This constructor returns the approximate distance based on the wheel diameter */
jvfausto 21:3489cffad196 493 float Wheelchair::getDistance()
jvfausto 21:3489cffad196 494 {
jvfausto 21:3489cffad196 495 return wheel->getDistance(Diameter);
ryanlin97 6:0cd57bdd8fbc 496 }
ryanlin97 8:381a4ec3fef8 497
jvfausto 21:3489cffad196 498 /* This constructor resets the wheel encoder's */
jvfausto 21:3489cffad196 499 void Wheelchair::resetDistance()
jvfausto 21:3489cffad196 500 {
ryanlin97 12:921488918749 501 wheel->reset();
jvfausto 21:3489cffad196 502 }
jvfausto 21:3489cffad196 503
jvfausto 21:3489cffad196 504
jvfausto 21:3489cffad196 505 /*Predetermined paths For Demmo*/
jvfausto 21:3489cffad196 506 void Wheelchair::desk()
jvfausto 21:3489cffad196 507 {
jvfausto 19:71a6621ee5c3 508 Wheelchair::pid_forward(5461);
jvfausto 19:71a6621ee5c3 509 Wheelchair::pid_right(87);
jvfausto 19:71a6621ee5c3 510 Wheelchair::pid_forward(3658);
jvfausto 19:71a6621ee5c3 511 Wheelchair::pid_right(87);
jvfausto 19:71a6621ee5c3 512 Wheelchair::pid_forward(3658);
jvfausto 21:3489cffad196 513 }
jvfausto 21:3489cffad196 514
jvfausto 21:3489cffad196 515 void Wheelchair::kitchen()
jvfausto 21:3489cffad196 516 {
jvfausto 19:71a6621ee5c3 517 Wheelchair::pid_forward(5461);
jvfausto 20:f42db4ae16f0 518 Wheelchair::pid_right(87);
jvfausto 19:71a6621ee5c3 519 Wheelchair::pid_forward(3658);
jvfausto 19:71a6621ee5c3 520 Wheelchair::pid_left(90);
jvfausto 19:71a6621ee5c3 521 Wheelchair::pid_forward(305);
jvfausto 21:3489cffad196 522 }
jvfausto 21:3489cffad196 523
jvfausto 21:3489cffad196 524 void Wheelchair::desk_to_kitchen()
jvfausto 21:3489cffad196 525 {
jvfausto 19:71a6621ee5c3 526 Wheelchair::pid_right(180);
jvfausto 19:71a6621ee5c3 527 Wheelchair::pid_forward(3700);
jvfausto 21:3489cffad196 528 }