Working file for communication with ROS
Dependencies: QEI2 PID VL53L1X_Filter
wheelchair.cpp@25:58ec657a44f2, 2018-10-27 (annotated)
- Committer:
- jvfausto
- Date:
- Sat Oct 27 16:47:50 2018 +0000
- Revision:
- 25:58ec657a44f2
- Parent:
- 24:d2f234fbc20d
With odometry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryanlin97 | 0:fc0c4a184482 | 1 | #include "wheelchair.h" |
jvfausto | 25:58ec657a44f2 | 2 | |
jvfausto | 23:8d11d953ceeb | 3 | bool manual_drive = false; // Variable Changes between joystick and auto drive |
jvfausto | 24:d2f234fbc20d | 4 | double curr_yaw, curr_vel, curr_velS; // Variable that contains current relative angle |
jvfausto | 23:8d11d953ceeb | 5 | double encoder_distance; // Keeps distanse due to original position |
jvfausto | 25:58ec657a44f2 | 6 | |
jvfausto | 23:8d11d953ceeb | 7 | volatile double Setpoint, Output, Input, Input2; // Variables for PID |
jvfausto | 23:8d11d953ceeb | 8 | volatile double pid_yaw, Distance, Setpoint2, Output2, encoder_distance2; // Variables for PID |
jvfausto | 24:d2f234fbc20d | 9 | volatile double vIn, vOut, vDesired; |
jvfausto | 24:d2f234fbc20d | 10 | volatile double vInS, vOutS, vDesiredS; |
jvfausto | 24:d2f234fbc20d | 11 | volatile double yIn, yOut, yDesired; |
jvfausto | 19:71a6621ee5c3 | 12 | |
jvfausto | 25:58ec657a44f2 | 13 | double z_angular, dist_old, curr_pos; |
jvfausto | 25:58ec657a44f2 | 14 | double x_position = 0; |
jvfausto | 25:58ec657a44f2 | 15 | double y_position = 0; |
jvfausto | 25:58ec657a44f2 | 16 | |
jvfausto | 23:8d11d953ceeb | 17 | PID myPID(&pid_yaw, &Output, &Setpoint, 5.5, .00, 0.0036, P_ON_E, DIRECT); // Angle PID object constructor |
jvfausto | 23:8d11d953ceeb | 18 | PID myPIDDistance(&Input, &Output, &Setpoint, 5.5, .00, 0.002, P_ON_E, DIRECT); // Distance PID object constructor |
jvfausto | 24:d2f234fbc20d | 19 | PID PIDVelosity(&vIn, &vOut, &vDesired, 5.5, .00, .002, P_ON_E, DIRECT); |
jvfausto | 24:d2f234fbc20d | 20 | PID PIDSlaveV(&vInS, &vOutS, &vDesiredS, 5.5, .00, .002, P_ON_E, DIRECT); |
jvfausto | 24:d2f234fbc20d | 21 | PID PIDAngularV(&yIn, &yOut, &yDesired, 5.5, .00, .002, P_ON_E, DIRECT); |
jvfausto | 25:58ec657a44f2 | 22 | |
jvfausto | 23:8d11d953ceeb | 23 | void Wheelchair::compass_thread() { // Thread that measures which angle we are at |
ryanlin97 | 11:d14a1f7f1297 | 24 | curr_yaw = imu->yaw(); |
jvfausto | 25:58ec657a44f2 | 25 | z_angular = curr_yaw; |
ryanlin97 | 11:d14a1f7f1297 | 26 | } |
jvfausto | 24:d2f234fbc20d | 27 | void Wheelchair::velosity_thread() { |
jvfausto | 24:d2f234fbc20d | 28 | curr_vel = wheel->getVelosity(); |
jvfausto | 24:d2f234fbc20d | 29 | curr_velS = wheelS->getVelosity(); |
jvfausto | 25:58ec657a44f2 | 30 | curr_pos = wheel->getDistance(53.975); |
jvfausto | 24:d2f234fbc20d | 31 | } |
ryanlin97 | 11:d14a1f7f1297 | 32 | |
jvfausto | 24:d2f234fbc20d | 33 | Wheelchair::Wheelchair(PinName xPin, PinName yPin, Serial* pc, Timer* time, QEI* qei, QEI* qeiS) // Function Constructor for Wheelchair class |
ryanlin97 | 1:c0beadca1617 | 34 | { |
jvfausto | 23:8d11d953ceeb | 35 | //Initializes X and Y variables to Pins |
jvfausto | 23:8d11d953ceeb | 36 | x = new PwmOut(xPin); |
ryanlin97 | 3:a5e71bfdb492 | 37 | y = new PwmOut(yPin); |
jvfausto | 23:8d11d953ceeb | 38 | |
jvfausto | 23:8d11d953ceeb | 39 | // Initializes IMU Library |
ryanlin97 | 11:d14a1f7f1297 | 40 | imu = new chair_BNO055(pc, time); |
jvfausto | 23:8d11d953ceeb | 41 | Wheelchair::stop(); // Wheelchair is not moving when initializing |
jvfausto | 23:8d11d953ceeb | 42 | imu->setup(); // turns on the IMU |
jvfausto | 23:8d11d953ceeb | 43 | out = pc; // "out" is called for serial monitor |
jvfausto | 24:d2f234fbc20d | 44 | wheelS = qeiS; // "wheel" is called for encoder |
jvfausto | 24:d2f234fbc20d | 45 | wheel = qei; |
jvfausto | 23:8d11d953ceeb | 46 | out->printf("wheelchair setup done \r\n"); // make sure it initialized |
ryanlin97 | 11:d14a1f7f1297 | 47 | ti = time; |
jvfausto | 23:8d11d953ceeb | 48 | myPID.SetMode(AUTOMATIC); // set PID to automatic |
ryanlin97 | 1:c0beadca1617 | 49 | } |
jvfausto | 25:58ec657a44f2 | 50 | |
jvfausto | 23:8d11d953ceeb | 51 | void Wheelchair::move(float x_coor, float y_coor) // moves the chair with joystick on manual |
ryanlin97 | 1:c0beadca1617 | 52 | { |
jvfausto | 25:58ec657a44f2 | 53 | |
jvfausto | 23:8d11d953ceeb | 54 | float scaled_x = ((x_coor * 1.6f) + 1.7f)/3.3f; // Scales one joystic measurement to the |
jvfausto | 23:8d11d953ceeb | 55 | float scaled_y = (3.3f - (y_coor * 1.6f))/3.3f; // chair's joystic measurement |
ryanlin97 | 11:d14a1f7f1297 | 56 | |
jvfausto | 23:8d11d953ceeb | 57 | x->write(scaled_x); // Sends the scaled joystic values to the chair |
ryanlin97 | 4:29a27953fe70 | 58 | y->write(scaled_y); |
ryanlin97 | 5:e0ccaab3959a | 59 | } |
jvfausto | 25:58ec657a44f2 | 60 | |
jvfausto | 23:8d11d953ceeb | 61 | void Wheelchair::forward() // In auto to move foward |
ryanlin97 | 1:c0beadca1617 | 62 | { |
ryanlin97 | 0:fc0c4a184482 | 63 | x->write(high); |
ryanlin97 | 3:a5e71bfdb492 | 64 | y->write(def+offset); |
ryanlin97 | 0:fc0c4a184482 | 65 | } |
jvfausto | 25:58ec657a44f2 | 66 | |
jvfausto | 23:8d11d953ceeb | 67 | void Wheelchair::backward() // In auto to move reverse |
ryanlin97 | 1:c0beadca1617 | 68 | { |
ryanlin97 | 0:fc0c4a184482 | 69 | x->write(low); |
ryanlin97 | 0:fc0c4a184482 | 70 | y->write(def); |
ryanlin97 | 0:fc0c4a184482 | 71 | } |
jvfausto | 25:58ec657a44f2 | 72 | |
jvfausto | 23:8d11d953ceeb | 73 | void Wheelchair::right() // In auto to move right |
ryanlin97 | 1:c0beadca1617 | 74 | { |
ryanlin97 | 0:fc0c4a184482 | 75 | x->write(def); |
ryanlin97 | 11:d14a1f7f1297 | 76 | y->write(low); |
ryanlin97 | 0:fc0c4a184482 | 77 | } |
jvfausto | 25:58ec657a44f2 | 78 | |
jvfausto | 23:8d11d953ceeb | 79 | void Wheelchair::left() // In auto to move left |
ryanlin97 | 1:c0beadca1617 | 80 | { |
ryanlin97 | 0:fc0c4a184482 | 81 | x->write(def); |
ryanlin97 | 11:d14a1f7f1297 | 82 | y->write(high); |
ryanlin97 | 0:fc0c4a184482 | 83 | } |
jvfausto | 25:58ec657a44f2 | 84 | |
jvfausto | 23:8d11d953ceeb | 85 | void Wheelchair::stop() // Stops the chair |
ryanlin97 | 1:c0beadca1617 | 86 | { |
ryanlin97 | 0:fc0c4a184482 | 87 | x->write(def); |
ryanlin97 | 0:fc0c4a184482 | 88 | y->write(def); |
ryanlin97 | 6:0cd57bdd8fbc | 89 | } |
ryanlin97 | 11:d14a1f7f1297 | 90 | // counter clockwise is - |
ryanlin97 | 11:d14a1f7f1297 | 91 | // clockwise is + |
jvfausto | 23:8d11d953ceeb | 92 | void Wheelchair::pid_right(int deg) // Takes in degree and turns right |
ryanlin97 | 12:921488918749 | 93 | { |
jvfausto | 23:8d11d953ceeb | 94 | bool overturn = false; //Boolean if we have to turn over relative 360˚ |
ryanlin97 | 12:921488918749 | 95 | |
jvfausto | 23:8d11d953ceeb | 96 | out->printf("pid right\r\r\n"); |
jvfausto | 23:8d11d953ceeb | 97 | x->write(def); // Not moving fowards or reverse |
jvfausto | 23:8d11d953ceeb | 98 | Setpoint = curr_yaw + deg; // Relative angle we want to turn |
jvfausto | 23:8d11d953ceeb | 99 | pid_yaw = curr_yaw; // Sets input to current angle(pid_yaw = input) |
jvfausto | 23:8d11d953ceeb | 100 | |
jvfausto | 23:8d11d953ceeb | 101 | if(Setpoint > 360) { //Turns on overturn boolean if setpoint over 360˚ |
ryanlin97 | 12:921488918749 | 102 | overturn = true; |
ryanlin97 | 12:921488918749 | 103 | } |
jvfausto | 23:8d11d953ceeb | 104 | |
jvfausto | 23:8d11d953ceeb | 105 | myPID.SetTunings(5.5,0, 0.0035); // Sets the constants for P and D |
jvfausto | 23:8d11d953ceeb | 106 | myPID.SetOutputLimits(0, def-low-.15); // Limits to the differnce between def and low |
jvfausto | 23:8d11d953ceeb | 107 | myPID.SetControllerDirection(DIRECT); // PID mode Direct |
jvfausto | 23:8d11d953ceeb | 108 | |
jvfausto | 23:8d11d953ceeb | 109 | while(pid_yaw < Setpoint - 3){ // Tells PID to stop when reaching |
jvfausto | 23:8d11d953ceeb | 110 | // a little less than desired angle |
jvfausto | 23:8d11d953ceeb | 111 | if(overturn && curr_yaw < Setpoint-deg-1) // Sets PID yaw to coterminal angle if necesary |
jvfausto | 17:7f3b69300bb6 | 112 | { |
jvfausto | 17:7f3b69300bb6 | 113 | pid_yaw = curr_yaw + 360; |
jvfausto | 17:7f3b69300bb6 | 114 | } |
jvfausto | 17:7f3b69300bb6 | 115 | else |
jvfausto | 17:7f3b69300bb6 | 116 | pid_yaw = curr_yaw; |
jvfausto | 23:8d11d953ceeb | 117 | |
jvfausto | 23:8d11d953ceeb | 118 | myPID.Compute(); // Does PID calculations |
jvfausto | 23:8d11d953ceeb | 119 | double tempor = -Output+def; // Temporary value with the voltage output |
jvfausto | 23:8d11d953ceeb | 120 | y->write(tempor); // Sends to chair y output command |
jvfausto | 23:8d11d953ceeb | 121 | |
jvfausto | 23:8d11d953ceeb | 122 | out->printf("curr_yaw %f\r\r\n", curr_yaw); |
jvfausto | 17:7f3b69300bb6 | 123 | out->printf("Setpoint = %f \r\n", Setpoint); |
jvfausto | 25:58ec657a44f2 | 124 | |
jvfausto | 23:8d11d953ceeb | 125 | wait(.05); // Small delay |
ryanlin97 | 12:921488918749 | 126 | } |
jvfausto | 23:8d11d953ceeb | 127 | Wheelchair::stop(); // Safety Stop |
jvfausto | 17:7f3b69300bb6 | 128 | out->printf("done \r\n"); |
jvfausto | 23:8d11d953ceeb | 129 | } |
jvfausto | 25:58ec657a44f2 | 130 | |
jvfausto | 23:8d11d953ceeb | 131 | void Wheelchair::pid_left(int deg) // Takes in degree and turns left |
ryanlin97 | 12:921488918749 | 132 | { |
jvfausto | 23:8d11d953ceeb | 133 | bool overturn = false; //Boolean if we have to turn under relative 0˚ |
ryanlin97 | 12:921488918749 | 134 | |
jvfausto | 23:8d11d953ceeb | 135 | out->printf("pid Left\r\r\n"); |
jvfausto | 23:8d11d953ceeb | 136 | x->write(def); // Not moving fowards or reverse |
jvfausto | 23:8d11d953ceeb | 137 | Setpoint = curr_yaw - deg; // Relative angle we want to turn |
jvfausto | 23:8d11d953ceeb | 138 | pid_yaw = curr_yaw; // Sets input to current angle(pid_yaw = input) |
jvfausto | 23:8d11d953ceeb | 139 | if(Setpoint < 0) { //Turns on overturn boolean if setpoint under 0˚ |
ryanlin97 | 12:921488918749 | 140 | overturn = true; |
ryanlin97 | 12:921488918749 | 141 | } |
jvfausto | 23:8d11d953ceeb | 142 | myPID.SetTunings(5,0, 0.004); // Sets the constants for P and D |
jvfausto | 23:8d11d953ceeb | 143 | myPID.SetOutputLimits(0,high-def-.12); // Limits to the differnce between High and Def |
jvfausto | 23:8d11d953ceeb | 144 | myPID.SetControllerDirection(REVERSE); // PID mode Reverse |
jvfausto | 23:8d11d953ceeb | 145 | while(pid_yaw > Setpoint+3){ // Tells PID to stop when reaching |
jvfausto | 23:8d11d953ceeb | 146 | // a little more than desired angle |
jvfausto | 23:8d11d953ceeb | 147 | if(overturn && curr_yaw > Setpoint+deg+1) // Sets PID yaw to coterminal angle if necesary |
jvfausto | 17:7f3b69300bb6 | 148 | { |
jvfausto | 17:7f3b69300bb6 | 149 | pid_yaw = curr_yaw - 360; |
jvfausto | 17:7f3b69300bb6 | 150 | } |
jvfausto | 17:7f3b69300bb6 | 151 | else |
jvfausto | 17:7f3b69300bb6 | 152 | pid_yaw = curr_yaw; |
jvfausto | 23:8d11d953ceeb | 153 | |
jvfausto | 23:8d11d953ceeb | 154 | myPID.Compute(); // Does PID calculations |
jvfausto | 23:8d11d953ceeb | 155 | double tempor = Output+def; // Temporary value with the voltage output |
jvfausto | 23:8d11d953ceeb | 156 | y->write(tempor); // Sends to chair y output command |
jvfausto | 23:8d11d953ceeb | 157 | |
jvfausto | 17:7f3b69300bb6 | 158 | out->printf("curr_yaw %f\r\n", curr_yaw); |
jvfausto | 23:8d11d953ceeb | 159 | wait(.05); // Small Delay |
ryanlin97 | 12:921488918749 | 160 | } |
jvfausto | 23:8d11d953ceeb | 161 | Wheelchair::stop(); // Safety Stop |
jvfausto | 23:8d11d953ceeb | 162 | } |
jvfausto | 25:58ec657a44f2 | 163 | |
jvfausto | 23:8d11d953ceeb | 164 | void Wheelchair::pid_turn(int deg) { // Determine wether we are turn right or left |
jvfausto | 25:58ec657a44f2 | 165 | |
jvfausto | 23:8d11d953ceeb | 166 | if(deg > 180) { // If deg > 180 turn left: coterminal angle |
ryanlin97 | 12:921488918749 | 167 | deg -= 360; |
ryanlin97 | 12:921488918749 | 168 | } |
jvfausto | 25:58ec657a44f2 | 169 | |
jvfausto | 23:8d11d953ceeb | 170 | else if(deg < -180) { // If deg < -180 turn right: coterminal angle |
ryanlin97 | 12:921488918749 | 171 | deg+=360; |
ryanlin97 | 12:921488918749 | 172 | } |
ryanlin97 | 12:921488918749 | 173 | |
jvfausto | 23:8d11d953ceeb | 174 | int turnAmt = abs(deg); // Makes sure input angle is positive |
jvfausto | 25:58ec657a44f2 | 175 | |
ryanlin97 | 12:921488918749 | 176 | if(deg >= 0){ |
jvfausto | 23:8d11d953ceeb | 177 | Wheelchair::pid_right(turnAmt); // Calls PID right if positive degree |
jvfausto | 23:8d11d953ceeb | 178 | } |
ryanlin97 | 12:921488918749 | 179 | else { |
jvfausto | 23:8d11d953ceeb | 180 | Wheelchair::pid_left(turnAmt); // Calls PID left if negative degree |
ryanlin97 | 12:921488918749 | 181 | } |
jvfausto | 23:8d11d953ceeb | 182 | } |
jvfausto | 19:71a6621ee5c3 | 183 | void Wheelchair::pid_forward(double mm) |
jvfausto | 17:7f3b69300bb6 | 184 | { |
jvfausto | 23:8d11d953ceeb | 185 | mm -= 20; // Makes sure distance does not overshoot |
jvfausto | 23:8d11d953ceeb | 186 | Input = 0; // Initializes imput to cero: Test latter w/o |
jvfausto | 23:8d11d953ceeb | 187 | wheel->reset(); // Resets encoders so that they start at 0 |
jvfausto | 17:7f3b69300bb6 | 188 | out->printf("pid foward\r\n"); |
jvfausto | 25:58ec657a44f2 | 189 | |
jvfausto | 23:8d11d953ceeb | 190 | double tempor; // Initializes Temporary variable for x input |
jvfausto | 23:8d11d953ceeb | 191 | Setpoint = mm; // Initializes the setpoint to desired value |
jvfausto | 25:58ec657a44f2 | 192 | |
jvfausto | 23:8d11d953ceeb | 193 | myPIDDistance.SetTunings(5.5,0, 0.0015); // Sets constants for P and D |
jvfausto | 23:8d11d953ceeb | 194 | myPIDDistance.SetOutputLimits(0,high-def-.15); // Limits to the differnce between High and Def |
jvfausto | 23:8d11d953ceeb | 195 | myPIDDistance.SetControllerDirection(DIRECT); // PID to Direct |
jvfausto | 23:8d11d953ceeb | 196 | y->write(def+offset); // Sets chair to not turn |
jvfausto | 23:8d11d953ceeb | 197 | |
jvfausto | 23:8d11d953ceeb | 198 | while(Input < Setpoint){ // Stop moving when reaching setpoint |
jvfausto | 23:8d11d953ceeb | 199 | |
jvfausto | 23:8d11d953ceeb | 200 | if(out->readable()) // Emergency Break |
jvfausto | 19:71a6621ee5c3 | 201 | break; |
jvfausto | 23:8d11d953ceeb | 202 | |
jvfausto | 23:8d11d953ceeb | 203 | Input = wheel->getDistance(53.975); // Gets Distance from Encoder onto PID |
jvfausto | 23:8d11d953ceeb | 204 | wait(.05); // Slight Delay: *****Test without |
jvfausto | 23:8d11d953ceeb | 205 | myPIDDistance.Compute(); // Compute Output for chair |
jvfausto | 25:58ec657a44f2 | 206 | |
jvfausto | 23:8d11d953ceeb | 207 | tempor = Output + def; // Temporary output variable |
jvfausto | 23:8d11d953ceeb | 208 | x->write(tempor); // Sends to chair x output |
jvfausto | 19:71a6621ee5c3 | 209 | out->printf("distance %f\r\n", Input); |
jvfausto | 17:7f3b69300bb6 | 210 | } |
ryanlin97 | 12:921488918749 | 211 | |
jvfausto | 17:7f3b69300bb6 | 212 | } |
jvfausto | 17:7f3b69300bb6 | 213 | void Wheelchair::pid_reverse(double mm) |
jvfausto | 17:7f3b69300bb6 | 214 | { |
jvfausto | 25:58ec657a44f2 | 215 | |
jvfausto | 17:7f3b69300bb6 | 216 | } |
jvfausto | 24:d2f234fbc20d | 217 | void Wheelchair::pid_twistA() |
jvfausto | 24:d2f234fbc20d | 218 | { |
jvfausto | 24:d2f234fbc20d | 219 | char c; |
jvfausto | 24:d2f234fbc20d | 220 | double temporA = def; |
jvfausto | 24:d2f234fbc20d | 221 | y->write(def); |
jvfausto | 24:d2f234fbc20d | 222 | x->write(def); |
jvfausto | 25:58ec657a44f2 | 223 | |
jvfausto | 24:d2f234fbc20d | 224 | PIDAngularV.SetTunings(.00015,0, 0.00); // Sets the constants for P and D |
jvfausto | 24:d2f234fbc20d | 225 | PIDAngularV.SetOutputLimits(-.1, .1); // Limits to the differnce between def and low |
jvfausto | 24:d2f234fbc20d | 226 | PIDAngularV.SetControllerDirection(DIRECT); // PID mode Direct |
jvfausto | 24:d2f234fbc20d | 227 | while(1) |
jvfausto | 24:d2f234fbc20d | 228 | { |
jvfausto | 24:d2f234fbc20d | 229 | if(out->readable()) |
jvfausto | 24:d2f234fbc20d | 230 | c = out->getc(); |
jvfausto | 24:d2f234fbc20d | 231 | if(c == '0') |
jvfausto | 24:d2f234fbc20d | 232 | { |
jvfausto | 24:d2f234fbc20d | 233 | yDesired = 0; |
jvfausto | 24:d2f234fbc20d | 234 | } |
jvfausto | 24:d2f234fbc20d | 235 | if(c == '1') |
jvfausto | 24:d2f234fbc20d | 236 | yDesired = 30; |
jvfausto | 24:d2f234fbc20d | 237 | if(c == '2') |
jvfausto | 24:d2f234fbc20d | 238 | yDesired = 90; |
jvfausto | 24:d2f234fbc20d | 239 | if(c == '9') |
jvfausto | 24:d2f234fbc20d | 240 | yDesired = -30; |
jvfausto | 24:d2f234fbc20d | 241 | if(c == '8') |
jvfausto | 24:d2f234fbc20d | 242 | yDesired = -90; |
jvfausto | 24:d2f234fbc20d | 243 | if(c == 'r') |
jvfausto | 24:d2f234fbc20d | 244 | { |
jvfausto | 24:d2f234fbc20d | 245 | yDesired = 0; |
jvfausto | 24:d2f234fbc20d | 246 | return; |
jvfausto | 24:d2f234fbc20d | 247 | } |
jvfausto | 24:d2f234fbc20d | 248 | |
jvfausto | 24:d2f234fbc20d | 249 | yIn = imu->gyro_z(); |
jvfausto | 24:d2f234fbc20d | 250 | PIDAngularV.Compute(); |
jvfausto | 24:d2f234fbc20d | 251 | temporA += yOut; // Temporary value with the voltage output |
jvfausto | 24:d2f234fbc20d | 252 | y->write(temporA); |
jvfausto | 25:58ec657a44f2 | 253 | //out->printf("temporA: %f, yDesired %f, angle: %f\r\n", temporA, yDesired, imu->gyro_z()); |
jvfausto | 24:d2f234fbc20d | 254 | wait(.05); |
jvfausto | 24:d2f234fbc20d | 255 | } |
jvfausto | 24:d2f234fbc20d | 256 | } |
jvfausto | 24:d2f234fbc20d | 257 | void Wheelchair::pid_twistV() |
jvfausto | 24:d2f234fbc20d | 258 | { |
jvfausto | 24:d2f234fbc20d | 259 | double temporV = def; |
jvfausto | 24:d2f234fbc20d | 260 | double temporS = def; |
jvfausto | 24:d2f234fbc20d | 261 | vDesiredS = 0; |
jvfausto | 24:d2f234fbc20d | 262 | char c; |
jvfausto | 24:d2f234fbc20d | 263 | x->write(def); |
jvfausto | 24:d2f234fbc20d | 264 | y->write(def); |
jvfausto | 25:58ec657a44f2 | 265 | wheel->reset(); |
jvfausto | 24:d2f234fbc20d | 266 | PIDVelosity.SetTunings(.00005,0, 0.00); // Sets the constants for P and D |
jvfausto | 24:d2f234fbc20d | 267 | PIDSlaveV.SetTunings(.007,0.000001, 0.000001); // Sets the constants for P and D |
jvfausto | 24:d2f234fbc20d | 268 | PIDVelosity.SetOutputLimits(-.005, .005); // Limits to the differnce between def and low |
jvfausto | 24:d2f234fbc20d | 269 | PIDSlaveV.SetOutputLimits(-.002, .002); // Limits to the differnce between def and low |
jvfausto | 24:d2f234fbc20d | 270 | PIDVelosity.SetControllerDirection(DIRECT); |
jvfausto | 24:d2f234fbc20d | 271 | PIDSlaveV.SetControllerDirection(DIRECT); |
jvfausto | 24:d2f234fbc20d | 272 | while(1) |
jvfausto | 24:d2f234fbc20d | 273 | { |
jvfausto | 24:d2f234fbc20d | 274 | if(out->readable()) |
jvfausto | 24:d2f234fbc20d | 275 | c = out->getc(); |
jvfausto | 24:d2f234fbc20d | 276 | if(c == '0') |
jvfausto | 24:d2f234fbc20d | 277 | vDesired = 0; |
jvfausto | 24:d2f234fbc20d | 278 | if(c == '3') |
jvfausto | 24:d2f234fbc20d | 279 | vDesired = 100; |
jvfausto | 24:d2f234fbc20d | 280 | if(c == '4') |
jvfausto | 24:d2f234fbc20d | 281 | vDesired = 150; |
jvfausto | 24:d2f234fbc20d | 282 | if(c == '7') |
jvfausto | 24:d2f234fbc20d | 283 | vDesired = -50; |
jvfausto | 24:d2f234fbc20d | 284 | if(c == '6') |
jvfausto | 24:d2f234fbc20d | 285 | vDesired = -100; |
jvfausto | 24:d2f234fbc20d | 286 | if(c == 'r') |
jvfausto | 24:d2f234fbc20d | 287 | { |
jvfausto | 24:d2f234fbc20d | 288 | yDesired = 0; |
jvfausto | 25:58ec657a44f2 | 289 | dist_old = 0; |
jvfausto | 24:d2f234fbc20d | 290 | return; |
jvfausto | 24:d2f234fbc20d | 291 | } |
jvfausto | 24:d2f234fbc20d | 292 | if(vDesired >= 0) |
jvfausto | 24:d2f234fbc20d | 293 | { |
jvfausto | 25:58ec657a44f2 | 294 | PIDVelosity.SetTunings(.000004,0, 0.00); // Sets the constants for P and D |
jvfausto | 25:58ec657a44f2 | 295 | PIDVelosity.SetOutputLimits(-.002, .002); // Limits to the differnce between def and low |
jvfausto | 24:d2f234fbc20d | 296 | } |
jvfausto | 24:d2f234fbc20d | 297 | else |
jvfausto | 24:d2f234fbc20d | 298 | { |
jvfausto | 24:d2f234fbc20d | 299 | PIDVelosity.SetTunings(.000015,0, 0.00); // Sets the constants for P and D |
jvfausto | 24:d2f234fbc20d | 300 | PIDVelosity.SetOutputLimits(-.0005, .0005); // Limits to the differnce between def and low |
jvfausto | 24:d2f234fbc20d | 301 | } |
jvfausto | 24:d2f234fbc20d | 302 | if(temporV >= 1) |
jvfausto | 24:d2f234fbc20d | 303 | temporV = 1; |
jvfausto | 24:d2f234fbc20d | 304 | vIn = curr_vel*100; |
jvfausto | 24:d2f234fbc20d | 305 | vInS = curr_vel-curr_velS; |
jvfausto | 24:d2f234fbc20d | 306 | PIDVelosity.Compute(); |
jvfausto | 24:d2f234fbc20d | 307 | PIDSlaveV.Compute(); |
jvfausto | 24:d2f234fbc20d | 308 | temporV += vOut; |
jvfausto | 24:d2f234fbc20d | 309 | temporS += vOutS; |
jvfausto | 24:d2f234fbc20d | 310 | x->write(temporV); |
jvfausto | 24:d2f234fbc20d | 311 | y->write(temporS); |
jvfausto | 25:58ec657a44f2 | 312 | //out->printf("Velosity: %f, Velosity2: %f, temporV %f, temporS %f\r\n", curr_vel, curr_velS, temporV, temporS); |
jvfausto | 25:58ec657a44f2 | 313 | Wheelchair::odomMsg(); |
jvfausto | 24:d2f234fbc20d | 314 | wait(.01); |
jvfausto | 24:d2f234fbc20d | 315 | } |
jvfausto | 24:d2f234fbc20d | 316 | } |
jvfausto | 25:58ec657a44f2 | 317 | void Wheelchair::odomMsg(){ |
jvfausto | 25:58ec657a44f2 | 318 | double dist_new = curr_pos; |
jvfausto | 25:58ec657a44f2 | 319 | double dist = dist_new-dist_old; |
jvfausto | 25:58ec657a44f2 | 320 | double temp_x = dist*sin(z_angular*3.14159/180); |
jvfausto | 25:58ec657a44f2 | 321 | double temp_y = dist*cos(z_angular*3.14159/180); |
jvfausto | 25:58ec657a44f2 | 322 | |
jvfausto | 25:58ec657a44f2 | 323 | x_position += temp_x; |
jvfausto | 25:58ec657a44f2 | 324 | y_position += temp_y; |
jvfausto | 25:58ec657a44f2 | 325 | |
jvfausto | 25:58ec657a44f2 | 326 | dist_old = dist_new; |
jvfausto | 25:58ec657a44f2 | 327 | } |
jvfausto | 25:58ec657a44f2 | 328 | void Wheelchair::showOdom(){ |
jvfausto | 25:58ec657a44f2 | 329 | out->printf("x %f, y %f, angle %f", x_position, y_position, z_angular); |
jvfausto | 25:58ec657a44f2 | 330 | } |
ryanlin97 | 12:921488918749 | 331 | float Wheelchair::getDistance() { |
ryanlin97 | 12:921488918749 | 332 | return wheel->getDistance(Diameter); |
ryanlin97 | 12:921488918749 | 333 | } |
ryanlin97 | 12:921488918749 | 334 | |
ryanlin97 | 12:921488918749 | 335 | void Wheelchair::resetDistance(){ |
ryanlin97 | 12:921488918749 | 336 | wheel->reset(); |
ryanlin97 | 12:921488918749 | 337 | } |
jvfausto | 23:8d11d953ceeb | 338 | /*Predetermined paths For Demmo*/ |
jvfausto | 19:71a6621ee5c3 | 339 | void Wheelchair::desk() { |
jvfausto | 19:71a6621ee5c3 | 340 | Wheelchair::pid_forward(5461); |
jvfausto | 19:71a6621ee5c3 | 341 | Wheelchair::pid_right(87); |
jvfausto | 19:71a6621ee5c3 | 342 | Wheelchair::pid_forward(3658); |
jvfausto | 19:71a6621ee5c3 | 343 | Wheelchair::pid_right(87); |
jvfausto | 19:71a6621ee5c3 | 344 | Wheelchair::pid_forward(3658); |
jvfausto | 19:71a6621ee5c3 | 345 | } |
jvfausto | 25:58ec657a44f2 | 346 | |
jvfausto | 19:71a6621ee5c3 | 347 | void Wheelchair::kitchen() { |
jvfausto | 19:71a6621ee5c3 | 348 | Wheelchair::pid_forward(5461); |
jvfausto | 20:f42db4ae16f0 | 349 | Wheelchair::pid_right(87); |
jvfausto | 19:71a6621ee5c3 | 350 | Wheelchair::pid_forward(3658); |
jvfausto | 19:71a6621ee5c3 | 351 | Wheelchair::pid_left(90); |
jvfausto | 19:71a6621ee5c3 | 352 | Wheelchair::pid_forward(305); |
jvfausto | 19:71a6621ee5c3 | 353 | } |
jvfausto | 25:58ec657a44f2 | 354 | |
jvfausto | 19:71a6621ee5c3 | 355 | void Wheelchair::desk_to_kitchen(){ |
jvfausto | 19:71a6621ee5c3 | 356 | Wheelchair::pid_right(180); |
jvfausto | 19:71a6621ee5c3 | 357 | Wheelchair::pid_forward(3700); |
jvfausto | 19:71a6621ee5c3 | 358 | } |
jvfausto | 25:58ec657a44f2 | 359 | |
jvfausto | 25:58ec657a44f2 | 360 |