![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
sdfa
Dependencies: AnglePosition2 Encoder FastPWM MODSERIAL Movement PIDController Servo SignalNumber2 biquadFilter mbed
Fork of kinematics_controlv2 by
main.cpp@5:b4abbd3c513c, 2017-11-01 (annotated)
- Committer:
- peterknoben
- Date:
- Wed Nov 01 11:47:03 2017 +0000
- Revision:
- 5:b4abbd3c513c
- Parent:
- 4:e15fc329b88b
fsd
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" |
DBerendsen | 0:5f4bc2d63814 | 11 | |
peterknoben | 4:e15fc329b88b | 12 | //This code is for Mbed 2 |
DBerendsen | 0:5f4bc2d63814 | 13 | //------------------------------------------------------------------------------ |
DBerendsen | 0:5f4bc2d63814 | 14 | //------------------------------------------------------------------------------ |
DBerendsen | 0:5f4bc2d63814 | 15 | //------------------------------------------------------------------------------ |
peterknoben | 3:c768a37620c9 | 16 | MODSERIAL pc(USBTX, USBRX); //Establish connection |
peterknoben | 5:b4abbd3c513c | 17 | Ticker MyControllerTicker; //Declare Ticker Motor 1 |
peterknoben | 5:b4abbd3c513c | 18 | Ticker MyTickerMode; //Declare Ticker Motor 2 |
peterknoben | 3:c768a37620c9 | 19 | Ticker MyTickerMean; //Declare Ticker Signalprocessing |
peterknoben | 3:c768a37620c9 | 20 | |
peterknoben | 4:e15fc329b88b | 21 | InterruptIn But2(PTA4); //Declare button for min calibration |
peterknoben | 4:e15fc329b88b | 22 | InterruptIn But1(PTC6); //Declare button for max calibration |
peterknoben | 3:c768a37620c9 | 23 | |
peterknoben | 1:406519ff0f17 | 24 | AnglePosition Angle; //Declare Angle calculater |
peterknoben | 1:406519ff0f17 | 25 | PIDControl PID; //Declare PID Controller |
peterknoben | 4:e15fc329b88b | 26 | SignalNumber SignalLeft; //Declare Signal determiner for Left arm |
peterknoben | 4:e15fc329b88b | 27 | SignalNumber SignalRight; //Declare Signal determiner for Right arm |
peterknoben | 5:b4abbd3c513c | 28 | SignalNumber SignalMode; |
peterknoben | 4:e15fc329b88b | 29 | Movement MoveLeft; //Declare Movement determiner |
peterknoben | 4:e15fc329b88b | 30 | Movement MoveRight; |
peterknoben | 3:c768a37620c9 | 31 | |
peterknoben | 4:e15fc329b88b | 32 | AnalogIn emg0( A0 ); //Set Inputpin for EMG 0 signal Left |
peterknoben | 5:b4abbd3c513c | 33 | //AnalogIn emg1( A1 ); //Set Inputpin for EMG 1 signal Left |
peterknoben | 4:e15fc329b88b | 34 | AnalogIn emg2( A2 ); //Set Inputpin for EMG 2 signal Right |
peterknoben | 5:b4abbd3c513c | 35 | //AnalogIn emg3( A3 ); //Set Inputpin for EMG 3 signal Right |
peterknoben | 4:e15fc329b88b | 36 | AnalogIn emg4( A4 ); //Set Inputpin for EMG 4 signal Mode |
peterknoben | 5:b4abbd3c513c | 37 | //AnalogIn emg5( A5 ); //Set Inputpin for EMG 5 signal Mode |
peterknoben | 5:b4abbd3c513c | 38 | DigitalOut Up( D9 ); //Set digital in for mode selection |
peterknoben | 5:b4abbd3c513c | 39 | DigitalOut Down( D8 ); |
peterknoben | 4:e15fc329b88b | 40 | DigitalOut Led_red(LED_RED); |
peterknoben | 4:e15fc329b88b | 41 | DigitalOut Led_green(LED_GREEN); |
peterknoben | 4:e15fc329b88b | 42 | DigitalOut Led_blue(LED_BLUE); |
peterknoben | 1:406519ff0f17 | 43 | |
peterknoben | 1:406519ff0f17 | 44 | const float CONTROLLER_TS = 0.02; //Motor frequency |
peterknoben | 4:e15fc329b88b | 45 | const float MEAN_TS = 0.002; //Filter frequency |
peterknoben | 4:e15fc329b88b | 46 | |
peterknoben | 3:c768a37620c9 | 47 | |
peterknoben | 3:c768a37620c9 | 48 | //------------------------------------------------------------------------------ |
peterknoben | 3:c768a37620c9 | 49 | //-----------------------------EMG Signals-------------------------------------- |
peterknoben | 3:c768a37620c9 | 50 | //------------------------------------------------------------------------------ |
peterknoben | 4:e15fc329b88b | 51 | // Filtering the signal and getting the usefull information out of it. |
peterknoben | 4:e15fc329b88b | 52 | const int n = 400; //Window size for the mean value, also adjust in signalnumber.cpp |
peterknoben | 3:c768a37620c9 | 53 | const int action =50; //Number of same mean values to change the signalnumber |
peterknoben | 4:e15fc329b88b | 54 | const int m = 300; //Number of samples for calibration |
peterknoben | 5:b4abbd3c513c | 55 | int CaseLeft, CaseRight, CaseMode; //Strength of the muscles |
peterknoben | 5:b4abbd3c513c | 56 | float emg_offsetLeft, emg_offsetmaxLeft; //Calibration offsets from zero to one for the left arm |
peterknoben | 5:b4abbd3c513c | 57 | float emg_offsetRight, emg_offsetmaxRight; //Calibration offsets from zero to one for the right arm |
peterknoben | 5:b4abbd3c513c | 58 | float emg_offsetMode, emg_offsetmaxMode; |
peterknoben | 5:b4abbd3c513c | 59 | float mean_value_left, mean_value_right, mean_value_mode; //Mean value of the filtered system |
peterknoben | 5:b4abbd3c513c | 60 | float kLeft, kRight, kMode; //Scaling factors |
peterknoben | 3:c768a37620c9 | 61 | |
peterknoben | 4:e15fc329b88b | 62 | //BiQuad filter variables |
peterknoben | 3:c768a37620c9 | 63 | BiQuad LP1( 0.6389437261127493, 1.2778874522254986, 0.6389437261127493, 1.1429772843080919, 0.4127976201429053 ); //Lowpass filter Biquad |
peterknoben | 3:c768a37620c9 | 64 | BiQuad HP2( 0.8370879899975344, -1.6741759799950688, 0.8370879899975344, -1.6474576182593796, 0.7008943417307579 ); //Highpass filter Biquad |
peterknoben | 3:c768a37620c9 | 65 | BiQuad NO3( 0.7063988100714527, -1.1429772843080923, 0.7063988100714527, -1.1429772843080923, 0.41279762014290533); //Notch filter Biquad |
peterknoben | 3:c768a37620c9 | 66 | BiQuadChain BiQuad_filter; |
peterknoben | 3:c768a37620c9 | 67 | |
peterknoben | 5:b4abbd3c513c | 68 | |
peterknoben | 5:b4abbd3c513c | 69 | //Calibration------------------------------------------------------------------- |
peterknoben | 4:e15fc329b88b | 70 | void setled(){ |
peterknoben | 5:b4abbd3c513c | 71 | Led_red=0; Led_green=1; Led_blue=1; |
peterknoben | 5:b4abbd3c513c | 72 | } |
peterknoben | 5:b4abbd3c513c | 73 | // Zero calibration |
peterknoben | 5:b4abbd3c513c | 74 | void mincalibration(){ |
peterknoben | 5:b4abbd3c513c | 75 | pc.printf("start min calibration \r\n"); |
peterknoben | 5:b4abbd3c513c | 76 | emg_offsetLeft = SignalLeft.calibrate(m,emg0); |
peterknoben | 5:b4abbd3c513c | 77 | emg_offsetRight = SignalRight.calibrate(m,emg2); |
peterknoben | 5:b4abbd3c513c | 78 | emg_offsetMode = SignalMode.calibrate(m, emg4); |
peterknoben | 5:b4abbd3c513c | 79 | pc.printf("offsets: %f %f \r\n", emg_offsetLeft, emg_offsetRight); |
peterknoben | 5:b4abbd3c513c | 80 | Led_green=0; Led_red=0; //Set led to Yellow |
peterknoben | 5:b4abbd3c513c | 81 | } |
peterknoben | 5:b4abbd3c513c | 82 | // One calibration |
peterknoben | 5:b4abbd3c513c | 83 | void maxcalibration(){ |
peterknoben | 5:b4abbd3c513c | 84 | pc.printf("start max calibration \r\n"); |
peterknoben | 5:b4abbd3c513c | 85 | emg_offsetmaxLeft = SignalLeft.calibrate(m,(emg0))-emg_offsetLeft; |
peterknoben | 5:b4abbd3c513c | 86 | emg_offsetmaxRight = SignalRight.calibrate(m,(emg2))-emg_offsetRight; |
peterknoben | 5:b4abbd3c513c | 87 | emg_offsetmaxMode = SignalMode.calibrate(m, (emg4))-emg_offsetMode; |
peterknoben | 5:b4abbd3c513c | 88 | kLeft = 1/emg_offsetmaxLeft; |
peterknoben | 5:b4abbd3c513c | 89 | kRight = 1/emg_offsetmaxRight; |
peterknoben | 5:b4abbd3c513c | 90 | kMode = 1/emg_offsetmaxMode; |
peterknoben | 5:b4abbd3c513c | 91 | pc.printf("offsets: %f %f, scale %f %f \r\n", emg_offsetmaxLeft, emg_offsetmaxRight, kLeft, kRight); |
peterknoben | 5:b4abbd3c513c | 92 | Led_red=1; //Set led to Green |
peterknoben | 3:c768a37620c9 | 93 | } |
peterknoben | 3:c768a37620c9 | 94 | |
peterknoben | 5:b4abbd3c513c | 95 | //Filtering the signals--------------------------------------------------------- |
peterknoben | 4:e15fc329b88b | 96 | //Filter de EMG signals with a BiQuad filter |
peterknoben | 5:b4abbd3c513c | 97 | float Filter(float input, float offset){ |
peterknoben | 5:b4abbd3c513c | 98 | float Signal=input-offset; //((input0+input1)/2) |
peterknoben | 3:c768a37620c9 | 99 | float Signal_filtered= BiQuad_filter.step(Signal); |
peterknoben | 3:c768a37620c9 | 100 | return Signal_filtered; |
peterknoben | 3:c768a37620c9 | 101 | } |
peterknoben | 3:c768a37620c9 | 102 | |
peterknoben | 4:e15fc329b88b | 103 | |
peterknoben | 5:b4abbd3c513c | 104 | //------------------------------------------------------------------------------ |
peterknoben | 5:b4abbd3c513c | 105 | //---------------------------------Servo---------------------------------------- |
peterknoben | 5:b4abbd3c513c | 106 | //------------------------------------------------------------------------------ |
peterknoben | 5:b4abbd3c513c | 107 | void servo(){ |
peterknoben | 5:b4abbd3c513c | 108 | float Signal_filteredLeft = fabs(Filter(emg0, emg_offsetLeft)); |
peterknoben | 5:b4abbd3c513c | 109 | float Signal_filteredRight = fabs(Filter(emg2, emg_offsetRight)); |
peterknoben | 5:b4abbd3c513c | 110 | CaseLeft = SignalLeft.getnumber(n, action, Signal_filteredLeft, kLeft); |
peterknoben | 5:b4abbd3c513c | 111 | CaseRight = SignalRight.getnumber(n, action, Signal_filteredRight, kRight); |
peterknoben | 5:b4abbd3c513c | 112 | if (CaseLeft>=3){ |
peterknoben | 5:b4abbd3c513c | 113 | Up = 1; |
peterknoben | 5:b4abbd3c513c | 114 | Up = 0; |
peterknoben | 5:b4abbd3c513c | 115 | } |
peterknoben | 5:b4abbd3c513c | 116 | else if (CaseRight >=3){ |
peterknoben | 5:b4abbd3c513c | 117 | Down = 1; |
peterknoben | 5:b4abbd3c513c | 118 | Down = 0; |
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 | 5:b4abbd3c513c | 147 | float Signal_filteredLeft = fabs(Filter(emg0, emg_offsetLeft)); |
peterknoben | 5:b4abbd3c513c | 148 | mean_value_left = SignalLeft.getmean(n, Signal_filteredLeft)*kLeft; |
peterknoben | 4:e15fc329b88b | 149 | CaseLeft = SignalLeft.getnumber(n, action, Signal_filteredLeft, kLeft); |
peterknoben | 4:e15fc329b88b | 150 | //Right |
peterknoben | 5:b4abbd3c513c | 151 | float Signal_filteredRight = fabs(Filter(emg2, emg_offsetRight)); |
peterknoben | 5:b4abbd3c513c | 152 | mean_value_right = SignalRight.getmean(n, Signal_filteredRight)*kRight; |
peterknoben | 4:e15fc329b88b | 153 | CaseRight = SignalRight.getnumber(n, action, Signal_filteredRight, kRight); |
peterknoben | 5:b4abbd3c513c | 154 | //Mode |
peterknoben | 5:b4abbd3c513c | 155 | float Signal_filteredMode = fabs(Filter(emg4, emg_offsetMode)); |
peterknoben | 5:b4abbd3c513c | 156 | mean_value_mode = SignalMode.getmean(n, Signal_filteredMode)*kMode; |
peterknoben | 5:b4abbd3c513c | 157 | CaseMode = SignalMode.getnumber(n, action, Signal_filteredMode, kMode); |
peterknoben | 5:b4abbd3c513c | 158 | if(CaseMode >= 3){ |
peterknoben | 5:b4abbd3c513c | 159 | milli ++; |
peterknoben | 5:b4abbd3c513c | 160 | if(milli>=150){ |
peterknoben | 5:b4abbd3c513c | 161 | mode_selection(); |
peterknoben | 5:b4abbd3c513c | 162 | milli=0; |
peterknoben | 5:b4abbd3c513c | 163 | } |
peterknoben | 5:b4abbd3c513c | 164 | } |
peterknoben | 5:b4abbd3c513c | 165 | else{ |
peterknoben | 5:b4abbd3c513c | 166 | milli=0; |
peterknoben | 5:b4abbd3c513c | 167 | } |
peterknoben | 3:c768a37620c9 | 168 | } |
peterknoben | 3:c768a37620c9 | 169 | |
peterknoben | 5:b4abbd3c513c | 170 | |
peterknoben | 5:b4abbd3c513c | 171 | |
peterknoben | 1:406519ff0f17 | 172 | //------------------------------------------------------------------------------ |
peterknoben | 1:406519ff0f17 | 173 | //-------------------------Kinematic Constants---------------------------------- |
peterknoben | 1:406519ff0f17 | 174 | //------------------------------------------------------------------------------ |
peterknoben | 4:e15fc329b88b | 175 | const double RAD_PER_PULSE = 0.00074799825*2; //Number of radials per pulse from the encoder |
peterknoben | 4:e15fc329b88b | 176 | const double PI = 3.14159265358979323846; //Pi |
peterknoben | 4:e15fc329b88b | 177 | const float max_rangex = 500.0; //Max range on the x axis |
peterknoben | 4:e15fc329b88b | 178 | const float max_rangey = 300.0; //Max range on the y axis |
peterknoben | 4:e15fc329b88b | 179 | const float x_offset = 156.15; //Start x position from the shoulder joint |
peterknoben | 4:e15fc329b88b | 180 | const float y_offset = -76.97; //Start y position from the shoulder joint |
peterknoben | 4:e15fc329b88b | 181 | const float alpha_offset = -(21.52/180)*PI; //Begin angle Alpha at zero zero |
peterknoben | 4:e15fc329b88b | 182 | const float beta_offset = 0.0; //Begin angle Beta at zero zero |
peterknoben | 4:e15fc329b88b | 183 | const float L1 = 450.0; //Length of the first body |
peterknoben | 4:e15fc329b88b | 184 | const float L2 = 490.0; //Length of the second body |
DBerendsen | 0:5f4bc2d63814 | 185 | |
DBerendsen | 0:5f4bc2d63814 | 186 | |
peterknoben | 5:b4abbd3c513c | 187 | |
DBerendsen | 0:5f4bc2d63814 | 188 | //------------------------------------------------------------------------------ |
DBerendsen | 0:5f4bc2d63814 | 189 | //--------------------------------Motor1---------------------------------------- |
DBerendsen | 0:5f4bc2d63814 | 190 | //------------------------------------------------------------------------------ |
peterknoben | 1:406519ff0f17 | 191 | FastPWM motor1(D5); |
DBerendsen | 0:5f4bc2d63814 | 192 | DigitalOut motor1DirectionPin(D4); |
DBerendsen | 0:5f4bc2d63814 | 193 | DigitalIn ENC2A(D12); |
DBerendsen | 0:5f4bc2d63814 | 194 | DigitalIn ENC2B(D13); |
DBerendsen | 0:5f4bc2d63814 | 195 | Encoder encoder1(D13,D12); |
peterknoben | 1:406519ff0f17 | 196 | const float MOTOR1_KP = 40.0; |
peterknoben | 1:406519ff0f17 | 197 | const float MOTOR1_KI = 0.0; |
peterknoben | 1:406519ff0f17 | 198 | const float MOTOR1_KD = 15.0; |
peterknoben | 5:b4abbd3c513c | 199 | double M1_v1 = 0.0, M1_v2 = 0.0; //Calculation values |
DBerendsen | 0:5f4bc2d63814 | 200 | const double motor1_gain = 2*PI; |
peterknoben | 1:406519ff0f17 | 201 | const float M1_N = 0.5; |
peterknoben | 5:b4abbd3c513c | 202 | static float position_math[2]={}; |
DBerendsen | 0:5f4bc2d63814 | 203 | |
DBerendsen | 0:5f4bc2d63814 | 204 | void motor1_control(){ |
peterknoben | 4:e15fc329b88b | 205 | float reference_alpha = Angle.getbeta(max_rangex, max_rangey, x_offset, y_offset, beta_offset, L1, L2, position_math[0], position_math[1]); |
DBerendsen | 0:5f4bc2d63814 | 206 | float position_alpha = RAD_PER_PULSE * encoder1.getPosition(); |
DBerendsen | 0:5f4bc2d63814 | 207 | float error_alpha = reference_alpha-position_alpha; |
peterknoben | 1:406519ff0f17 | 208 | 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 | 209 | motor1 = fabs(magnitude1); |
peterknoben | 1:406519ff0f17 | 210 | // Determine Motor Direction |
peterknoben | 1:406519ff0f17 | 211 | if (magnitude1 < 0){ |
DBerendsen | 0:5f4bc2d63814 | 212 | motor1DirectionPin = 1; |
DBerendsen | 0:5f4bc2d63814 | 213 | } |
DBerendsen | 0:5f4bc2d63814 | 214 | else{ |
DBerendsen | 0:5f4bc2d63814 | 215 | motor1DirectionPin = 0; |
DBerendsen | 0:5f4bc2d63814 | 216 | } |
DBerendsen | 0:5f4bc2d63814 | 217 | } |
DBerendsen | 0:5f4bc2d63814 | 218 | |
DBerendsen | 0:5f4bc2d63814 | 219 | //------------------------------------------------------------------------------ |
DBerendsen | 0:5f4bc2d63814 | 220 | //--------------------------------Motor2---------------------------------------- |
DBerendsen | 0:5f4bc2d63814 | 221 | //------------------------------------------------------------------------------ |
peterknoben | 1:406519ff0f17 | 222 | FastPWM motor2(D6); |
DBerendsen | 0:5f4bc2d63814 | 223 | DigitalOut motor2DirectionPin(D7); |
DBerendsen | 0:5f4bc2d63814 | 224 | DigitalIn ENC1A(D10); |
DBerendsen | 0:5f4bc2d63814 | 225 | DigitalIn ENC1B(D11); |
DBerendsen | 0:5f4bc2d63814 | 226 | Encoder encoder2(D10,D11); |
peterknoben | 1:406519ff0f17 | 227 | const float MOTOR2_KP = 60.0; |
peterknoben | 1:406519ff0f17 | 228 | const float MOTOR2_KI = 0.0; |
peterknoben | 1:406519ff0f17 | 229 | const float MOTOR2_KD = 15.0; |
peterknoben | 5:b4abbd3c513c | 230 | double M2_v1 = 0.0, M2_v2 = 0.0; //Calculation values |
DBerendsen | 0:5f4bc2d63814 | 231 | const double motor2_gain = 2*PI; |
peterknoben | 1:406519ff0f17 | 232 | const float M2_N = 0.5; |
DBerendsen | 0:5f4bc2d63814 | 233 | |
DBerendsen | 0:5f4bc2d63814 | 234 | void motor2_control(){ |
peterknoben | 4:e15fc329b88b | 235 | float reference_beta = Angle.getalpha(max_rangex, max_rangey, x_offset, y_offset, alpha_offset, L1, L2, position_math[0], position_math[1]); |
DBerendsen | 0:5f4bc2d63814 | 236 | float position_beta = RAD_PER_PULSE * -encoder2.getPosition(); |
DBerendsen | 0:5f4bc2d63814 | 237 | float error_beta = reference_beta-position_beta; |
peterknoben | 1:406519ff0f17 | 238 | 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 | 239 | motor2 = fabs(magnitude2); |
peterknoben | 1:406519ff0f17 | 240 | //Determine Motor Direction |
DBerendsen | 0:5f4bc2d63814 | 241 | if (magnitude2 > 0){ |
DBerendsen | 0:5f4bc2d63814 | 242 | motor2DirectionPin = 1; |
DBerendsen | 0:5f4bc2d63814 | 243 | } |
DBerendsen | 0:5f4bc2d63814 | 244 | else{ |
DBerendsen | 0:5f4bc2d63814 | 245 | motor2DirectionPin = 0; |
DBerendsen | 0:5f4bc2d63814 | 246 | } |
DBerendsen | 0:5f4bc2d63814 | 247 | } |
peterknoben | 5:b4abbd3c513c | 248 | |
peterknoben | 5:b4abbd3c513c | 249 | void motor_control(){ |
peterknoben | 5:b4abbd3c513c | 250 | position_math[0]= MoveLeft.getposition(CaseLeft, mode, 0, max_rangex); |
peterknoben | 5:b4abbd3c513c | 251 | position_math[1]= MoveRight.getposition(CaseRight, mode, 1, max_rangey); |
peterknoben | 5:b4abbd3c513c | 252 | motor1_control(); |
peterknoben | 5:b4abbd3c513c | 253 | motor2_control(); |
peterknoben | 5:b4abbd3c513c | 254 | } |
DBerendsen | 0:5f4bc2d63814 | 255 | //------------------------------------------------------------------------------ |
DBerendsen | 0:5f4bc2d63814 | 256 | //------------------------------------------------------------------------------ |
DBerendsen | 0:5f4bc2d63814 | 257 | //------------------------------------------------------------------------------ |
DBerendsen | 0:5f4bc2d63814 | 258 | |
DBerendsen | 0:5f4bc2d63814 | 259 | int main(){ |
DBerendsen | 0:5f4bc2d63814 | 260 | pc.baud(115200); |
peterknoben | 4:e15fc329b88b | 261 | setled(); |
peterknoben | 3:c768a37620c9 | 262 | BiQuad_filter.add( &LP1 ).add( &HP2 ).add( &NO3); |
peterknoben | 4:e15fc329b88b | 263 | But2.rise(&mincalibration); |
peterknoben | 4:e15fc329b88b | 264 | But1.rise(&maxcalibration); |
peterknoben | 5:b4abbd3c513c | 265 | motor1.period(0.0001f); motor2.period(0.0001f); |
peterknoben | 5:b4abbd3c513c | 266 | MyControllerTicker.attach(&motor_control, CONTROLLER_TS); |
peterknoben | 3:c768a37620c9 | 267 | MyTickerMean.attach(&signalnumber, MEAN_TS); |
peterknoben | 5:b4abbd3c513c | 268 | // MyTickerMode.attach(&signalmode, MEAN_TS); |
peterknoben | 5:b4abbd3c513c | 269 | // MyTickerMean.attach(&signalnumberright, MEAN_TS); |
peterknoben | 5:b4abbd3c513c | 270 | // MyTickerMean.attach(&signalmode,MEAN_TS); |
peterknoben | 4:e15fc329b88b | 271 | |
peterknoben | 5:b4abbd3c513c | 272 | while(1) { |
peterknoben | 5:b4abbd3c513c | 273 | pc.printf("Mean %f %f %f \r\n", mean_value_left, mean_value_right, mean_value_mode); |
peterknoben | 5:b4abbd3c513c | 274 | // pc.printf("Case %i %i %i, mode = %i \r\n", CaseLeft, CaseRight, CaseMode, mode); |
peterknoben | 5:b4abbd3c513c | 275 | wait(0.1f); |
peterknoben | 5:b4abbd3c513c | 276 | } |
peterknoben | 1:406519ff0f17 | 277 | } |
peterknoben | 1:406519ff0f17 | 278 | |
peterknoben | 1:406519ff0f17 | 279 |