Odometry communication

Dependencies:   PID QEI chair_BNO055 ros_lib_kinetic3

Dependents:   wheelchaircontrolrealtime1

Fork of wheelchaircontrol by Jesus Fausto

Committer:
jvfausto
Date:
Sat Nov 03 20:35:30 2018 +0000
Revision:
29:a6a1ccf14c63
Parent:
28:889fc0e5f8c4
Now with twist and Odom messages

Who changed what in which revision?

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