Numero Uno / Mbed 2 deprecated TheProgram

Dependencies:   EMG HIDScope PID QEI mbed TextLCD

Committer:
ewoud
Date:
Tue Oct 13 18:06:20 2015 +0000
Revision:
1:dafb63606b66
Parent:
0:ca94aa9bf823
Child:
3:70f78cfc0f25
cleared up comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ewoud 0:ca94aa9bf823 1 //****************************************************************************/
ewoud 0:ca94aa9bf823 2 // Includes
ewoud 0:ca94aa9bf823 3 #include "mbed.h"
ewoud 0:ca94aa9bf823 4 #include "PID.h"
ewoud 0:ca94aa9bf823 5 #include "QEI.h"
ewoud 0:ca94aa9bf823 6 #include "HIDScope.h"
ewoud 0:ca94aa9bf823 7 #include "biquadFilter.h"
ewoud 0:ca94aa9bf823 8 #include "inits.h" // all globals, pin and variable initialization
ewoud 0:ca94aa9bf823 9 #include "EMG.h"
ewoud 0:ca94aa9bf823 10
ewoud 0:ca94aa9bf823 11 void setGoFlag(){
ewoud 0:ca94aa9bf823 12 if (goFlag==true){
ewoud 0:ca94aa9bf823 13 //pc.printf("rate too high, error in setGoFlag \n\r");
ewoud 0:ca94aa9bf823 14 // flag is already set true: code too slow or frequency too high
ewoud 0:ca94aa9bf823 15 }
ewoud 0:ca94aa9bf823 16 goFlag=true;
ewoud 0:ca94aa9bf823 17 }
ewoud 0:ca94aa9bf823 18
ewoud 0:ca94aa9bf823 19 void systemStart(){
ewoud 0:ca94aa9bf823 20 systemOn=true;
ewoud 0:ca94aa9bf823 21 }
ewoud 0:ca94aa9bf823 22 void systemStop(){
ewoud 0:ca94aa9bf823 23 systemOn=false;
ewoud 0:ca94aa9bf823 24 pc.printf("system stopped\n\r");
ewoud 0:ca94aa9bf823 25 }
ewoud 1:dafb63606b66 26
ewoud 0:ca94aa9bf823 27 int main() {
ewoud 0:ca94aa9bf823 28
ewoud 0:ca94aa9bf823 29 // initialize (defined in inits.h)
ewoud 0:ca94aa9bf823 30 initMotors();
ewoud 0:ca94aa9bf823 31 initPIDs();
ewoud 0:ca94aa9bf823 32
ewoud 0:ca94aa9bf823 33 outofboundsled=1;
ewoud 0:ca94aa9bf823 34 edgeleft.mode(PullUp);
ewoud 0:ca94aa9bf823 35 edgeright.mode(PullUp);
ewoud 1:dafb63606b66 36
ewoud 1:dafb63606b66 37 // interrupts
ewoud 0:ca94aa9bf823 38 motorControlTicker.attach(&setGoFlag, RATE);
ewoud 0:ca94aa9bf823 39 cali_button.rise(&calibrate);
ewoud 0:ca94aa9bf823 40 startButton.rise(&systemStart);
ewoud 0:ca94aa9bf823 41 stopButton.rise(&systemStop);
ewoud 0:ca94aa9bf823 42
ewoud 0:ca94aa9bf823 43 while (true){
ewoud 0:ca94aa9bf823 44
ewoud 0:ca94aa9bf823 45 if (goFlag==true && systemOn==true){
ewoud 0:ca94aa9bf823 46 /*********** Contents *************/
ewoud 0:ca94aa9bf823 47 // 1) get emg signal
ewoud 0:ca94aa9bf823 48 // 2) calculate desired angle velocities
ewoud 0:ca94aa9bf823 49 // 3) calculate current angle velocities
ewoud 1:dafb63606b66 50 // 4) pid controller output
ewoud 0:ca94aa9bf823 51 // 5) user output
ewoud 0:ca94aa9bf823 52
ewoud 1:dafb63606b66 53 // **************
ewoud 0:ca94aa9bf823 54 // get emg signal
ewoud 0:ca94aa9bf823 55 sample_filter();
ewoud 0:ca94aa9bf823 56
ewoud 0:ca94aa9bf823 57 request_pos=y1;
ewoud 0:ca94aa9bf823 58 request_neg=y2;
ewoud 0:ca94aa9bf823 59
ewoud 1:dafb63606b66 60 // determine if forward or backward signal is stronger
ewoud 0:ca94aa9bf823 61 if (request_pos > request_neg){
ewoud 0:ca94aa9bf823 62 request = request_pos;
ewoud 0:ca94aa9bf823 63 }
ewoud 0:ca94aa9bf823 64 else {
ewoud 0:ca94aa9bf823 65 request = -request_neg;
ewoud 0:ca94aa9bf823 66 }
ewoud 0:ca94aa9bf823 67 request=request*maxspeed; // turn [-1, 1] into [-max cm/s, max cm/s]
ewoud 1:dafb63606b66 68
ewoud 1:dafb63606b66 69 // ***************
ewoud 0:ca94aa9bf823 70 // calculate required rotational velocity from the requested horizontal velocity
ewoud 1:dafb63606b66 71 // first get the current position from the motor encoders and make them start at 45 degree.
ewoud 1:dafb63606b66 72 leftAngle=(leftQei.getPulses()/round)*360+45;
ewoud 0:ca94aa9bf823 73 rightAngle=(rightQei.getPulses()/round)*360+45;
ewoud 0:ca94aa9bf823 74
ewoud 1:dafb63606b66 75 // trigonometry to get xy position from angles (cm)
ewoud 0:ca94aa9bf823 76 currentX = (tan(rightAngle*M_PI/180)*l)/(tan(leftAngle*M_PI/180)+tan(rightAngle*M_PI/180));
ewoud 0:ca94aa9bf823 77 currentY = tan(leftAngle*M_PI/180)*currentX;
ewoud 0:ca94aa9bf823 78
ewoud 0:ca94aa9bf823 79 // restrict motion if edges are touched
ewoud 0:ca94aa9bf823 80 if (edgeleft==0){
ewoud 0:ca94aa9bf823 81 if (request < 0){request=0; pc.printf("hit left edge \n\r");}
ewoud 0:ca94aa9bf823 82 }
ewoud 0:ca94aa9bf823 83 if (edgeright==0){
ewoud 0:ca94aa9bf823 84 if (request > 0){request=0; pc.printf("hit right edge \n\r");}
ewoud 0:ca94aa9bf823 85 }
ewoud 0:ca94aa9bf823 86
ewoud 1:dafb63606b66 87 // calculate the position to go to according the the current position + the distance that should be covered in this timestep (cm)
ewoud 0:ca94aa9bf823 88 toX=currentX+request*RATE;
ewoud 0:ca94aa9bf823 89 toY=currentY+0*RATE; // should be vertical request*RATE
ewoud 0:ca94aa9bf823 90
ewoud 1:dafb63606b66 91 // trigonometry to get angles from xy new position (degree)
ewoud 0:ca94aa9bf823 92 toLeftAngle = atan(toY/toX)*180/M_PI;
ewoud 0:ca94aa9bf823 93 toRightAngle = atan(toY/(l-toX))*180/M_PI;
ewoud 0:ca94aa9bf823 94
ewoud 0:ca94aa9bf823 95 // restrict motion if angles out-of-bound
ewoud 0:ca94aa9bf823 96 if (toLeftAngle < 0){toLeftAngle=0; pc.printf("out of bounds \n\r");}
ewoud 0:ca94aa9bf823 97 if (toLeftAngle > 90){toLeftAngle=90; pc.printf("out of bounds \n\r");}
ewoud 0:ca94aa9bf823 98 if (toRightAngle < 0){toRightAngle=0; pc.printf("out of bounds \n\r");}
ewoud 0:ca94aa9bf823 99 if (toRightAngle > 90){toRightAngle=90; pc.printf("out of bounds \n\r");}
ewoud 0:ca94aa9bf823 100
ewoud 1:dafb63606b66 101 // restrict motion if position is out of reach of the arms
ewoud 0:ca94aa9bf823 102 //if (sqrt(pow(currentX,2)+pow(currentY,2)) > armlength){ // too far from left arm
ewoud 0:ca94aa9bf823 103 //return 0;
ewoud 0:ca94aa9bf823 104 //}
ewoud 0:ca94aa9bf823 105 //if (sqrt(pow(l-currentX,2)+pow(currentY,2)) > armlength){ // too far from right arm
ewoud 0:ca94aa9bf823 106 //return 0;
ewoud 0:ca94aa9bf823 107 //}
ewoud 0:ca94aa9bf823 108
ewoud 1:dafb63606b66 109 // calculate the neccesairy velocities to make these angles happen (degree/sec)
ewoud 1:dafb63606b66 110 leftRequest=(toLeftAngle-leftAngle)/RATE;
ewoud 1:dafb63606b66 111 rightRequest=(toRightAngle-rightAngle)/RATE;
ewoud 0:ca94aa9bf823 112
ewoud 0:ca94aa9bf823 113 // set the setpoint to the pid controller
ewoud 0:ca94aa9bf823 114 leftController.setSetPoint(leftRequest);
ewoud 0:ca94aa9bf823 115 rightController.setSetPoint(rightRequest);
ewoud 0:ca94aa9bf823 116
ewoud 1:dafb63606b66 117 // **************
ewoud 0:ca94aa9bf823 118 // Velocity calculation
ewoud 1:dafb63606b66 119 // left, differentiate from encoders
ewoud 1:dafb63606b66 120 leftPulses = leftQei.getPulses()/round*360; // (degree)
ewoud 1:dafb63606b66 121 leftVelocity = ((float)(leftPulses - leftPrevPulses))/ RATE; // (degree/sec)
ewoud 0:ca94aa9bf823 122 //leftVelocity = leftVelocityfilter.step(leftVelocity);
ewoud 0:ca94aa9bf823 123 leftPrevPulses = leftPulses;
ewoud 1:dafb63606b66 124 leftController.setProcessValue(leftVelocity); // set PID process value
ewoud 0:ca94aa9bf823 125
ewoud 0:ca94aa9bf823 126 // right
ewoud 1:dafb63606b66 127 rightPulses = rightQei.getPulses()/round*360; // (degree)
ewoud 1:dafb63606b66 128 rightVelocity = ((float)(rightPulses - rightPrevPulses))/ RATE; // (degree/sec)
ewoud 0:ca94aa9bf823 129 //rightVelocity = rightVelocityfilter.step(rightVelocity);
ewoud 0:ca94aa9bf823 130 rightPrevPulses = rightPulses;
ewoud 1:dafb63606b66 131 rightController.setProcessValue(rightVelocity); // set PID process value
ewoud 0:ca94aa9bf823 132
ewoud 0:ca94aa9bf823 133
ewoud 1:dafb63606b66 134 // **************
ewoud 0:ca94aa9bf823 135 // PID control output
ewoud 0:ca94aa9bf823 136 // left
ewoud 0:ca94aa9bf823 137
ewoud 0:ca94aa9bf823 138 leftPwmDuty = leftController.compute();
ewoud 1:dafb63606b66 139 if (leftPwmDuty < 0){ // put motor in reverse when we have a negative value
ewoud 0:ca94aa9bf823 140 leftDirection = 0;
ewoud 0:ca94aa9bf823 141 leftMotor = -leftPwmDuty;
ewoud 0:ca94aa9bf823 142 }
ewoud 0:ca94aa9bf823 143 else {
ewoud 0:ca94aa9bf823 144 leftDirection = 1;
ewoud 0:ca94aa9bf823 145 leftMotor = leftPwmDuty;
ewoud 0:ca94aa9bf823 146 }
ewoud 0:ca94aa9bf823 147
ewoud 0:ca94aa9bf823 148 // right
ewoud 0:ca94aa9bf823 149 rightPwmDuty = rightController.compute();
ewoud 1:dafb63606b66 150 if (rightPwmDuty < 0){ // put motor in reverse when we have a negative value
ewoud 0:ca94aa9bf823 151 rightDirection = 1;
ewoud 0:ca94aa9bf823 152 rightMotor = -rightPwmDuty;
ewoud 0:ca94aa9bf823 153 }
ewoud 0:ca94aa9bf823 154 else {
ewoud 0:ca94aa9bf823 155 rightDirection = 0;
ewoud 0:ca94aa9bf823 156 rightMotor = rightPwmDuty;
ewoud 0:ca94aa9bf823 157 }
ewoud 0:ca94aa9bf823 158
ewoud 1:dafb63606b66 159 // ***************
ewoud 0:ca94aa9bf823 160 // User feedback
ewoud 0:ca94aa9bf823 161 scope.set(0, leftRequest);
ewoud 0:ca94aa9bf823 162 scope.set(1, leftPwmDuty);
ewoud 0:ca94aa9bf823 163 scope.set(2, leftVelocity);
ewoud 0:ca94aa9bf823 164 scope.set(3, currentX);
ewoud 0:ca94aa9bf823 165 scope.set(4, currentY);
ewoud 0:ca94aa9bf823 166 scope.send();
ewoud 1:dafb63606b66 167
ewoud 0:ca94aa9bf823 168 goFlag=false;
ewoud 0:ca94aa9bf823 169 }
ewoud 0:ca94aa9bf823 170 if(systemOn==false)
ewoud 0:ca94aa9bf823 171 {
ewoud 0:ca94aa9bf823 172 leftMotor=0;
ewoud 0:ca94aa9bf823 173 rightMotor=0;
ewoud 0:ca94aa9bf823 174 }
ewoud 0:ca94aa9bf823 175 }
ewoud 0:ca94aa9bf823 176
ewoud 0:ca94aa9bf823 177 }