Eindelijk!!!!!
Dependencies: AnglePosition2 Encoder FastPWM HIDScope MODSERIAL Movement PIDController Servo SignalNumber biquadFilter mbed
Fork of Robot_control by
main.cpp@8:24f6744d47b2, 2017-11-01 (annotated)
- Committer:
- peterknoben
- Date:
- Wed Nov 01 22:23:25 2017 +0000
- Revision:
- 8:24f6744d47b2
- Parent:
- 7:105e3ae0fb19
- Child:
- 10:614050a50c2f
working
Who changed what in which revision?
User | Revision | Line number | New 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" |
peterknoben | 8:24f6744d47b2 | 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 |
peterknoben | 8:24f6744d47b2 | 19 | Ticker MyTickerMode; //Declare Ticker |
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); |
peterknoben | 1:406519ff0f17 | 41 | |
peterknoben | 1:406519ff0f17 | 42 | const float CONTROLLER_TS = 0.02; //Motor frequency |
peterknoben | 4:e15fc329b88b | 43 | const float MEAN_TS = 0.002; //Filter frequency |
peterknoben | 4:e15fc329b88b | 44 | |
peterknoben | 3:c768a37620c9 | 45 | //------------------------------------------------------------------------------ |
peterknoben | 3:c768a37620c9 | 46 | //-----------------------------EMG Signals-------------------------------------- |
peterknoben | 3:c768a37620c9 | 47 | //------------------------------------------------------------------------------ |
peterknoben | 4:e15fc329b88b | 48 | // Filtering the signal and getting the usefull information out of it. |
peterknoben | 8:24f6744d47b2 | 49 | const int n = 1500; //Window size for the mean value, also adjust in signalnumber.cpp |
peterknoben | 8:24f6744d47b2 | 50 | const int action =100; //Number of same mean values to change the signalnumber |
peterknoben | 8:24f6744d47b2 | 51 | const int m = 1500; //Number of samples for calibration |
peterknoben | 5:b4abbd3c513c | 52 | int CaseLeft, CaseRight, CaseMode; //Strength of the muscles |
peterknoben | 5:b4abbd3c513c | 53 | float emg_offsetLeft, emg_offsetmaxLeft; //Calibration offsets from zero to one for the left arm |
peterknoben | 5:b4abbd3c513c | 54 | float emg_offsetRight, emg_offsetmaxRight; //Calibration offsets from zero to one for the right arm |
peterknoben | 5:b4abbd3c513c | 55 | float emg_offsetMode, emg_offsetmaxMode; |
peterknoben | 5:b4abbd3c513c | 56 | float mean_value_left, mean_value_right, mean_value_mode; //Mean value of the filtered system |
peterknoben | 5:b4abbd3c513c | 57 | float kLeft, kRight, kMode; //Scaling factors |
peterknoben | 6:edd30e11f3c7 | 58 | float Signal_filteredLeft, Signal_filteredRight, Signal_filteredMode; |
peterknoben | 3:c768a37620c9 | 59 | |
peterknoben | 4:e15fc329b88b | 60 | //BiQuad filter variables |
peterknoben | 6:edd30e11f3c7 | 61 | BiQuad LP1L( 0.6389437261127493, 1.2778874522254986, 0.6389437261127493, 1.1429772843080919, 0.4127976201429053 ); //Lowpass filter Biquad |
peterknoben | 6:edd30e11f3c7 | 62 | BiQuad HP2L( 0.8370879899975344, -1.6741759799950688, 0.8370879899975344, -1.6474576182593796, 0.7008943417307579 ); //Highpass filter Biquad |
peterknoben | 6:edd30e11f3c7 | 63 | BiQuad NO3L( 0.7063988100714527, -1.1429772843080923, 0.7063988100714527, -1.1429772843080923, 0.41279762014290533); //Notch filter Biquad |
peterknoben | 6:edd30e11f3c7 | 64 | BiQuad LP1R( 0.6389437261127493, 1.2778874522254986, 0.6389437261127493, 1.1429772843080919, 0.4127976201429053 ); //Lowpass filter Biquad |
peterknoben | 6:edd30e11f3c7 | 65 | BiQuad HP2R( 0.8370879899975344, -1.6741759799950688, 0.8370879899975344, -1.6474576182593796, 0.7008943417307579 ); //Highpass filter Biquad |
peterknoben | 6:edd30e11f3c7 | 66 | BiQuad NO3R( 0.7063988100714527, -1.1429772843080923, 0.7063988100714527, -1.1429772843080923, 0.41279762014290533); //Notch filter Biquad |
peterknoben | 6:edd30e11f3c7 | 67 | BiQuad LP1M( 0.6389437261127493, 1.2778874522254986, 0.6389437261127493, 1.1429772843080919, 0.4127976201429053 ); //Lowpass filter Biquad |
peterknoben | 6:edd30e11f3c7 | 68 | BiQuad HP2M( 0.8370879899975344, -1.6741759799950688, 0.8370879899975344, -1.6474576182593796, 0.7008943417307579 ); //Highpass filter Biquad |
peterknoben | 6:edd30e11f3c7 | 69 | BiQuad NO3M( 0.7063988100714527, -1.1429772843080923, 0.7063988100714527, -1.1429772843080923, 0.41279762014290533); //Notch filter Biquad |
peterknoben | 6:edd30e11f3c7 | 70 | BiQuadChain BiQuad_filter_Left; |
peterknoben | 6:edd30e11f3c7 | 71 | BiQuadChain BiQuad_filter_Right; |
peterknoben | 6:edd30e11f3c7 | 72 | BiQuadChain BiQuad_filter_Mode; |
peterknoben | 3:c768a37620c9 | 73 | |
peterknoben | 7:105e3ae0fb19 | 74 | float FilterLeft(float input){ |
peterknoben | 7:105e3ae0fb19 | 75 | float Signal_filtered= BiQuad_filter_Left.step(input); |
peterknoben | 7:105e3ae0fb19 | 76 | return Signal_filtered; |
peterknoben | 7:105e3ae0fb19 | 77 | } |
peterknoben | 7:105e3ae0fb19 | 78 | float FilterRight(float input){ |
peterknoben | 7:105e3ae0fb19 | 79 | float Signal_filtered= BiQuad_filter_Right.step(input); |
peterknoben | 7:105e3ae0fb19 | 80 | return Signal_filtered; |
peterknoben | 7:105e3ae0fb19 | 81 | } |
peterknoben | 7:105e3ae0fb19 | 82 | float FilterMode(float input){ |
peterknoben | 7:105e3ae0fb19 | 83 | float Signal_filtered= BiQuad_filter_Mode.step(input); |
peterknoben | 7:105e3ae0fb19 | 84 | return Signal_filtered; |
peterknoben | 7:105e3ae0fb19 | 85 | } |
peterknoben | 5:b4abbd3c513c | 86 | |
peterknoben | 5:b4abbd3c513c | 87 | //Calibration------------------------------------------------------------------- |
peterknoben | 4:e15fc329b88b | 88 | void setled(){ |
peterknoben | 5:b4abbd3c513c | 89 | Led_red=0; Led_green=1; Led_blue=1; |
peterknoben | 5:b4abbd3c513c | 90 | } |
peterknoben | 8:24f6744d47b2 | 91 | |
peterknoben | 5:b4abbd3c513c | 92 | // One calibration |
peterknoben | 5:b4abbd3c513c | 93 | void maxcalibration(){ |
peterknoben | 5:b4abbd3c513c | 94 | pc.printf("start max calibration \r\n"); |
peterknoben | 8:24f6744d47b2 | 95 | emg_offsetmaxLeft = SignalLeft.calibrate(m, FilterLeft(emg0)); |
peterknoben | 8:24f6744d47b2 | 96 | emg_offsetmaxRight = SignalRight.calibrate(m, FilterRight(emg2)); |
peterknoben | 8:24f6744d47b2 | 97 | emg_offsetmaxMode = SignalMode.calibrate(m, FilterMode(emg4)); |
peterknoben | 5:b4abbd3c513c | 98 | kLeft = 1/emg_offsetmaxLeft; |
peterknoben | 5:b4abbd3c513c | 99 | kRight = 1/emg_offsetmaxRight; |
peterknoben | 5:b4abbd3c513c | 100 | kMode = 1/emg_offsetmaxMode; |
peterknoben | 5:b4abbd3c513c | 101 | Led_red=1; //Set led to Green |
peterknoben | 3:c768a37620c9 | 102 | } |
peterknoben | 3:c768a37620c9 | 103 | |
peterknoben | 5:b4abbd3c513c | 104 | //------------------------------------------------------------------------------ |
peterknoben | 5:b4abbd3c513c | 105 | //---------------------------------Servo---------------------------------------- |
peterknoben | 5:b4abbd3c513c | 106 | //------------------------------------------------------------------------------ |
peterknoben | 5:b4abbd3c513c | 107 | void servo(){ |
peterknoben | 8:24f6744d47b2 | 108 | Signal_filteredLeft = fabs(FilterLeft(emg0));//*kLeft |
peterknoben | 7:105e3ae0fb19 | 109 | Signal_filteredRight = fabs(FilterRight(emg2)); //*kRight |
peterknoben | 8:24f6744d47b2 | 110 | CaseLeft = SignalLeft.getnumberLeft(n, action, Signal_filteredLeft); |
peterknoben | 8:24f6744d47b2 | 111 | CaseRight = SignalRight.getnumberRight(n, action, Signal_filteredRight); |
peterknoben | 5:b4abbd3c513c | 112 | if (CaseLeft>=3){ |
peterknoben | 6:edd30e11f3c7 | 113 | Up = 0; |
peterknoben | 6:edd30e11f3c7 | 114 | Up = 1; |
peterknoben | 5:b4abbd3c513c | 115 | } |
peterknoben | 5:b4abbd3c513c | 116 | else if (CaseRight >=3){ |
peterknoben | 6:edd30e11f3c7 | 117 | Down = 0; |
peterknoben | 5:b4abbd3c513c | 118 | Down = 1; |
peterknoben | 5:b4abbd3c513c | 119 | } |
peterknoben | 5:b4abbd3c513c | 120 | } |
peterknoben | 5:b4abbd3c513c | 121 | int milli =0; |
peterknoben | 5:b4abbd3c513c | 122 | |
peterknoben | 5:b4abbd3c513c | 123 | //------------------------------------------------------------------------------ |
peterknoben | 5:b4abbd3c513c | 124 | //---------------------------Mode selection------------------------------------- |
peterknoben | 5:b4abbd3c513c | 125 | //------------------------------------------------------------------------------ |
peterknoben | 5:b4abbd3c513c | 126 | int mode =0; |
peterknoben | 5:b4abbd3c513c | 127 | |
peterknoben | 5:b4abbd3c513c | 128 | //Recieving mode selection from EMG mode signal |
peterknoben | 5:b4abbd3c513c | 129 | void mode_selection(){ |
peterknoben | 5:b4abbd3c513c | 130 | if(mode ==6){ |
peterknoben | 5:b4abbd3c513c | 131 | mode=1; |
peterknoben | 5:b4abbd3c513c | 132 | } |
peterknoben | 5:b4abbd3c513c | 133 | else{ |
peterknoben | 5:b4abbd3c513c | 134 | mode++; |
peterknoben | 5:b4abbd3c513c | 135 | } |
peterknoben | 5:b4abbd3c513c | 136 | if (mode==3||mode==6){ |
peterknoben | 5:b4abbd3c513c | 137 | servo(); |
peterknoben | 5:b4abbd3c513c | 138 | } |
peterknoben | 5:b4abbd3c513c | 139 | pc.printf("\r\n mode = %i \r\n", mode); |
peterknoben | 5:b4abbd3c513c | 140 | } |
peterknoben | 5:b4abbd3c513c | 141 | |
peterknoben | 5:b4abbd3c513c | 142 | // Control mode selection------------------------------------------------------- |
peterknoben | 5:b4abbd3c513c | 143 | |
peterknoben | 5:b4abbd3c513c | 144 | //Determine the signalnumbers (i.e. speed) based on the strenght of the EMG signals |
peterknoben | 3:c768a37620c9 | 145 | void signalnumber(){ |
peterknoben | 4:e15fc329b88b | 146 | //Left |
peterknoben | 8:24f6744d47b2 | 147 | Signal_filteredLeft = fabs(FilterLeft(emg0)*kLeft);//*kLeft |
peterknoben | 8:24f6744d47b2 | 148 | // mean_value_left = SignalLeft.getmeanLeft(n, Signal_filteredLeft); |
peterknoben | 8:24f6744d47b2 | 149 | mean_value_left = SignalLeft.getmeanLeft(n, fabs(FilterLeft(emg0.read())*kLeft)); |
peterknoben | 8:24f6744d47b2 | 150 | CaseLeft = SignalLeft.getnumberLeft(n, action, Signal_filteredLeft); |
peterknoben | 4:e15fc329b88b | 151 | //Right |
peterknoben | 8:24f6744d47b2 | 152 | Signal_filteredRight = fabs(FilterRight(emg2)*kRight);//*kRight |
peterknoben | 8:24f6744d47b2 | 153 | // mean_value_right = SignalRight.getmeanRight(n, Signal_filteredRight); |
peterknoben | 8:24f6744d47b2 | 154 | mean_value_right = SignalRight.getmeanRight(n, fabs(FilterRight(emg2.read())*kRight)); |
peterknoben | 8:24f6744d47b2 | 155 | CaseRight = SignalRight.getnumberRight(n, action, Signal_filteredRight); |
peterknoben | 5:b4abbd3c513c | 156 | //Mode |
peterknoben | 8:24f6744d47b2 | 157 | /* |
peterknoben | 8:24f6744d47b2 | 158 | Signal_filteredMode = fabs(FilterMode(emg4));//*kMode |
peterknoben | 8:24f6744d47b2 | 159 | mean_value_mode = SignalMode.getmeanMode(n, Signal_filteredMode); |
peterknoben | 8:24f6744d47b2 | 160 | CaseMode = SignalMode.getnumberMode(n, action, Signal_filteredMode); |
peterknoben | 8:24f6744d47b2 | 161 | if(CaseMode >= 3){ |
peterknoben | 5:b4abbd3c513c | 162 | milli ++; |
peterknoben | 5:b4abbd3c513c | 163 | if(milli>=150){ |
peterknoben | 5:b4abbd3c513c | 164 | mode_selection(); |
peterknoben | 5:b4abbd3c513c | 165 | milli=0; |
peterknoben | 5:b4abbd3c513c | 166 | } |
peterknoben | 5:b4abbd3c513c | 167 | } |
peterknoben | 5:b4abbd3c513c | 168 | else{ |
peterknoben | 5:b4abbd3c513c | 169 | milli=0; |
peterknoben | 6:edd30e11f3c7 | 170 | }*/ |
peterknoben | 3:c768a37620c9 | 171 | } |
peterknoben | 3:c768a37620c9 | 172 | |
peterknoben | 1:406519ff0f17 | 173 | //------------------------------------------------------------------------------ |
peterknoben | 1:406519ff0f17 | 174 | //-------------------------Kinematic Constants---------------------------------- |
peterknoben | 1:406519ff0f17 | 175 | //------------------------------------------------------------------------------ |
peterknoben | 4:e15fc329b88b | 176 | const double RAD_PER_PULSE = 0.00074799825*2; //Number of radials per pulse from the encoder |
peterknoben | 4:e15fc329b88b | 177 | const double PI = 3.14159265358979323846; //Pi |
peterknoben | 8:24f6744d47b2 | 178 | const float max_rangex = 500.0, max_rangey = 300.0; //Max range on the y axis |
peterknoben | 8:24f6744d47b2 | 179 | const float x_offset = 156.15, y_offset = -76.97; //Starting positions from the shoulder joint |
peterknoben | 4:e15fc329b88b | 180 | const float alpha_offset = -(21.52/180)*PI; //Begin angle Alpha at zero zero |
peterknoben | 4:e15fc329b88b | 181 | const float beta_offset = 0.0; //Begin angle Beta at zero zero |
peterknoben | 8:24f6744d47b2 | 182 | const float L1 = 450.0, L2 = 490.0; //Length of the bodies |
peterknoben | 5:b4abbd3c513c | 183 | |
DBerendsen | 0:5f4bc2d63814 | 184 | //------------------------------------------------------------------------------ |
DBerendsen | 0:5f4bc2d63814 | 185 | //--------------------------------Motor1---------------------------------------- |
DBerendsen | 0:5f4bc2d63814 | 186 | //------------------------------------------------------------------------------ |
peterknoben | 1:406519ff0f17 | 187 | FastPWM motor1(D5); |
DBerendsen | 0:5f4bc2d63814 | 188 | DigitalOut motor1DirectionPin(D4); |
DBerendsen | 0:5f4bc2d63814 | 189 | DigitalIn ENC2A(D12); |
DBerendsen | 0:5f4bc2d63814 | 190 | DigitalIn ENC2B(D13); |
DBerendsen | 0:5f4bc2d63814 | 191 | Encoder encoder1(D13,D12); |
peterknoben | 1:406519ff0f17 | 192 | const float MOTOR1_KP = 40.0; |
peterknoben | 1:406519ff0f17 | 193 | const float MOTOR1_KI = 0.0; |
peterknoben | 1:406519ff0f17 | 194 | const float MOTOR1_KD = 15.0; |
peterknoben | 5:b4abbd3c513c | 195 | double M1_v1 = 0.0, M1_v2 = 0.0; //Calculation values |
DBerendsen | 0:5f4bc2d63814 | 196 | const double motor1_gain = 2*PI; |
peterknoben | 1:406519ff0f17 | 197 | const float M1_N = 0.5; |
peterknoben | 8:24f6744d47b2 | 198 | float position_math_l =0, position_math_r=0; |
DBerendsen | 0:5f4bc2d63814 | 199 | |
DBerendsen | 0:5f4bc2d63814 | 200 | void motor1_control(){ |
peterknoben | 8:24f6744d47b2 | 201 | 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 | 202 | float position_alpha = RAD_PER_PULSE * encoder1.getPosition(); |
DBerendsen | 0:5f4bc2d63814 | 203 | float error_alpha = reference_alpha-position_alpha; |
peterknoben | 1:406519ff0f17 | 204 | float magnitude1 = PID.get(error_alpha, MOTOR1_KP, MOTOR1_KI, MOTOR1_KD, CONTROLLER_TS, M1_N, M1_v1, M1_v2) / motor1_gain; |
DBerendsen | 0:5f4bc2d63814 | 205 | motor1 = fabs(magnitude1); |
peterknoben | 1:406519ff0f17 | 206 | // Determine Motor Direction |
peterknoben | 1:406519ff0f17 | 207 | if (magnitude1 < 0){ |
DBerendsen | 0:5f4bc2d63814 | 208 | motor1DirectionPin = 1; |
DBerendsen | 0:5f4bc2d63814 | 209 | } |
DBerendsen | 0:5f4bc2d63814 | 210 | else{ |
DBerendsen | 0:5f4bc2d63814 | 211 | motor1DirectionPin = 0; |
DBerendsen | 0:5f4bc2d63814 | 212 | } |
DBerendsen | 0:5f4bc2d63814 | 213 | } |
DBerendsen | 0:5f4bc2d63814 | 214 | |
DBerendsen | 0:5f4bc2d63814 | 215 | //------------------------------------------------------------------------------ |
DBerendsen | 0:5f4bc2d63814 | 216 | //--------------------------------Motor2---------------------------------------- |
DBerendsen | 0:5f4bc2d63814 | 217 | //------------------------------------------------------------------------------ |
peterknoben | 1:406519ff0f17 | 218 | FastPWM motor2(D6); |
DBerendsen | 0:5f4bc2d63814 | 219 | DigitalOut motor2DirectionPin(D7); |
DBerendsen | 0:5f4bc2d63814 | 220 | DigitalIn ENC1A(D10); |
DBerendsen | 0:5f4bc2d63814 | 221 | DigitalIn ENC1B(D11); |
DBerendsen | 0:5f4bc2d63814 | 222 | Encoder encoder2(D10,D11); |
peterknoben | 1:406519ff0f17 | 223 | const float MOTOR2_KP = 60.0; |
peterknoben | 1:406519ff0f17 | 224 | const float MOTOR2_KI = 0.0; |
peterknoben | 1:406519ff0f17 | 225 | const float MOTOR2_KD = 15.0; |
peterknoben | 5:b4abbd3c513c | 226 | double M2_v1 = 0.0, M2_v2 = 0.0; //Calculation values |
DBerendsen | 0:5f4bc2d63814 | 227 | const double motor2_gain = 2*PI; |
peterknoben | 1:406519ff0f17 | 228 | const float M2_N = 0.5; |
DBerendsen | 0:5f4bc2d63814 | 229 | |
DBerendsen | 0:5f4bc2d63814 | 230 | void motor2_control(){ |
peterknoben | 8:24f6744d47b2 | 231 | 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 | 232 | float position_beta = RAD_PER_PULSE * -encoder2.getPosition(); |
DBerendsen | 0:5f4bc2d63814 | 233 | float error_beta = reference_beta-position_beta; |
peterknoben | 1:406519ff0f17 | 234 | float magnitude2 = PID.get(error_beta, MOTOR2_KP, MOTOR2_KI, MOTOR2_KD, CONTROLLER_TS, M2_N, M1_v1, M1_v2) / motor2_gain; |
DBerendsen | 0:5f4bc2d63814 | 235 | motor2 = fabs(magnitude2); |
peterknoben | 1:406519ff0f17 | 236 | //Determine Motor Direction |
DBerendsen | 0:5f4bc2d63814 | 237 | if (magnitude2 > 0){ |
DBerendsen | 0:5f4bc2d63814 | 238 | motor2DirectionPin = 1; |
DBerendsen | 0:5f4bc2d63814 | 239 | } |
DBerendsen | 0:5f4bc2d63814 | 240 | else{ |
DBerendsen | 0:5f4bc2d63814 | 241 | motor2DirectionPin = 0; |
DBerendsen | 0:5f4bc2d63814 | 242 | } |
DBerendsen | 0:5f4bc2d63814 | 243 | } |
peterknoben | 5:b4abbd3c513c | 244 | |
peterknoben | 8:24f6744d47b2 | 245 | //------------------------------------------------------------------------------ |
peterknoben | 8:24f6744d47b2 | 246 | //----------------------------Motor controller---------------------------------- |
peterknoben | 8:24f6744d47b2 | 247 | //------------------------------------------------------------------------------ |
peterknoben | 8:24f6744d47b2 | 248 | int direction_l, direction_r; |
peterknoben | 8:24f6744d47b2 | 249 | |
peterknoben | 5:b4abbd3c513c | 250 | void motor_control(){ |
peterknoben | 8:24f6744d47b2 | 251 | direction_l = MoveLeft.getdirectionLeft(mode); |
peterknoben | 8:24f6744d47b2 | 252 | direction_r = MoveRight.getdirectionRight(mode); |
peterknoben | 8:24f6744d47b2 | 253 | position_math_l= MoveLeft.getpositionLeft(CaseLeft, mode, max_rangex); |
peterknoben | 8:24f6744d47b2 | 254 | position_math_r= MoveRight.getpositionRight(CaseRight, mode, max_rangey); |
peterknoben | 8:24f6744d47b2 | 255 | // motor1_control(); |
peterknoben | 8:24f6744d47b2 | 256 | // motor2_control(); |
peterknoben | 5:b4abbd3c513c | 257 | } |
DBerendsen | 0:5f4bc2d63814 | 258 | //------------------------------------------------------------------------------ |
peterknoben | 8:24f6744d47b2 | 259 | //-------------------------------HID Scope-------------------------------------- |
DBerendsen | 0:5f4bc2d63814 | 260 | //------------------------------------------------------------------------------ |
peterknoben | 8:24f6744d47b2 | 261 | |
peterknoben | 8:24f6744d47b2 | 262 | HIDScope scope(4); |
peterknoben | 8:24f6744d47b2 | 263 | Ticker scopeTimer; |
peterknoben | 8:24f6744d47b2 | 264 | Ticker controllerTimer; |
peterknoben | 8:24f6744d47b2 | 265 | |
peterknoben | 8:24f6744d47b2 | 266 | void HID_Monitor(){ |
peterknoben | 8:24f6744d47b2 | 267 | scope.set(0, fabs(FilterLeft(emg0.read()))); |
peterknoben | 8:24f6744d47b2 | 268 | scope.set(1, fabs(FilterRight(emg2.read()))); |
peterknoben | 8:24f6744d47b2 | 269 | scope.set(2, SignalLeft.getmeanLeft(n, fabs(FilterLeft(emg0.read())))); |
peterknoben | 8:24f6744d47b2 | 270 | scope.set(3, SignalRight.getmeanRight(n, fabs(FilterRight(emg2.read())))); |
peterknoben | 8:24f6744d47b2 | 271 | } |
peterknoben | 8:24f6744d47b2 | 272 | |
peterknoben | 8:24f6744d47b2 | 273 | //Working |
peterknoben | 8:24f6744d47b2 | 274 | //SignalLeft.getmeanLeft(n, fabs(FilterLeft(emg0.read()))) |
peterknoben | 8:24f6744d47b2 | 275 | //SignalRight.getmeanRight(n, fabs(FilterRight(emg2.read()))) |
peterknoben | 8:24f6744d47b2 | 276 | |
peterknoben | 8:24f6744d47b2 | 277 | //------------------------------------------------------------------------------ |
peterknoben | 8:24f6744d47b2 | 278 | //------------------------------Main-------------------------------------------- |
DBerendsen | 0:5f4bc2d63814 | 279 | //------------------------------------------------------------------------------ |
DBerendsen | 0:5f4bc2d63814 | 280 | |
DBerendsen | 0:5f4bc2d63814 | 281 | int main(){ |
DBerendsen | 0:5f4bc2d63814 | 282 | pc.baud(115200); |
peterknoben | 4:e15fc329b88b | 283 | setled(); |
peterknoben | 6:edd30e11f3c7 | 284 | BiQuad_filter_Left.add( &LP1L ).add( &HP2L ).add( &NO3L); |
peterknoben | 6:edd30e11f3c7 | 285 | BiQuad_filter_Right.add( &LP1R ).add( &HP2R ).add( &NO3R); |
peterknoben | 6:edd30e11f3c7 | 286 | BiQuad_filter_Mode.add( &LP1M ).add( &HP2M ).add( &NO3M); |
peterknoben | 4:e15fc329b88b | 287 | But1.rise(&maxcalibration); |
peterknoben | 6:edd30e11f3c7 | 288 | Mode.rise(&mode_selection); |
peterknoben | 5:b4abbd3c513c | 289 | motor1.period(0.0001f); motor2.period(0.0001f); |
peterknoben | 8:24f6744d47b2 | 290 | MyControllerTicker.attach(&motor_control, CONTROLLER_TS); //Determining motorposiitons |
peterknoben | 8:24f6744d47b2 | 291 | MyTickerMean.attach(&signalnumber, MEAN_TS); //Detemining the signalnumbers |
peterknoben | 5:b4abbd3c513c | 292 | // MyTickerMode.attach(&signalmode, MEAN_TS); |
peterknoben | 4:e15fc329b88b | 293 | |
peterknoben | 8:24f6744d47b2 | 294 | //Scope |
peterknoben | 8:24f6744d47b2 | 295 | scopeTimer.attach_us(&scope, &HIDScope::send, 2e4); |
peterknoben | 8:24f6744d47b2 | 296 | controllerTimer.attach_us(&HID_Monitor, 1e3); |
peterknoben | 8:24f6744d47b2 | 297 | |
peterknoben | 5:b4abbd3c513c | 298 | while(1) { |
peterknoben | 8:24f6744d47b2 | 299 | 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); |
peterknoben | 5:b4abbd3c513c | 300 | wait(0.1f); |
peterknoben | 5:b4abbd3c513c | 301 | } |
peterknoben | 1:406519ff0f17 | 302 | } |
peterknoben | 1:406519ff0f17 | 303 | |
peterknoben | 1:406519ff0f17 | 304 |