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