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