Eindelijk!!!!!

Dependencies:   AnglePosition2 Encoder FastPWM HIDScope MODSERIAL Movement PIDController Servo SignalNumber biquadFilter mbed

Fork of Robot_control by Peter Knoben

Committer:
DBerendsen
Date:
Thu Nov 02 13:23:01 2017 +0000
Revision:
13:d22b72b0c3a9
Parent:
12:0765ea2c4c85
Eindelijk!!!!!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
peterknoben 3:c768a37620c9 1 #include "MODSERIAL.h"
peterknoben 1:406519ff0f17 2 #include "AnglePosition.h"
peterknoben 1:406519ff0f17 3 #include "PIDControl.h"
peterknoben 3:c768a37620c9 4 #include "BiQuad.h"
peterknoben 3:c768a37620c9 5 #include "signalnumber.h"
peterknoben 4:e15fc329b88b 6 #include "Movement.h"
DBerendsen 0:5f4bc2d63814 7 #include "mbed.h"
DBerendsen 0:5f4bc2d63814 8 #include "encoder.h"
DBerendsen 0:5f4bc2d63814 9 #include "Servo.h"
peterknoben 1:406519ff0f17 10 #include "FastPWM.h"
DBerendsen 12:0765ea2c4c85 11 //#include "HIDScope.h"
DBerendsen 0:5f4bc2d63814 12
peterknoben 4:e15fc329b88b 13 //This code is for Mbed 2
DBerendsen 0:5f4bc2d63814 14 //------------------------------------------------------------------------------
DBerendsen 0:5f4bc2d63814 15 //------------------------------------------------------------------------------
DBerendsen 0:5f4bc2d63814 16 //------------------------------------------------------------------------------
peterknoben 3:c768a37620c9 17 MODSERIAL pc(USBTX, USBRX); //Establish connection
peterknoben 8:24f6744d47b2 18 Ticker MyControllerTicker; //Declare Ticker for Motors
DBerendsen 11:28b5ec12a4b9 19 Ticker MyTickerServo; //Declare Ticker Servo control
peterknoben 3:c768a37620c9 20 Ticker MyTickerMean; //Declare Ticker Signalprocessing
peterknoben 3:c768a37620c9 21
peterknoben 8:24f6744d47b2 22 InterruptIn But1(PTA4); //Declare button for min calibration
peterknoben 8:24f6744d47b2 23 InterruptIn Mode(PTC6); //Declare button for max calibration
peterknoben 3:c768a37620c9 24
peterknoben 1:406519ff0f17 25 AnglePosition Angle; //Declare Angle calculater
peterknoben 1:406519ff0f17 26 PIDControl PID; //Declare PID Controller
peterknoben 4:e15fc329b88b 27 SignalNumber SignalLeft; //Declare Signal determiner for Left arm
peterknoben 4:e15fc329b88b 28 SignalNumber SignalRight; //Declare Signal determiner for Right arm
peterknoben 5:b4abbd3c513c 29 SignalNumber SignalMode;
peterknoben 4:e15fc329b88b 30 Movement MoveLeft; //Declare Movement determiner
peterknoben 4:e15fc329b88b 31 Movement MoveRight;
peterknoben 3:c768a37620c9 32
peterknoben 4:e15fc329b88b 33 AnalogIn emg0( A0 ); //Set Inputpin for EMG 0 signal Left
peterknoben 4:e15fc329b88b 34 AnalogIn emg2( A2 ); //Set Inputpin for EMG 2 signal Right
peterknoben 4:e15fc329b88b 35 AnalogIn emg4( A4 ); //Set Inputpin for EMG 4 signal Mode
peterknoben 5:b4abbd3c513c 36 DigitalOut Up( D9 ); //Set digital in for mode selection
peterknoben 5:b4abbd3c513c 37 DigitalOut Down( D8 );
peterknoben 4:e15fc329b88b 38 DigitalOut Led_red(LED_RED);
peterknoben 4:e15fc329b88b 39 DigitalOut Led_green(LED_GREEN);
peterknoben 4:e15fc329b88b 40 DigitalOut Led_blue(LED_BLUE);
DBerendsen 12:0765ea2c4c85 41 DigitalOut Led_calibration(D2);
peterknoben 1:406519ff0f17 42
peterknoben 1:406519ff0f17 43 const float CONTROLLER_TS = 0.02; //Motor frequency
peterknoben 4:e15fc329b88b 44 const float MEAN_TS = 0.002; //Filter frequency
peterknoben 4:e15fc329b88b 45
peterknoben 3:c768a37620c9 46 //------------------------------------------------------------------------------
peterknoben 3:c768a37620c9 47 //-----------------------------EMG Signals--------------------------------------
peterknoben 3:c768a37620c9 48 //------------------------------------------------------------------------------
peterknoben 4:e15fc329b88b 49 // Filtering the signal and getting the usefull information out of it.
DBerendsen 10:614050a50c2f 50 const int n = 500; //Window size for the mean value, also adjust in signalnumber.cpp
peterknoben 8:24f6744d47b2 51 const int action =100; //Number of same mean values to change the signalnumber
DBerendsen 10:614050a50c2f 52 const int m = 500; //Number of samples for calibration
peterknoben 5:b4abbd3c513c 53 int CaseLeft, CaseRight, CaseMode; //Strength of the muscles
peterknoben 5:b4abbd3c513c 54 float emg_offsetLeft, emg_offsetmaxLeft; //Calibration offsets from zero to one for the left arm
peterknoben 5:b4abbd3c513c 55 float emg_offsetRight, emg_offsetmaxRight; //Calibration offsets from zero to one for the right arm
peterknoben 5:b4abbd3c513c 56 float emg_offsetMode, emg_offsetmaxMode;
peterknoben 5:b4abbd3c513c 57 float mean_value_left, mean_value_right, mean_value_mode; //Mean value of the filtered system
peterknoben 5:b4abbd3c513c 58 float kLeft, kRight, kMode; //Scaling factors
peterknoben 6:edd30e11f3c7 59 float Signal_filteredLeft, Signal_filteredRight, Signal_filteredMode;
peterknoben 3:c768a37620c9 60
peterknoben 4:e15fc329b88b 61 //BiQuad filter variables
peterknoben 6:edd30e11f3c7 62 BiQuad LP1L( 0.6389437261127493, 1.2778874522254986, 0.6389437261127493, 1.1429772843080919, 0.4127976201429053 ); //Lowpass filter Biquad
peterknoben 6:edd30e11f3c7 63 BiQuad HP2L( 0.8370879899975344, -1.6741759799950688, 0.8370879899975344, -1.6474576182593796, 0.7008943417307579 ); //Highpass filter Biquad
peterknoben 6:edd30e11f3c7 64 BiQuad NO3L( 0.7063988100714527, -1.1429772843080923, 0.7063988100714527, -1.1429772843080923, 0.41279762014290533); //Notch filter Biquad
peterknoben 6:edd30e11f3c7 65 BiQuad LP1R( 0.6389437261127493, 1.2778874522254986, 0.6389437261127493, 1.1429772843080919, 0.4127976201429053 ); //Lowpass filter Biquad
peterknoben 6:edd30e11f3c7 66 BiQuad HP2R( 0.8370879899975344, -1.6741759799950688, 0.8370879899975344, -1.6474576182593796, 0.7008943417307579 ); //Highpass filter Biquad
peterknoben 6:edd30e11f3c7 67 BiQuad NO3R( 0.7063988100714527, -1.1429772843080923, 0.7063988100714527, -1.1429772843080923, 0.41279762014290533); //Notch filter Biquad
peterknoben 6:edd30e11f3c7 68 BiQuad LP1M( 0.6389437261127493, 1.2778874522254986, 0.6389437261127493, 1.1429772843080919, 0.4127976201429053 ); //Lowpass filter Biquad
peterknoben 6:edd30e11f3c7 69 BiQuad HP2M( 0.8370879899975344, -1.6741759799950688, 0.8370879899975344, -1.6474576182593796, 0.7008943417307579 ); //Highpass filter Biquad
peterknoben 6:edd30e11f3c7 70 BiQuad NO3M( 0.7063988100714527, -1.1429772843080923, 0.7063988100714527, -1.1429772843080923, 0.41279762014290533); //Notch filter Biquad
peterknoben 6:edd30e11f3c7 71 BiQuadChain BiQuad_filter_Left;
peterknoben 6:edd30e11f3c7 72 BiQuadChain BiQuad_filter_Right;
peterknoben 6:edd30e11f3c7 73 BiQuadChain BiQuad_filter_Mode;
peterknoben 3:c768a37620c9 74
peterknoben 7:105e3ae0fb19 75 float FilterLeft(float input){
peterknoben 7:105e3ae0fb19 76 float Signal_filtered= BiQuad_filter_Left.step(input);
peterknoben 7:105e3ae0fb19 77 return Signal_filtered;
peterknoben 7:105e3ae0fb19 78 }
peterknoben 7:105e3ae0fb19 79 float FilterRight(float input){
peterknoben 7:105e3ae0fb19 80 float Signal_filtered= BiQuad_filter_Right.step(input);
peterknoben 7:105e3ae0fb19 81 return Signal_filtered;
peterknoben 7:105e3ae0fb19 82 }
peterknoben 7:105e3ae0fb19 83 float FilterMode(float input){
peterknoben 7:105e3ae0fb19 84 float Signal_filtered= BiQuad_filter_Mode.step(input);
peterknoben 7:105e3ae0fb19 85 return Signal_filtered;
peterknoben 7:105e3ae0fb19 86 }
peterknoben 5:b4abbd3c513c 87
peterknoben 5:b4abbd3c513c 88 //Calibration-------------------------------------------------------------------
peterknoben 4:e15fc329b88b 89 void setled(){
DBerendsen 12:0765ea2c4c85 90 Led_red=1; Led_green=1; Led_blue=1; Led_calibration=0;
peterknoben 5:b4abbd3c513c 91 }
peterknoben 8:24f6744d47b2 92
peterknoben 5:b4abbd3c513c 93 // One calibration
peterknoben 5:b4abbd3c513c 94 void maxcalibration(){
peterknoben 5:b4abbd3c513c 95 pc.printf("start max calibration \r\n");
peterknoben 8:24f6744d47b2 96 emg_offsetmaxLeft = SignalLeft.calibrate(m, FilterLeft(emg0));
peterknoben 8:24f6744d47b2 97 emg_offsetmaxRight = SignalRight.calibrate(m, FilterRight(emg2));
peterknoben 8:24f6744d47b2 98 emg_offsetmaxMode = SignalMode.calibrate(m, FilterMode(emg4));
peterknoben 5:b4abbd3c513c 99 kLeft = 1/emg_offsetmaxLeft;
peterknoben 5:b4abbd3c513c 100 kRight = 1/emg_offsetmaxRight;
peterknoben 5:b4abbd3c513c 101 kMode = 1/emg_offsetmaxMode;
DBerendsen 12:0765ea2c4c85 102 Led_calibration = 1; //Set led to Green
peterknoben 3:c768a37620c9 103 }
peterknoben 3:c768a37620c9 104
peterknoben 5:b4abbd3c513c 105 //------------------------------------------------------------------------------
peterknoben 5:b4abbd3c513c 106 //---------------------------------Servo----------------------------------------
peterknoben 5:b4abbd3c513c 107 //------------------------------------------------------------------------------
DBerendsen 11:28b5ec12a4b9 108 void up(){
DBerendsen 11:28b5ec12a4b9 109 Up = 1;
DBerendsen 11:28b5ec12a4b9 110 Up = 0;
DBerendsen 11:28b5ec12a4b9 111 Up = 1;
DBerendsen 11:28b5ec12a4b9 112 }
DBerendsen 11:28b5ec12a4b9 113 void down(){
DBerendsen 11:28b5ec12a4b9 114 Down = 1;
DBerendsen 11:28b5ec12a4b9 115 Down = 0;
DBerendsen 11:28b5ec12a4b9 116 Down = 1;
DBerendsen 11:28b5ec12a4b9 117 }
DBerendsen 11:28b5ec12a4b9 118
DBerendsen 11:28b5ec12a4b9 119 int mode =0;
peterknoben 5:b4abbd3c513c 120 void servo(){
DBerendsen 11:28b5ec12a4b9 121 if(mode==3||mode==6){
DBerendsen 11:28b5ec12a4b9 122 Signal_filteredLeft = fabs(FilterLeft(emg0)*kLeft);//*kLeft
DBerendsen 11:28b5ec12a4b9 123 CaseLeft = SignalLeft.getnumberLeft(n, action, Signal_filteredLeft);
DBerendsen 11:28b5ec12a4b9 124 Signal_filteredRight = fabs(FilterRight(emg2)*kRight);//*kRight
DBerendsen 11:28b5ec12a4b9 125 CaseRight = SignalRight.getnumberRight(n, action, Signal_filteredRight);
DBerendsen 11:28b5ec12a4b9 126 if (CaseLeft == 2){
DBerendsen 11:28b5ec12a4b9 127 up();
DBerendsen 11:28b5ec12a4b9 128 }
DBerendsen 11:28b5ec12a4b9 129 else if (CaseRight == 2){
DBerendsen 11:28b5ec12a4b9 130 down();
DBerendsen 11:28b5ec12a4b9 131 }
peterknoben 5:b4abbd3c513c 132 }
peterknoben 5:b4abbd3c513c 133 }
DBerendsen 10:614050a50c2f 134
peterknoben 5:b4abbd3c513c 135
peterknoben 5:b4abbd3c513c 136 //------------------------------------------------------------------------------
peterknoben 5:b4abbd3c513c 137 //---------------------------Mode selection-------------------------------------
peterknoben 5:b4abbd3c513c 138 //------------------------------------------------------------------------------
peterknoben 5:b4abbd3c513c 139
peterknoben 5:b4abbd3c513c 140 //Recieving mode selection from EMG mode signal
peterknoben 5:b4abbd3c513c 141 void mode_selection(){
peterknoben 5:b4abbd3c513c 142 if(mode ==6){
peterknoben 5:b4abbd3c513c 143 mode=1;
peterknoben 5:b4abbd3c513c 144 }
peterknoben 5:b4abbd3c513c 145 else{
peterknoben 5:b4abbd3c513c 146 mode++;
DBerendsen 11:28b5ec12a4b9 147 }
peterknoben 5:b4abbd3c513c 148 pc.printf("\r\n mode = %i \r\n", mode);
DBerendsen 10:614050a50c2f 149
DBerendsen 10:614050a50c2f 150
DBerendsen 12:0765ea2c4c85 151 if((mode==3) || (mode==6)) {
DBerendsen 10:614050a50c2f 152 Led_red = 0;
DBerendsen 10:614050a50c2f 153 Led_green = 0;
DBerendsen 10:614050a50c2f 154 Led_blue = 0;
DBerendsen 10:614050a50c2f 155 }
DBerendsen 10:614050a50c2f 156 else if (mode==1) {
DBerendsen 10:614050a50c2f 157 Led_red = 0;
DBerendsen 10:614050a50c2f 158 Led_green = 1;
DBerendsen 10:614050a50c2f 159 Led_blue = 1;
DBerendsen 10:614050a50c2f 160 }
DBerendsen 10:614050a50c2f 161 else if (mode==2) {
DBerendsen 10:614050a50c2f 162 Led_red = 1;
DBerendsen 10:614050a50c2f 163 Led_green = 0;
DBerendsen 10:614050a50c2f 164 Led_blue = 1;
DBerendsen 10:614050a50c2f 165 }
DBerendsen 10:614050a50c2f 166 else if (mode==4) {
DBerendsen 10:614050a50c2f 167 Led_red = 1;
DBerendsen 10:614050a50c2f 168 Led_green = 1;
DBerendsen 10:614050a50c2f 169 Led_blue = 0;
DBerendsen 10:614050a50c2f 170 }
DBerendsen 10:614050a50c2f 171 else if (mode==5) {
DBerendsen 10:614050a50c2f 172 Led_red = 0;
DBerendsen 10:614050a50c2f 173 Led_green = 0;
DBerendsen 10:614050a50c2f 174 Led_blue = 1;
DBerendsen 12:0765ea2c4c85 175 }
DBerendsen 10:614050a50c2f 176
peterknoben 5:b4abbd3c513c 177 }
peterknoben 5:b4abbd3c513c 178
peterknoben 5:b4abbd3c513c 179 // Control mode selection-------------------------------------------------------
peterknoben 5:b4abbd3c513c 180
peterknoben 5:b4abbd3c513c 181 //Determine the signalnumbers (i.e. speed) based on the strenght of the EMG signals
peterknoben 3:c768a37620c9 182 void signalnumber(){
peterknoben 4:e15fc329b88b 183 //Left
peterknoben 8:24f6744d47b2 184 Signal_filteredLeft = fabs(FilterLeft(emg0)*kLeft);//*kLeft
peterknoben 8:24f6744d47b2 185 // mean_value_left = SignalLeft.getmeanLeft(n, Signal_filteredLeft);
peterknoben 8:24f6744d47b2 186 mean_value_left = SignalLeft.getmeanLeft(n, fabs(FilterLeft(emg0.read())*kLeft));
peterknoben 8:24f6744d47b2 187 CaseLeft = SignalLeft.getnumberLeft(n, action, Signal_filteredLeft);
peterknoben 4:e15fc329b88b 188 //Right
peterknoben 8:24f6744d47b2 189 Signal_filteredRight = fabs(FilterRight(emg2)*kRight);//*kRight
peterknoben 8:24f6744d47b2 190 // mean_value_right = SignalRight.getmeanRight(n, Signal_filteredRight);
peterknoben 8:24f6744d47b2 191 mean_value_right = SignalRight.getmeanRight(n, fabs(FilterRight(emg2.read())*kRight));
peterknoben 8:24f6744d47b2 192 CaseRight = SignalRight.getnumberRight(n, action, Signal_filteredRight);
peterknoben 5:b4abbd3c513c 193 //Mode
peterknoben 8:24f6744d47b2 194 /*
peterknoben 8:24f6744d47b2 195 Signal_filteredMode = fabs(FilterMode(emg4));//*kMode
peterknoben 8:24f6744d47b2 196 mean_value_mode = SignalMode.getmeanMode(n, Signal_filteredMode);
peterknoben 8:24f6744d47b2 197 CaseMode = SignalMode.getnumberMode(n, action, Signal_filteredMode);
peterknoben 8:24f6744d47b2 198 if(CaseMode >= 3){
peterknoben 5:b4abbd3c513c 199 milli ++;
peterknoben 5:b4abbd3c513c 200 if(milli>=150){
peterknoben 5:b4abbd3c513c 201 mode_selection();
peterknoben 5:b4abbd3c513c 202 milli=0;
peterknoben 5:b4abbd3c513c 203 }
peterknoben 5:b4abbd3c513c 204 }
peterknoben 5:b4abbd3c513c 205 else{
peterknoben 5:b4abbd3c513c 206 milli=0;
peterknoben 6:edd30e11f3c7 207 }*/
peterknoben 3:c768a37620c9 208 }
peterknoben 3:c768a37620c9 209
peterknoben 1:406519ff0f17 210 //------------------------------------------------------------------------------
peterknoben 1:406519ff0f17 211 //-------------------------Kinematic Constants----------------------------------
peterknoben 1:406519ff0f17 212 //------------------------------------------------------------------------------
peterknoben 4:e15fc329b88b 213 const double RAD_PER_PULSE = 0.00074799825*2; //Number of radials per pulse from the encoder
peterknoben 4:e15fc329b88b 214 const double PI = 3.14159265358979323846; //Pi
DBerendsen 11:28b5ec12a4b9 215 const float max_rangex = 650.0, max_rangey = 450.0; //Max range on the y axis
peterknoben 8:24f6744d47b2 216 const float x_offset = 156.15, y_offset = -76.97; //Starting positions from the shoulder joint
DBerendsen 10:614050a50c2f 217 const float alpha_offset = -0.4114;
DBerendsen 10:614050a50c2f 218 const float beta_offset = 0.0486;
peterknoben 8:24f6744d47b2 219 const float L1 = 450.0, L2 = 490.0; //Length of the bodies
peterknoben 5:b4abbd3c513c 220
DBerendsen 0:5f4bc2d63814 221 //------------------------------------------------------------------------------
DBerendsen 0:5f4bc2d63814 222 //--------------------------------Motor1----------------------------------------
DBerendsen 0:5f4bc2d63814 223 //------------------------------------------------------------------------------
peterknoben 1:406519ff0f17 224 FastPWM motor1(D5);
DBerendsen 0:5f4bc2d63814 225 DigitalOut motor1DirectionPin(D4);
DBerendsen 0:5f4bc2d63814 226 DigitalIn ENC2A(D12);
DBerendsen 0:5f4bc2d63814 227 DigitalIn ENC2B(D13);
DBerendsen 0:5f4bc2d63814 228 Encoder encoder1(D13,D12);
peterknoben 1:406519ff0f17 229 const float MOTOR1_KP = 40.0;
peterknoben 1:406519ff0f17 230 const float MOTOR1_KI = 0.0;
peterknoben 1:406519ff0f17 231 const float MOTOR1_KD = 15.0;
peterknoben 5:b4abbd3c513c 232 double M1_v1 = 0.0, M1_v2 = 0.0; //Calculation values
DBerendsen 0:5f4bc2d63814 233 const double motor1_gain = 2*PI;
peterknoben 1:406519ff0f17 234 const float M1_N = 0.5;
peterknoben 8:24f6744d47b2 235 float position_math_l =0, position_math_r=0;
DBerendsen 0:5f4bc2d63814 236
DBerendsen 12:0765ea2c4c85 237 void motor1_control(){
peterknoben 8:24f6744d47b2 238 float reference_alpha = Angle.getbeta(max_rangex, max_rangey, x_offset, y_offset, beta_offset, L1, L2, position_math_l, position_math_r);
DBerendsen 0:5f4bc2d63814 239 float position_alpha = RAD_PER_PULSE * encoder1.getPosition();
DBerendsen 0:5f4bc2d63814 240 float error_alpha = reference_alpha-position_alpha;
peterknoben 1:406519ff0f17 241 float magnitude1 = PID.get(error_alpha, MOTOR1_KP, MOTOR1_KI, MOTOR1_KD, CONTROLLER_TS, M1_N, M1_v1, M1_v2) / motor1_gain;
DBerendsen 10:614050a50c2f 242
DBerendsen 10:614050a50c2f 243 //Determine Motor Magnitude
DBerendsen 10:614050a50c2f 244 if (fabs(magnitude1)>1) {
DBerendsen 10:614050a50c2f 245 motor1 = 1;
DBerendsen 10:614050a50c2f 246 }
DBerendsen 10:614050a50c2f 247 else {
DBerendsen 10:614050a50c2f 248 motor1 = fabs(magnitude1);
DBerendsen 10:614050a50c2f 249 }
DBerendsen 10:614050a50c2f 250
DBerendsen 10:614050a50c2f 251
peterknoben 1:406519ff0f17 252 // Determine Motor Direction
peterknoben 1:406519ff0f17 253 if (magnitude1 < 0){
DBerendsen 0:5f4bc2d63814 254 motor1DirectionPin = 1;
DBerendsen 0:5f4bc2d63814 255 }
DBerendsen 0:5f4bc2d63814 256 else{
DBerendsen 0:5f4bc2d63814 257 motor1DirectionPin = 0;
DBerendsen 0:5f4bc2d63814 258 }
DBerendsen 0:5f4bc2d63814 259 }
DBerendsen 0:5f4bc2d63814 260
DBerendsen 0:5f4bc2d63814 261 //------------------------------------------------------------------------------
DBerendsen 0:5f4bc2d63814 262 //--------------------------------Motor2----------------------------------------
DBerendsen 0:5f4bc2d63814 263 //------------------------------------------------------------------------------
peterknoben 1:406519ff0f17 264 FastPWM motor2(D6);
DBerendsen 0:5f4bc2d63814 265 DigitalOut motor2DirectionPin(D7);
DBerendsen 0:5f4bc2d63814 266 DigitalIn ENC1A(D10);
DBerendsen 0:5f4bc2d63814 267 DigitalIn ENC1B(D11);
DBerendsen 0:5f4bc2d63814 268 Encoder encoder2(D10,D11);
peterknoben 1:406519ff0f17 269 const float MOTOR2_KP = 60.0;
peterknoben 1:406519ff0f17 270 const float MOTOR2_KI = 0.0;
peterknoben 1:406519ff0f17 271 const float MOTOR2_KD = 15.0;
peterknoben 5:b4abbd3c513c 272 double M2_v1 = 0.0, M2_v2 = 0.0; //Calculation values
DBerendsen 0:5f4bc2d63814 273 const double motor2_gain = 2*PI;
peterknoben 1:406519ff0f17 274 const float M2_N = 0.5;
DBerendsen 0:5f4bc2d63814 275
DBerendsen 12:0765ea2c4c85 276 void motor2_control(){
peterknoben 8:24f6744d47b2 277 float reference_beta = Angle.getalpha(max_rangex, max_rangey, x_offset, y_offset, alpha_offset, L1, L2, position_math_l, position_math_r);
DBerendsen 0:5f4bc2d63814 278 float position_beta = RAD_PER_PULSE * -encoder2.getPosition();
DBerendsen 0:5f4bc2d63814 279 float error_beta = reference_beta-position_beta;
peterknoben 1:406519ff0f17 280 float magnitude2 = PID.get(error_beta, MOTOR2_KP, MOTOR2_KI, MOTOR2_KD, CONTROLLER_TS, M2_N, M1_v1, M1_v2) / motor2_gain;
DBerendsen 10:614050a50c2f 281
DBerendsen 10:614050a50c2f 282 //Determine Motor Magnitude
DBerendsen 10:614050a50c2f 283 if (fabs(magnitude2)>1) {
DBerendsen 10:614050a50c2f 284 motor2 = 1;
DBerendsen 10:614050a50c2f 285 }
DBerendsen 10:614050a50c2f 286 else {
DBerendsen 10:614050a50c2f 287 motor2 = fabs(magnitude2);
DBerendsen 10:614050a50c2f 288 }
DBerendsen 10:614050a50c2f 289
DBerendsen 10:614050a50c2f 290
peterknoben 1:406519ff0f17 291 //Determine Motor Direction
DBerendsen 0:5f4bc2d63814 292 if (magnitude2 > 0){
DBerendsen 0:5f4bc2d63814 293 motor2DirectionPin = 1;
DBerendsen 0:5f4bc2d63814 294 }
DBerendsen 0:5f4bc2d63814 295 else{
DBerendsen 0:5f4bc2d63814 296 motor2DirectionPin = 0;
DBerendsen 0:5f4bc2d63814 297 }
DBerendsen 0:5f4bc2d63814 298 }
peterknoben 5:b4abbd3c513c 299
peterknoben 8:24f6744d47b2 300 //------------------------------------------------------------------------------
peterknoben 8:24f6744d47b2 301 //----------------------------Motor controller----------------------------------
peterknoben 8:24f6744d47b2 302 //------------------------------------------------------------------------------
peterknoben 8:24f6744d47b2 303 int direction_l, direction_r;
DBerendsen 10:614050a50c2f 304 float magnitude1, magnitude2;
peterknoben 8:24f6744d47b2 305
peterknoben 5:b4abbd3c513c 306 void motor_control(){
DBerendsen 10:614050a50c2f 307 direction_l = MoveLeft.getdirectionLeft(mode); //x-direction
DBerendsen 10:614050a50c2f 308 direction_r = MoveRight.getdirectionRight(mode); //y-direction
DBerendsen 10:614050a50c2f 309 position_math_l= MoveLeft.getpositionLeft(CaseLeft, mode, max_rangex); //x-direction
DBerendsen 10:614050a50c2f 310 position_math_r= MoveRight.getpositionRight(CaseRight, mode, max_rangey); //y-direction
DBerendsen 12:0765ea2c4c85 311 motor1_control(); //magnitude1 =
DBerendsen 12:0765ea2c4c85 312 motor2_control(); // magnitude2 =
DBerendsen 12:0765ea2c4c85 313
peterknoben 5:b4abbd3c513c 314 }
DBerendsen 11:28b5ec12a4b9 315 //------------------------------------------------------------------------------
peterknoben 8:24f6744d47b2 316 //-------------------------------HID Scope--------------------------------------
DBerendsen 0:5f4bc2d63814 317 //------------------------------------------------------------------------------
peterknoben 8:24f6744d47b2 318
DBerendsen 12:0765ea2c4c85 319 //HIDScope scope(4);
DBerendsen 12:0765ea2c4c85 320 //Ticker scopeTimer;
DBerendsen 12:0765ea2c4c85 321 Ticker FilterTimer;
peterknoben 8:24f6744d47b2 322
DBerendsen 12:0765ea2c4c85 323 void FilterUpdate(){
DBerendsen 12:0765ea2c4c85 324 FilterLeft(emg0.read());
DBerendsen 12:0765ea2c4c85 325 FilterRight(emg2.read());
DBerendsen 12:0765ea2c4c85 326 SignalLeft.getmeanLeft(n, fabs(FilterLeft(emg0.read())));
DBerendsen 12:0765ea2c4c85 327 SignalRight.getmeanRight(n, fabs(FilterRight(emg2.read())));
peterknoben 8:24f6744d47b2 328 }
peterknoben 8:24f6744d47b2 329
peterknoben 8:24f6744d47b2 330 //Working
peterknoben 8:24f6744d47b2 331 //SignalLeft.getmeanLeft(n, fabs(FilterLeft(emg0.read())))
DBerendsen 11:28b5ec12a4b9 332 //SignalRight.getmeanRight(n, fabs(FilterRight(emg2.read())))
peterknoben 8:24f6744d47b2 333
peterknoben 8:24f6744d47b2 334 //------------------------------------------------------------------------------
peterknoben 8:24f6744d47b2 335 //------------------------------Main--------------------------------------------
DBerendsen 0:5f4bc2d63814 336 //------------------------------------------------------------------------------
DBerendsen 0:5f4bc2d63814 337
DBerendsen 0:5f4bc2d63814 338 int main(){
DBerendsen 0:5f4bc2d63814 339 pc.baud(115200);
peterknoben 4:e15fc329b88b 340 setled();
peterknoben 6:edd30e11f3c7 341 BiQuad_filter_Left.add( &LP1L ).add( &HP2L ).add( &NO3L);
peterknoben 6:edd30e11f3c7 342 BiQuad_filter_Right.add( &LP1R ).add( &HP2R ).add( &NO3R);
peterknoben 6:edd30e11f3c7 343 BiQuad_filter_Mode.add( &LP1M ).add( &HP2M ).add( &NO3M);
peterknoben 4:e15fc329b88b 344 But1.rise(&maxcalibration);
peterknoben 6:edd30e11f3c7 345 Mode.rise(&mode_selection);
peterknoben 5:b4abbd3c513c 346 motor1.period(0.0001f); motor2.period(0.0001f);
peterknoben 8:24f6744d47b2 347 MyControllerTicker.attach(&motor_control, CONTROLLER_TS); //Determining motorposiitons
peterknoben 8:24f6744d47b2 348 MyTickerMean.attach(&signalnumber, MEAN_TS); //Detemining the signalnumbers
DBerendsen 11:28b5ec12a4b9 349 MyTickerServo.attach(&servo, 0.1f);
DBerendsen 11:28b5ec12a4b9 350
peterknoben 8:24f6744d47b2 351 //Scope
DBerendsen 12:0765ea2c4c85 352 //scopeTimer.attach_us(&scope, &HIDScope::send, 2e4);
DBerendsen 12:0765ea2c4c85 353 FilterTimer.attach_us(&FilterUpdate, 1e3);
peterknoben 8:24f6744d47b2 354
peterknoben 5:b4abbd3c513c 355 while(1) {
DBerendsen 10:614050a50c2f 356 //pc.printf(" direction %i , %i Signal numbers %i %i Position %f %f \r\n", direction_l, direction_r, CaseLeft, CaseRight, position_math_l, position_math_r);
DBerendsen 10:614050a50c2f 357 pc.printf("direction %i , %i Signal numbers %i %i Position %f %f magnitude1 =%f magnitude2=%f \r\n",direction_l, direction_r, CaseLeft, CaseRight, position_math_l, position_math_r, magnitude1, magnitude2);
peterknoben 5:b4abbd3c513c 358 wait(0.1f);
peterknoben 5:b4abbd3c513c 359 }
peterknoben 1:406519ff0f17 360 }
peterknoben 1:406519ff0f17 361
peterknoben 1:406519ff0f17 362