Alle subfiles samengevoegd

Dependencies:   HIDScope biquadFilter mbed MODSERIAL FastPWM QEI

Committer:
sjoerdbarts
Date:
Mon Oct 31 13:05:24 2016 +0000
Revision:
12:9b0451105991
Parent:
11:417193f23342
Child:
13:e8b8b035abbd
sample before the rest of calculations for motorangles

Who changed what in which revision?

UserRevisionLine numberNew contents of line
s1600907 0:c96cf9760185 1 #include "mbed.h"
s1600907 0:c96cf9760185 2 #include "math.h"
s1600907 1:e1267e72ade8 3 #include "MODSERIAL.h"
sjoerdbarts 6:00011220b82b 4 #include "HIDScope.h"
sjoerdbarts 6:00011220b82b 5 #include "FastPWM.h"
sjoerdbarts 6:00011220b82b 6 #include "QEI.h"
sjoerdbarts 6:00011220b82b 7 #include "BiQuad.h"
sjoerdbarts 6:00011220b82b 8 #define SERIAL_BAUD 115200 // baud rate for serial communication
s1600907 0:c96cf9760185 9
sjoerdbarts 6:00011220b82b 10 // Serial connection
s1600907 1:e1267e72ade8 11 MODSERIAL pc(USBTX,USBRX);
sjoerdbarts 6:00011220b82b 12
s1600907 0:c96cf9760185 13 //EMG aansluitingen
s1600907 0:c96cf9760185 14 AnalogIn emg0( A0 ); //Biceps Rechts
s1600907 2:92593c9d6146 15 AnalogIn emg1( A1 ); //Bicpes Links
s1600907 2:92593c9d6146 16 AnalogIn emg2( A2 ); //Upper leg
sjoerdbarts 6:00011220b82b 17
sjoerdbarts 12:9b0451105991 18 // Setup Potmeters
sjoerdbarts 6:00011220b82b 19 // Potmeter 1 gives the reference position x
sjoerdbarts 6:00011220b82b 20 AnalogIn pot1(A3);
sjoerdbarts 6:00011220b82b 21 // Potmeter 2 gives the reference position y
sjoerdbarts 6:00011220b82b 22 AnalogIn pot2(A4);
sjoerdbarts 6:00011220b82b 23
sjoerdbarts 6:00011220b82b 24 // Setup Buttons
sjoerdbarts 6:00011220b82b 25 InterruptIn button1(PTB9); // button 1
sjoerdbarts 6:00011220b82b 26 InterruptIn button2(PTA1); // button 2
sjoerdbarts 6:00011220b82b 27 InterruptIn button3(PTC6); // SW2
sjoerdbarts 6:00011220b82b 28 InterruptIn button4(PTA4); // SW3
sjoerdbarts 6:00011220b82b 29
sjoerdbarts 8:676646c50aeb 30 // Setup botton values
sjoerdbarts 8:676646c50aeb 31 bool button1_value = false;
sjoerdbarts 8:676646c50aeb 32 bool button2_value = false;
sjoerdbarts 8:676646c50aeb 33
sjoerdbarts 6:00011220b82b 34 // Set motor Pinouts
sjoerdbarts 6:00011220b82b 35 DigitalOut motor1_dir(D4);
sjoerdbarts 6:00011220b82b 36 FastPWM motor1_pwm(D5);
sjoerdbarts 6:00011220b82b 37 DigitalOut motor2_dir(D7);
sjoerdbarts 6:00011220b82b 38 FastPWM motor2_pwm(D6);
sjoerdbarts 6:00011220b82b 39
sjoerdbarts 6:00011220b82b 40 // Set LED pins
sjoerdbarts 12:9b0451105991 41 DigitalOut led(D15);
sjoerdbarts 12:9b0451105991 42 DigitalOut red(LED_RED);
sjoerdbarts 12:9b0451105991 43 DigitalOut blue(LED_BLUE);
sjoerdbarts 12:9b0451105991 44 DigitalOut green(LED_GREEN);
sjoerdbarts 6:00011220b82b 45
sjoerdbarts 6:00011220b82b 46 // Set HID scope
sjoerdbarts 6:00011220b82b 47 HIDScope scope(6);
sjoerdbarts 6:00011220b82b 48
sjoerdbarts 6:00011220b82b 49 // Set encoder
sjoerdbarts 6:00011220b82b 50 QEI m1_EncoderCW(D10,D11,NC,32);
sjoerdbarts 6:00011220b82b 51 QEI m1_EncoderCCW(D11,D10,NC,32);
sjoerdbarts 6:00011220b82b 52 QEI m2_EncoderCW(D13,D12,NC,32);
sjoerdbarts 6:00011220b82b 53 QEI m2_EncoderCCW(D12,D13,NC,32);
s1600907 0:c96cf9760185 54
sjoerdbarts 7:bafc32b576c4 55 // Constants for control
sjoerdbarts 7:bafc32b576c4 56 volatile const int COUNTS_PER_REV = 8400;
sjoerdbarts 8:676646c50aeb 57 volatile const double DEGREES_PER_PULSE = 8400 / 360;
sjoerdbarts 7:bafc32b576c4 58
sjoerdbarts 7:bafc32b576c4 59 // Set initial Kp and Ki
sjoerdbarts 7:bafc32b576c4 60 volatile double Kp = 0.01; // last known good Kp: 0.01
sjoerdbarts 8:676646c50aeb 61 volatile double Ki = 0.0025; // last known good Ki: 0.0025
sjoerdbarts 7:bafc32b576c4 62 volatile double Kd = 0.0; // last known good Kd: 0.0
sjoerdbarts 7:bafc32b576c4 63
sjoerdbarts 8:676646c50aeb 64 // Set frequencies for sampling, calc and control.
sjoerdbarts 8:676646c50aeb 65 volatile const double SAMPLE_F = 500; // 500 Hz
sjoerdbarts 8:676646c50aeb 66 volatile const double SAMPLE_TS = 1.0/SAMPLE_F;
sjoerdbarts 12:9b0451105991 67 volatile const double CALC_F = 500.0; // 100 Hz
sjoerdbarts 8:676646c50aeb 68 volatile const double CALC_TS = 1.0/CALC_F;
sjoerdbarts 8:676646c50aeb 69 volatile const double CONTROLLER_TS = 0.01; // 100 Hz
sjoerdbarts 8:676646c50aeb 70
sjoerdbarts 7:bafc32b576c4 71 volatile double Ts = 0.01;
sjoerdbarts 7:bafc32b576c4 72 volatile double N = 100;
sjoerdbarts 7:bafc32b576c4 73
sjoerdbarts 7:bafc32b576c4 74 // Memory values for controllers
sjoerdbarts 7:bafc32b576c4 75 double m1_v1 = 0, m1_v2 = 0;
sjoerdbarts 7:bafc32b576c4 76 double m2_v1 = 0, m2_v2 = 0;
s1600907 0:c96cf9760185 77
s1600907 0:c96cf9760185 78 //BiQuad
s1600907 0:c96cf9760185 79 // making the biquads and chains; imported from matlab
s1600907 0:c96cf9760185 80 BiQuadChain bqc; //Chain for right biceps
s1600907 0:c96cf9760185 81 BiQuadChain bqc2; //Chain for left biceps
s1600907 0:c96cf9760185 82 BiQuadChain bqc3; //Chain for Upper leg
s1600907 0:c96cf9760185 83 // 1 to 3 are for right biceps
s1600907 0:c96cf9760185 84 BiQuad bq1( 9.14969e-01, -1.82994e+00, 9.14969e-01, -1.82269e+00, 8.37182e-01 );
s1600907 0:c96cf9760185 85 BiQuad bq2( 9.91104e-01, -1.60364e+00, 9.91104e-01, -1.60364e+00, 9.82207e-01 );
s1600907 0:c96cf9760185 86 BiQuad bq3( 8.76555e-05, 1.75311e-04, 8.76555e-05, -1.97334e+00, 9.73695e-01 );
s1600907 0:c96cf9760185 87 // 4 to 6 are for left biceps
s1600907 0:c96cf9760185 88 BiQuad bq4( 9.14969e-01, -1.82994e+00, 9.14969e-01, -1.82269e+00, 8.37182e-01 );
s1600907 0:c96cf9760185 89 BiQuad bq5( 9.91104e-01, -1.60364e+00, 9.91104e-01, -1.60364e+00, 9.82207e-01 );
s1600907 0:c96cf9760185 90 BiQuad bq6( 8.76555e-05, 1.75311e-04, 8.76555e-05, -1.97334e+00, 9.73695e-01 );
s1600907 0:c96cf9760185 91 // 7 to 9 are for upper leg
s1600907 0:c96cf9760185 92 BiQuad bq7( 9.14969e-01, -1.82994e+00, 9.14969e-01, -1.82269e+00, 8.37182e-01 );
s1600907 0:c96cf9760185 93 BiQuad bq8( 9.91104e-01, -1.60364e+00, 9.91104e-01, -1.60364e+00, 9.82207e-01 );
s1600907 0:c96cf9760185 94 BiQuad bq9( 3.91302e-05, 7.82604e-05, 3.91302e-05, -1.98223e+00, 9.82385e-01 );
s1600907 0:c96cf9760185 95
sjoerdbarts 7:bafc32b576c4 96 // Variables for filter
sjoerdbarts 7:bafc32b576c4 97 // muscle EMG < treshhold
s1600907 0:c96cf9760185 98 volatile bool BicepsLeft = false;
s1600907 0:c96cf9760185 99 volatile bool BicepsRight = false;
s1600907 0:c96cf9760185 100 volatile bool Leg = false;
s1600907 0:c96cf9760185 101
sjoerdbarts 7:bafc32b576c4 102 // Variables for MotorHoekBerekenen (MotorAngleCalculation)
s1600907 1:e1267e72ade8 103 volatile double x = 0.0; // beginpositie x en y
s1600907 1:e1267e72ade8 104 volatile double y = 305.5;
s1600907 0:c96cf9760185 105 volatile const double pi = 3.14159265359;
sjoerdbarts 10:4bd8ec9e79ff 106 volatile double Speed = 10; // Speed with which x and y are changed, in mm/s
sjoerdbarts 10:4bd8ec9e79ff 107 volatile double Theta1Gear = 60.0; // Beginpositie op 60 graden
s1600907 3:c524afedf863 108 volatile double Theta2Gear = 60.0;
s1600907 1:e1267e72ade8 109 volatile double Theta1 = Theta1Gear*42/12; // Beginpositie van MotorHoek
s1600907 1:e1267e72ade8 110 volatile double Theta2 = Theta2Gear*42/12;
sjoerdbarts 8:676646c50aeb 111
sjoerdbarts 10:4bd8ec9e79ff 112 // Stepper motor setup
sjoerdbarts 10:4bd8ec9e79ff 113 // Set constans
sjoerdbarts 10:4bd8ec9e79ff 114 volatile int stepmode = 16; // step mode
sjoerdbarts 10:4bd8ec9e79ff 115 volatile int steps = 150*stepmode; // amount of steps for 90 degree rotation = 50, full step mode: 1.8 degree per step.
sjoerdbarts 10:4bd8ec9e79ff 116 volatile int i = 0;
sjoerdbarts 10:4bd8ec9e79ff 117 volatile bool Stepper_CCW = false;
sjoerdbarts 10:4bd8ec9e79ff 118
sjoerdbarts 11:417193f23342 119 // Setup stepper motor pins
sjoerdbarts 12:9b0451105991 120 // DigitalOut pinMode(PTD2); //D11 NOW USES 3.3V OUPUT, SINCE THIS IS ALWAYS ON
sjoerdbarts 11:417193f23342 121 DigitalOut pinSleep(PTE24); //D14
sjoerdbarts 11:417193f23342 122 DigitalOut pinStep(PTC4); //D9
sjoerdbarts 11:417193f23342 123 DigitalOut pinDir(PTC12); //D8
sjoerdbarts 11:417193f23342 124
sjoerdbarts 11:417193f23342 125 // Control of the stepper motor with 1 Ticker and 1 Timeout
sjoerdbarts 11:417193f23342 126 Ticker TickerStepper;
sjoerdbarts 11:417193f23342 127 Timeout TimeoutStepper;
sjoerdbarts 11:417193f23342 128
sjoerdbarts 10:4bd8ec9e79ff 129 // Global variable Step_State
s1600907 0:c96cf9760185 130 volatile bool Stepper_State = false; // false = we zijn niet bezig met flippen
sjoerdbarts 10:4bd8ec9e79ff 131
sjoerdbarts 10:4bd8ec9e79ff 132 /////////////////////// Functtions for stepper motor /////////////////////////////////////
sjoerdbarts 10:4bd8ec9e79ff 133 // Set the Step pin to 0
sjoerdbarts 10:4bd8ec9e79ff 134 void StepperFall()
sjoerdbarts 10:4bd8ec9e79ff 135 {
sjoerdbarts 10:4bd8ec9e79ff 136 pinStep = 0;
sjoerdbarts 10:4bd8ec9e79ff 137 }
sjoerdbarts 10:4bd8ec9e79ff 138 // Turn off stepper and power off driver board. Reset Stepper_CCS, i and Stepper_State
sjoerdbarts 10:4bd8ec9e79ff 139 void Stepper_Off()
sjoerdbarts 10:4bd8ec9e79ff 140 {
sjoerdbarts 10:4bd8ec9e79ff 141 TickerStepper.detach();
sjoerdbarts 10:4bd8ec9e79ff 142 pinSleep = 0;
sjoerdbarts 10:4bd8ec9e79ff 143 i=0;
sjoerdbarts 10:4bd8ec9e79ff 144 Stepper_State = false;
sjoerdbarts 10:4bd8ec9e79ff 145 Stepper_CCW = false;
sjoerdbarts 10:4bd8ec9e79ff 146 }
sjoerdbarts 10:4bd8ec9e79ff 147
sjoerdbarts 10:4bd8ec9e79ff 148 // Change direction
sjoerdbarts 10:4bd8ec9e79ff 149 void Stepper_Change_Dir()
sjoerdbarts 10:4bd8ec9e79ff 150 {
sjoerdbarts 10:4bd8ec9e79ff 151 pinDir = true;
sjoerdbarts 10:4bd8ec9e79ff 152 Stepper_CCW = true;
sjoerdbarts 10:4bd8ec9e79ff 153 i = 0;
sjoerdbarts 10:4bd8ec9e79ff 154 }
sjoerdbarts 10:4bd8ec9e79ff 155
sjoerdbarts 10:4bd8ec9e79ff 156 // Move the motor 1 step
sjoerdbarts 10:4bd8ec9e79ff 157 void StepperRise()
sjoerdbarts 10:4bd8ec9e79ff 158 {
sjoerdbarts 10:4bd8ec9e79ff 159 if((i<steps)&&(not Stepper_CCW))
sjoerdbarts 10:4bd8ec9e79ff 160 {
sjoerdbarts 10:4bd8ec9e79ff 161 pinStep=1; // Set stepper to 1
sjoerdbarts 10:4bd8ec9e79ff 162 // attach timeout to set it back to 0, min step duration is 1.8 us
sjoerdbarts 10:4bd8ec9e79ff 163 TimeoutStepper.attach_us(&StepperFall, 2);
sjoerdbarts 10:4bd8ec9e79ff 164 i=i+1;
sjoerdbarts 10:4bd8ec9e79ff 165 }
sjoerdbarts 10:4bd8ec9e79ff 166 else if (Stepper_CCW)
sjoerdbarts 10:4bd8ec9e79ff 167 {
sjoerdbarts 10:4bd8ec9e79ff 168 if(i<steps)
sjoerdbarts 10:4bd8ec9e79ff 169 {
sjoerdbarts 10:4bd8ec9e79ff 170 pinStep=1;
sjoerdbarts 10:4bd8ec9e79ff 171 TimeoutStepper.attach_us(&StepperFall, 2);
sjoerdbarts 10:4bd8ec9e79ff 172 i=i+1;
sjoerdbarts 10:4bd8ec9e79ff 173 }
sjoerdbarts 10:4bd8ec9e79ff 174 else
sjoerdbarts 10:4bd8ec9e79ff 175 {
sjoerdbarts 10:4bd8ec9e79ff 176 Stepper_Off();
sjoerdbarts 10:4bd8ec9e79ff 177 }
sjoerdbarts 10:4bd8ec9e79ff 178 }
sjoerdbarts 10:4bd8ec9e79ff 179 else
sjoerdbarts 10:4bd8ec9e79ff 180 {
sjoerdbarts 10:4bd8ec9e79ff 181 Stepper_Change_Dir();
sjoerdbarts 10:4bd8ec9e79ff 182 }
sjoerdbarts 10:4bd8ec9e79ff 183 }
sjoerdbarts 10:4bd8ec9e79ff 184
sjoerdbarts 10:4bd8ec9e79ff 185 // Activate the stepper motor
sjoerdbarts 10:4bd8ec9e79ff 186 // at a constant speed
sjoerdbarts 10:4bd8ec9e79ff 187 void Stepper_On()
sjoerdbarts 10:4bd8ec9e79ff 188 {
sjoerdbarts 10:4bd8ec9e79ff 189 // Set global variable to true
sjoerdbarts 10:4bd8ec9e79ff 190 Stepper_State = true;
sjoerdbarts 10:4bd8ec9e79ff 191 // Power on the board and set direction to CW
sjoerdbarts 10:4bd8ec9e79ff 192 pinSleep = 1;
sjoerdbarts 10:4bd8ec9e79ff 193 pinDir = false;
sjoerdbarts 10:4bd8ec9e79ff 194 // Calc the speed of the stepper
sjoerdbarts 10:4bd8ec9e79ff 195 int nPPS = 50*stepmode; // was 1200, this controlls the speed, keep this times the step mode
sjoerdbarts 11:417193f23342 196 double fFrequency_Hz = 1.f * nPPS;
sjoerdbarts 11:417193f23342 197 double fPeriod_s = 1.0f / fFrequency_Hz;
sjoerdbarts 10:4bd8ec9e79ff 198 // Check if the speed is too fast small and report back if that is the case
sjoerdbarts 10:4bd8ec9e79ff 199 if (fPeriod_s < 2e-6)
sjoerdbarts 10:4bd8ec9e79ff 200 {
sjoerdbarts 10:4bd8ec9e79ff 201 pc.printf("\r\n ERROR: fPeriod_s too small \r\n");
sjoerdbarts 10:4bd8ec9e79ff 202 fPeriod_s=2e-6;
sjoerdbarts 10:4bd8ec9e79ff 203 }
sjoerdbarts 10:4bd8ec9e79ff 204
sjoerdbarts 10:4bd8ec9e79ff 205 TickerStepper.attach(&StepperRise, // void(*fptr)(void)
sjoerdbarts 10:4bd8ec9e79ff 206 fPeriod_s // float t
sjoerdbarts 10:4bd8ec9e79ff 207 );
sjoerdbarts 10:4bd8ec9e79ff 208 }
s1600907 0:c96cf9760185 209
sjoerdbarts 7:bafc32b576c4 210 /////////////////////// functions for filters /////////////////////////////////////////////
s1600907 0:c96cf9760185 211 void sample()
s1600907 0:c96cf9760185 212 {
s1600907 0:c96cf9760185 213 /* Sample function
s1600907 0:c96cf9760185 214 samples the emg0 (of the rightBiceps)
s1600907 0:c96cf9760185 215 samples the emg1 (of the leftBiceps)
s1600907 0:c96cf9760185 216 samples the emg2 (of the Upper leg)
s1600907 0:c96cf9760185 217 filter the singals
s1600907 0:c96cf9760185 218 sends it to HIDScope
s1600907 0:c96cf9760185 219 */
s1600907 0:c96cf9760185 220
s1600907 0:c96cf9760185 221 // Rechts Biceps
s1600907 0:c96cf9760185 222 double inRechts = emg0.read(); // EMG signal
s1600907 0:c96cf9760185 223 double FilterRechts = bqc.step(inRechts); // High pass + Notch (50 HZ)
s1600907 0:c96cf9760185 224 double RectifyRechts = fabs(FilterRechts); // Rectify
s1600907 0:c96cf9760185 225 double outRechts = bq3.step(RectifyRechts); // Low pass
s1600907 2:92593c9d6146 226
s1600907 0:c96cf9760185 227 // Links Biceps
s1600907 0:c96cf9760185 228 double inLinks = emg1.read(); // EMG signal
s1600907 0:c96cf9760185 229 double FilterLinks = bqc2.step(inLinks); // High pass + Notch (50 HZ)
s1600907 0:c96cf9760185 230 double RectifyLinks = fabs(FilterLinks); // Rectify
s1600907 0:c96cf9760185 231 double outLinks = bq6.step(RectifyLinks); // Low pass
s1600907 0:c96cf9760185 232
s1600907 0:c96cf9760185 233 // Upper leg
s1600907 0:c96cf9760185 234 double inBeen = emg2.read(); // EMG signal
s1600907 0:c96cf9760185 235 double FilterBeen = bqc3.step(inBeen); // High pass + Notch (50 HZ)
s1600907 0:c96cf9760185 236 double RectifyBeen = fabs(FilterBeen); // Rectify
s1600907 2:92593c9d6146 237 double outBeen = bq9.step(RectifyBeen); // Low pass
s1600907 0:c96cf9760185 238
s1600907 0:c96cf9760185 239
s1600907 0:c96cf9760185 240 /* EMG signal in channel 0 (the first channel)
s1600907 0:c96cf9760185 241 and the filtered EMG signal in channel 1 (the second channel)
s1600907 0:c96cf9760185 242 of the HIDscope */
s1600907 5:b4a0301ec8cc 243 /*scope.set(0,inRechts);
s1600907 0:c96cf9760185 244 scope.set(1,outRechts);
s1600907 2:92593c9d6146 245 scope.set(2,inLinks);
s1600907 0:c96cf9760185 246 scope.set(3,outLinks);
s1600907 0:c96cf9760185 247 scope.set(4,inBeen);
s1600907 0:c96cf9760185 248 scope.set(5,outBeen);
s1600907 5:b4a0301ec8cc 249 */
s1600907 0:c96cf9760185 250 // To indicate that the function is working, the LED is on
s1600907 1:e1267e72ade8 251 if (outRechts >= 0.04){
s1600907 0:c96cf9760185 252 BicepsRight = true;
s1600907 0:c96cf9760185 253 }
s1600907 0:c96cf9760185 254 else{
s1600907 0:c96cf9760185 255 BicepsRight = false;
s1600907 0:c96cf9760185 256 }
s1600907 2:92593c9d6146 257
s1600907 0:c96cf9760185 258 // If Biceps links is actuated then:
s1600907 1:e1267e72ade8 259 if (outLinks >= 0.04){
s1600907 0:c96cf9760185 260 BicepsLeft = true;
s1600907 0:c96cf9760185 261 }
s1600907 0:c96cf9760185 262 else{
s1600907 0:c96cf9760185 263 BicepsLeft = false;
s1600907 0:c96cf9760185 264 }
s1600907 0:c96cf9760185 265 // If upper leg is actuated then:
s1600907 0:c96cf9760185 266 if (outBeen >= 0.01){
s1600907 0:c96cf9760185 267 Leg = true;
s1600907 0:c96cf9760185 268 }
s1600907 0:c96cf9760185 269 else{
s1600907 0:c96cf9760185 270 Leg = false;
s1600907 0:c96cf9760185 271 }
s1600907 0:c96cf9760185 272 }
s1600907 0:c96cf9760185 273
sjoerdbarts 7:bafc32b576c4 274 ///////////////////// functions for "motorhoekberekenen" //////////////////////////
sjoerdbarts 7:bafc32b576c4 275 // save previous x
s1600907 0:c96cf9760185 276 double Calc_Prev_x () {
s1600907 0:c96cf9760185 277 double Prev_x = x;
s1600907 0:c96cf9760185 278 //pc.printf("prev x = %f\r\n", Prev_x);
s1600907 0:c96cf9760185 279 return Prev_x;
s1600907 0:c96cf9760185 280 }
s1600907 0:c96cf9760185 281
sjoerdbarts 7:bafc32b576c4 282 // save previous y
s1600907 0:c96cf9760185 283 double Calc_Prev_y () {
s1600907 0:c96cf9760185 284 double Prev_y = y;
s1600907 0:c96cf9760185 285 //pc.printf("prev y = %f\r\n", Prev_y);
s1600907 0:c96cf9760185 286 return Prev_y;
s1600907 0:c96cf9760185 287 }
s1600907 0:c96cf9760185 288
sjoerdbarts 7:bafc32b576c4 289 // save previous Theta1Gear
s1600907 4:05c5da1c8b08 290 double Calc_Prev_Theta1_Gear () {
s1600907 4:05c5da1c8b08 291 double Prev_Theta1_Gear = Theta1Gear;
s1600907 4:05c5da1c8b08 292 return Prev_Theta1_Gear;
s1600907 4:05c5da1c8b08 293 }
s1600907 4:05c5da1c8b08 294
sjoerdbarts 7:bafc32b576c4 295 // save previous Theta2Gear
s1600907 4:05c5da1c8b08 296 double Calc_Prev_Theta2_Gear () {
s1600907 4:05c5da1c8b08 297 double Prev_Theta2_Gear = Theta2Gear;
s1600907 4:05c5da1c8b08 298 return Prev_Theta2_Gear;
s1600907 4:05c5da1c8b08 299 }
s1600907 4:05c5da1c8b08 300
sjoerdbarts 8:676646c50aeb 301 void CalcXY ()
sjoerdbarts 7:bafc32b576c4 302 {
sjoerdbarts 8:676646c50aeb 303 // calc steps in mm
sjoerdbarts 8:676646c50aeb 304 double Step = Speed/CALC_F ; //10 mm per seconde afleggen
s1600907 0:c96cf9760185 305
s1600907 0:c96cf9760185 306 if (BicepsLeft==true && BicepsRight==true && Leg==true && Stepper_State==false) {
sjoerdbarts 11:417193f23342 307 Stepper_On();
s1600907 0:c96cf9760185 308 }
s1600907 0:c96cf9760185 309 else if (BicepsLeft==true && BicepsRight==false && Leg==false && Stepper_State==false) {
s1600907 0:c96cf9760185 310 x = x - Step;
s1600907 0:c96cf9760185 311 }
s1600907 0:c96cf9760185 312 else if (BicepsLeft==false && BicepsRight==true && Leg==false && Stepper_State==false) {
s1600907 0:c96cf9760185 313 x = x + Step; // naar Right bewegen
s1600907 0:c96cf9760185 314 }
s1600907 0:c96cf9760185 315 else if (BicepsLeft==true && BicepsRight==true && Leg==false && Stepper_State==false) {
s1600907 0:c96cf9760185 316 y = y + Step; // naar voren bewegen
s1600907 0:c96cf9760185 317 }
s1600907 0:c96cf9760185 318 else if (BicepsLeft==false && BicepsRight==true && Leg==true && Stepper_State==false) {
s1600907 0:c96cf9760185 319 y = y - Step; // naar achter bewegen
s1600907 0:c96cf9760185 320 }
s1600907 0:c96cf9760185 321 else if (BicepsLeft==false && BicepsRight==false && Leg==false || Stepper_State==true) {
s1600907 0:c96cf9760185 322 }
s1600907 0:c96cf9760185 323
s1600907 0:c96cf9760185 324 // Grenswaardes LET OP: ARMEN MISSCHIEN GEBLOKKEERD DOOR BALK AAN DE BINNENKANT
s1600907 0:c96cf9760185 325 if (x > 200) {
s1600907 0:c96cf9760185 326 x = 200;
s1600907 0:c96cf9760185 327 }
s1600907 0:c96cf9760185 328 else if (x < -200) {
s1600907 0:c96cf9760185 329 x = -200;
s1600907 0:c96cf9760185 330 }
s1600907 0:c96cf9760185 331 if (y > 306) {
s1600907 0:c96cf9760185 332 y = 306;
s1600907 0:c96cf9760185 333 }
s1600907 0:c96cf9760185 334 else if (y < 50) {
s1600907 0:c96cf9760185 335 y = 50; // GOKJE, UITPROBEREN
s1600907 0:c96cf9760185 336 }
s1600907 5:b4a0301ec8cc 337 //pc.printf("x = %f, y = %f\r\n", x, y);
s1600907 1:e1267e72ade8 338
s1600907 2:92593c9d6146 339 //scope.set(2,x);
s1600907 2:92593c9d6146 340 //scope.set(3,y);
s1600907 0:c96cf9760185 341 }
s1600907 0:c96cf9760185 342
s1600907 0:c96cf9760185 343 // diagonaal berekenen voor linker arm
s1600907 0:c96cf9760185 344 double CalcDia1 (double x, double y) {
s1600907 0:c96cf9760185 345 double a = 50.0; // de afstand van gekozen assenstelsel tot de armas (assenstelsel precies in het midden) KEERTJE NAMETEN
s1600907 0:c96cf9760185 346 double BV1 = sqrt(pow((a+x),2) + pow(y,2)); // diagonaal (afstand van armas tot locatie) berekenen
s1600907 0:c96cf9760185 347 double Dia1 = pow(BV1,2)/(2*BV1); // berekenen van de afstand oorsprong tot diagonaal
s1600907 0:c96cf9760185 348
s1600907 0:c96cf9760185 349 //pc.printf("dia = %f, x = %f, y= %f\r\n", Dia1, x, y);
s1600907 0:c96cf9760185 350 return Dia1;
s1600907 0:c96cf9760185 351 }
s1600907 0:c96cf9760185 352
s1600907 0:c96cf9760185 353 // diagonaal berekenen voor rechter arm
s1600907 0:c96cf9760185 354 double CalcDia2 (double x, double y) {
s1600907 0:c96cf9760185 355 double a = 50.0;
s1600907 0:c96cf9760185 356 double BV2 = sqrt(pow((x-a),2) + pow(y,2)); // zelfde nog een keer doen maar nu voor de rechter arm
s1600907 0:c96cf9760185 357 double Dia2 = pow(BV2,2)/(2*BV2);
s1600907 0:c96cf9760185 358
s1600907 0:c96cf9760185 359 //pc.printf("dia = %f, x = %f, y= %f\r\n", Dia2, x, y);
s1600907 0:c96cf9760185 360 return Dia2;
s1600907 0:c96cf9760185 361 }
s1600907 0:c96cf9760185 362
s1600907 0:c96cf9760185 363 // calculate Theta1
s1600907 4:05c5da1c8b08 364 void CalcTheta1 (double Dia1) {
s1600907 0:c96cf9760185 365 double a = 50.0;
s1600907 0:c96cf9760185 366 double Bar = 200.0; // lengte van de armen
s1600907 0:c96cf9760185 367
s1600907 0:c96cf9760185 368 // Hoek berekenen van het grote tandwiel (gear)
s1600907 0:c96cf9760185 369 if (x > -a) {
s1600907 0:c96cf9760185 370 Theta1Gear = pi - atan(y/(x+a)) - acos(Dia1/Bar);
s1600907 0:c96cf9760185 371 }
s1600907 0:c96cf9760185 372 else if (x > -a) {
s1600907 0:c96cf9760185 373 Theta1Gear = pi - (pi + atan(y/(x+a))) - acos(Dia1/Bar);
s1600907 0:c96cf9760185 374 }
s1600907 0:c96cf9760185 375 else { // als x=-a
s1600907 0:c96cf9760185 376 Theta1Gear = 0.5*pi - acos(Dia1/Bar);
s1600907 0:c96cf9760185 377 }
s1600907 3:c524afedf863 378 Theta1Gear = Theta1Gear*180.0/pi; // veranderen van radialen naar graden
s1600907 0:c96cf9760185 379
s1600907 0:c96cf9760185 380 // omrekenen van grote tandwiel naar motortandwiel
s1600907 0:c96cf9760185 381 Theta1 = Theta1Gear*42.0/12.0; // grote tandwiel heeft 42 tanden. Motortandwiel heeft er 12.
s1600907 0:c96cf9760185 382
s1600907 5:b4a0301ec8cc 383 pc.printf("thetaMotor = %f, thetaGear = %f\r\n", Theta1, Theta1Gear);
s1600907 0:c96cf9760185 384 }
s1600907 0:c96cf9760185 385
s1600907 4:05c5da1c8b08 386 void CalcTheta2 (double Dia2) {
s1600907 0:c96cf9760185 387 double a = 50.0;
s1600907 0:c96cf9760185 388 double Bar = 200.0; // lengte van de armen
s1600907 0:c96cf9760185 389 double Prev_Theta2_Gear = Theta2Gear;
s1600907 4:05c5da1c8b08 390
s1600907 0:c96cf9760185 391 // Hoek berekenen van het grote tandwiel (gear)
s1600907 0:c96cf9760185 392 if (x < a) {
s1600907 0:c96cf9760185 393 Theta2Gear = pi + atan(y/(x-a)) - acos(Dia2/Bar);
s1600907 0:c96cf9760185 394 }
s1600907 0:c96cf9760185 395 else if (x > a) {
s1600907 0:c96cf9760185 396 Theta2Gear = pi - (pi - atan(y/(x-a))) - acos(Dia2/Bar);
s1600907 0:c96cf9760185 397 }
s1600907 0:c96cf9760185 398 else { // als x=a
s1600907 0:c96cf9760185 399 Theta2Gear = 0.5*pi - acos(Dia2/Bar);
s1600907 0:c96cf9760185 400 }
s1600907 3:c524afedf863 401 Theta2Gear = Theta2Gear*180/pi; // veranderen van radialen naar graden
s1600907 0:c96cf9760185 402
s1600907 4:05c5da1c8b08 403 // omrekenen van grote tandwiel naar motortandwiel
s1600907 4:05c5da1c8b08 404 Theta2 = Theta2Gear*42.0/12.0; // grote tandwiel heeft 42 tanden. Motortandwiel heeft er 12.
s1600907 4:05c5da1c8b08 405
s1600907 5:b4a0301ec8cc 406 pc.printf("thetaMotor = %f, thetaGear = %f\r\n", Theta2, Theta2Gear);
s1600907 4:05c5da1c8b08 407 }
s1600907 4:05c5da1c8b08 408
s1600907 4:05c5da1c8b08 409 // als een van de hoeken te groot wordt, zet dan alles terug naar de vorige positie
s1600907 4:05c5da1c8b08 410 void AngleLimits (double Prev_Theta1_Gear, double Prev_Theta2_Gear, double Prev_x, double Prev_y) {
s1600907 4:05c5da1c8b08 411 double MaxThetaGear = 100.0; // de hoek voordat arm het opstakel raakt (max hoek is 107.62 tussen arm en opstakel, maken er 100 van voor veiligheid)
s1600907 4:05c5da1c8b08 412
s1600907 4:05c5da1c8b08 413 if (Theta1Gear >= MaxThetaGear || Theta2Gear >= MaxThetaGear) {
s1600907 4:05c5da1c8b08 414 Theta1Gear = Prev_Theta1_Gear;
s1600907 0:c96cf9760185 415 Theta2Gear = Prev_Theta2_Gear;
s1600907 0:c96cf9760185 416 x = Prev_x;
s1600907 0:c96cf9760185 417 y = Prev_y;
s1600907 4:05c5da1c8b08 418
s1600907 4:05c5da1c8b08 419 Theta1 = Theta1Gear*42.0/12.0;
s1600907 4:05c5da1c8b08 420 Theta2 = Theta2Gear*42.0/12.0;
s1600907 0:c96cf9760185 421 }
s1600907 5:b4a0301ec8cc 422 pc.printf("thetaMotor = %f, thetaGear = %f\r\n", Theta2, Theta2Gear);
s1600907 0:c96cf9760185 423 }
s1600907 0:c96cf9760185 424
s1600907 0:c96cf9760185 425 void CalculationsForTheta () {
sjoerdbarts 12:9b0451105991 426 sample();
s1600907 0:c96cf9760185 427 double Prev_x = Calc_Prev_x ();
s1600907 0:c96cf9760185 428 double Prev_y = Calc_Prev_y ();
sjoerdbarts 8:676646c50aeb 429 double Prev_Theta1_Gear = Calc_Prev_Theta1_Gear ();
sjoerdbarts 8:676646c50aeb 430 double Prev_Theta2_Gear = Calc_Prev_Theta2_Gear ();
sjoerdbarts 8:676646c50aeb 431 CalcXY();
s1600907 0:c96cf9760185 432 double Dia1 = CalcDia1 (x, y);
s1600907 0:c96cf9760185 433 double Dia2 = CalcDia2 (x, y);
s1600907 4:05c5da1c8b08 434 CalcTheta1 (Dia1);
sjoerdbarts 8:676646c50aeb 435 CalcTheta2 (Dia2);
sjoerdbarts 8:676646c50aeb 436 AngleLimits (Prev_Theta1_Gear, Prev_Theta2_Gear, Prev_x, Prev_y); // laatste check
s1600907 0:c96cf9760185 437 }
s1600907 0:c96cf9760185 438
sjoerdbarts 8:676646c50aeb 439 ////////////////////////// Code for control /////////////////////////////////////
sjoerdbarts 8:676646c50aeb 440
sjoerdbarts 8:676646c50aeb 441 double m1_GetPosition()
sjoerdbarts 8:676646c50aeb 442 {
sjoerdbarts 8:676646c50aeb 443 int countsCW = m1_EncoderCW.getPulses();
sjoerdbarts 8:676646c50aeb 444 int countsCCW= m1_EncoderCCW.getPulses();
sjoerdbarts 8:676646c50aeb 445 int net_counts=countsCW-countsCCW;
sjoerdbarts 8:676646c50aeb 446 double Position=(net_counts*360.0)/COUNTS_PER_REV;
sjoerdbarts 8:676646c50aeb 447 return Position;
sjoerdbarts 8:676646c50aeb 448 }
sjoerdbarts 8:676646c50aeb 449
sjoerdbarts 8:676646c50aeb 450 double m2_GetPosition()
sjoerdbarts 8:676646c50aeb 451 {
sjoerdbarts 8:676646c50aeb 452 int countsCW = m2_EncoderCW.getPulses();
sjoerdbarts 8:676646c50aeb 453 int countsCCW= m2_EncoderCCW.getPulses();
sjoerdbarts 8:676646c50aeb 454 int net_counts=countsCW-countsCCW;
sjoerdbarts 8:676646c50aeb 455 double Position=(net_counts*360.0)/COUNTS_PER_REV;
sjoerdbarts 8:676646c50aeb 456 return Position;
sjoerdbarts 8:676646c50aeb 457 }
sjoerdbarts 8:676646c50aeb 458
sjoerdbarts 8:676646c50aeb 459 // Position control when calibrating
sjoerdbarts 8:676646c50aeb 460 double m1_GetPosition_cal()
sjoerdbarts 8:676646c50aeb 461 {
sjoerdbarts 8:676646c50aeb 462 int countsCW = m1_EncoderCW.getPulses();
sjoerdbarts 8:676646c50aeb 463 int countsCCW= m1_EncoderCCW.getPulses();
sjoerdbarts 8:676646c50aeb 464 int net_counts=countsCW-countsCCW;
sjoerdbarts 8:676646c50aeb 465 double Position=(net_counts*360.0)/COUNTS_PER_REV+210.0f; // calibrated position is 210 degrees
sjoerdbarts 8:676646c50aeb 466 return Position;
sjoerdbarts 8:676646c50aeb 467 }
sjoerdbarts 8:676646c50aeb 468
sjoerdbarts 8:676646c50aeb 469 // Position control when calibrating
sjoerdbarts 8:676646c50aeb 470 double m2_GetPosition_cal()
sjoerdbarts 8:676646c50aeb 471 {
sjoerdbarts 8:676646c50aeb 472 int countsCW = m2_EncoderCW.getPulses();
sjoerdbarts 8:676646c50aeb 473 int countsCCW= m2_EncoderCCW.getPulses();
sjoerdbarts 8:676646c50aeb 474 int net_counts=countsCW-countsCCW;
sjoerdbarts 8:676646c50aeb 475 double Position=(net_counts*360.0)/COUNTS_PER_REV+210.0f; // calibrated position is 210 degrees
sjoerdbarts 8:676646c50aeb 476 return Position;
sjoerdbarts 8:676646c50aeb 477 }
sjoerdbarts 8:676646c50aeb 478 double m1_PID(double error, const double Kp, const double Ki, const double Kd, const double Ts, const double N, double &m1_v1, double &m1_v2)
sjoerdbarts 8:676646c50aeb 479 {
sjoerdbarts 8:676646c50aeb 480 double a1 = -4/(N*Ts+2),
sjoerdbarts 8:676646c50aeb 481 a2 = -1*(N*Ts - 2)/(N*Ts+2),
sjoerdbarts 8:676646c50aeb 482 b0 = (4*Kp + 4*Kd*N + 2*Ki*Ts + 2*Kp*N*Ts + Ki*N*pow(Ts,2))/(2*N*Ts + 4),
sjoerdbarts 8:676646c50aeb 483 b1 = (Ki*N*pow(Ts,2) - 4*Kp - 4*Kd*N)/(N*Ts + 2),
sjoerdbarts 8:676646c50aeb 484 b2 = (4*Kp + 4*Kd*N - 2*Ki*Ts - 2*Kp*N*Ts + Ki*N*pow(Ts,2))/(2*N*Ts + 4);
sjoerdbarts 8:676646c50aeb 485
sjoerdbarts 8:676646c50aeb 486 double v = error - a1*m1_v1 - a2*m1_v2;
sjoerdbarts 8:676646c50aeb 487 double u = b0*v + b1*m1_v1 + b2*m1_v2;
sjoerdbarts 8:676646c50aeb 488 m1_v2 = m1_v1; m1_v1 = v;
sjoerdbarts 8:676646c50aeb 489 return u;
sjoerdbarts 8:676646c50aeb 490 }
sjoerdbarts 8:676646c50aeb 491
sjoerdbarts 8:676646c50aeb 492 double m2_PID(double error, const double Kp, const double Ki, const double Kd, const double Ts, const double N, double &m2_v1, double &m2_v2)
sjoerdbarts 8:676646c50aeb 493 {
sjoerdbarts 8:676646c50aeb 494 double a1 = -4/(N*Ts+2),
sjoerdbarts 8:676646c50aeb 495 a2 = -1*(N*Ts - 2)/(N*Ts+2),
sjoerdbarts 8:676646c50aeb 496 b0 = (4*Kp + 4*Kd*N + 2*Ki*Ts + 2*Kp*N*Ts + Ki*N*pow(Ts,2))/(2*N*Ts + 4),
sjoerdbarts 8:676646c50aeb 497 b1 = (Ki*N*pow(Ts,2) - 4*Kp - 4*Kd*N)/(N*Ts + 2),
sjoerdbarts 8:676646c50aeb 498 b2 = (4*Kp + 4*Kd*N - 2*Ki*Ts - 2*Kp*N*Ts + Ki*N*pow(Ts,2))/(2*N*Ts + 4);
sjoerdbarts 8:676646c50aeb 499
sjoerdbarts 8:676646c50aeb 500 double v = error - a1*m1_v1 - a2*m1_v2;
sjoerdbarts 8:676646c50aeb 501 double u = b0*v + b1*m1_v1 + b2*m1_v2;
sjoerdbarts 8:676646c50aeb 502 m1_v2 = m1_v1; m1_v1 = v;
sjoerdbarts 8:676646c50aeb 503 return u;
sjoerdbarts 8:676646c50aeb 504 }
sjoerdbarts 8:676646c50aeb 505
sjoerdbarts 8:676646c50aeb 506
s1600907 0:c96cf9760185 507
sjoerdbarts 8:676646c50aeb 508 void SetMotor(int motor_number, double MotorValue)
sjoerdbarts 8:676646c50aeb 509 {
sjoerdbarts 8:676646c50aeb 510 // Given -1<=motorValue<=1, this sets the PWM and direction
sjoerdbarts 8:676646c50aeb 511 // bits for motor 1. Positive value makes motor rotating
sjoerdbarts 8:676646c50aeb 512 // clockwise. motorValues outside range are truncated to
sjoerdbarts 8:676646c50aeb 513 // within range
sjoerdbarts 8:676646c50aeb 514 if (motor_number == 1)
sjoerdbarts 8:676646c50aeb 515 {
sjoerdbarts 8:676646c50aeb 516 if (MotorValue >=0)
sjoerdbarts 8:676646c50aeb 517 {
sjoerdbarts 8:676646c50aeb 518 motor1_dir=0;
sjoerdbarts 8:676646c50aeb 519 }
sjoerdbarts 8:676646c50aeb 520 else
sjoerdbarts 8:676646c50aeb 521 {
sjoerdbarts 8:676646c50aeb 522 motor1_dir=1;
sjoerdbarts 8:676646c50aeb 523 }
sjoerdbarts 8:676646c50aeb 524 if (fabs(MotorValue)>1){
sjoerdbarts 8:676646c50aeb 525 motor1_pwm.write(1);
sjoerdbarts 8:676646c50aeb 526 }
sjoerdbarts 8:676646c50aeb 527 else
sjoerdbarts 8:676646c50aeb 528 {
sjoerdbarts 8:676646c50aeb 529 motor1_pwm.write(abs(MotorValue));
sjoerdbarts 8:676646c50aeb 530 }
sjoerdbarts 8:676646c50aeb 531 }
sjoerdbarts 8:676646c50aeb 532 else
sjoerdbarts 8:676646c50aeb 533 {
sjoerdbarts 8:676646c50aeb 534 if (MotorValue >=0)
sjoerdbarts 8:676646c50aeb 535 {
sjoerdbarts 8:676646c50aeb 536 motor2_dir=0;
sjoerdbarts 8:676646c50aeb 537 }
sjoerdbarts 8:676646c50aeb 538 else
sjoerdbarts 8:676646c50aeb 539 {
sjoerdbarts 8:676646c50aeb 540 motor2_dir=1;
sjoerdbarts 8:676646c50aeb 541 }
sjoerdbarts 8:676646c50aeb 542 if (fabs(MotorValue)>1){
sjoerdbarts 8:676646c50aeb 543 motor2_pwm.write(1);
sjoerdbarts 8:676646c50aeb 544 }
sjoerdbarts 8:676646c50aeb 545 else
sjoerdbarts 8:676646c50aeb 546 {
sjoerdbarts 8:676646c50aeb 547 motor2_pwm.write(abs(MotorValue));
sjoerdbarts 8:676646c50aeb 548 }
sjoerdbarts 8:676646c50aeb 549 }
sjoerdbarts 8:676646c50aeb 550 }
sjoerdbarts 8:676646c50aeb 551
sjoerdbarts 8:676646c50aeb 552 void BlinkLed(){
sjoerdbarts 8:676646c50aeb 553 led = not led;
sjoerdbarts 8:676646c50aeb 554 }
sjoerdbarts 8:676646c50aeb 555
sjoerdbarts 8:676646c50aeb 556 void Controller_motor()
sjoerdbarts 8:676646c50aeb 557 {
sjoerdbarts 8:676646c50aeb 558 // get the actual position
sjoerdbarts 8:676646c50aeb 559 double m1_Position = m1_GetPosition_cal();
sjoerdbarts 8:676646c50aeb 560 double m2_Position = m2_GetPosition_cal();
sjoerdbarts 8:676646c50aeb 561 // Set position scopes
sjoerdbarts 8:676646c50aeb 562 scope.set(0,x);
sjoerdbarts 8:676646c50aeb 563 scope.set(1,y);
sjoerdbarts 8:676646c50aeb 564 scope.set(2,Theta1);
sjoerdbarts 8:676646c50aeb 565 scope.set(3,Theta2);
sjoerdbarts 8:676646c50aeb 566 /*
sjoerdbarts 8:676646c50aeb 567 scope.set(0,Theta1);
sjoerdbarts 8:676646c50aeb 568 scope.set(1,m1_Position);
sjoerdbarts 8:676646c50aeb 569 scope.set(3,Theta2);
sjoerdbarts 8:676646c50aeb 570 scope.set(4,m2_Position);
sjoerdbarts 8:676646c50aeb 571 */
sjoerdbarts 8:676646c50aeb 572 //scope.set(2,m1_Position);
sjoerdbarts 8:676646c50aeb 573 //scope.set(4,m2_Position);
sjoerdbarts 8:676646c50aeb 574 // calc the error
sjoerdbarts 8:676646c50aeb 575 double m1_error = Theta1 - m1_Position;
sjoerdbarts 8:676646c50aeb 576 double m2_error = Theta2 - m2_Position;
sjoerdbarts 8:676646c50aeb 577 //scope.set(2,m1_error);
sjoerdbarts 8:676646c50aeb 578 //scope.set(6,m2_error);
sjoerdbarts 8:676646c50aeb 579 // calc motorvalues for controller;
sjoerdbarts 8:676646c50aeb 580 double m1_MotorValue = m1_PID(m1_error, Kp, Ki, Kd, Ts, N, m1_v1, m1_v2);
sjoerdbarts 8:676646c50aeb 581 double m2_MotorValue = m2_PID(m2_error, Kp, Ki, Kd, Ts, N, m2_v1, m2_v2);
sjoerdbarts 8:676646c50aeb 582 scope.set(4,m1_MotorValue);
sjoerdbarts 8:676646c50aeb 583 scope.set(5,m2_MotorValue);
sjoerdbarts 8:676646c50aeb 584 // Set the motorvalues
sjoerdbarts 10:4bd8ec9e79ff 585 // SetMotor(1, m1_MotorValue);
sjoerdbarts 10:4bd8ec9e79ff 586 // SetMotor(2, m2_MotorValue);
sjoerdbarts 8:676646c50aeb 587 // Set motorvalues for scope
sjoerdbarts 8:676646c50aeb 588 // Send data to HIDScope
sjoerdbarts 8:676646c50aeb 589 scope.send();
sjoerdbarts 8:676646c50aeb 590 }
sjoerdbarts 8:676646c50aeb 591
sjoerdbarts 8:676646c50aeb 592 void PotControl()
sjoerdbarts 8:676646c50aeb 593 {
sjoerdbarts 8:676646c50aeb 594 double Motor1_Value = (pot1.read() - 0.5f)/2.0f;
sjoerdbarts 8:676646c50aeb 595 double Motor2_Value = (pot2.read() - 0.5f)/2.0f;
sjoerdbarts 8:676646c50aeb 596 //pc.printf("\r\n Motor value 1: %f \r\n",Motor1_Value);
sjoerdbarts 8:676646c50aeb 597 //pc.printf("\r\n Motor value 2: %f \r\n",Motor2_Value);
sjoerdbarts 8:676646c50aeb 598 double m1_Position = m1_GetPosition();
sjoerdbarts 8:676646c50aeb 599 double m2_Position = m2_GetPosition();
sjoerdbarts 8:676646c50aeb 600 scope.set(0, Motor1_Value);
sjoerdbarts 8:676646c50aeb 601 scope.set(1, m1_Position);
sjoerdbarts 8:676646c50aeb 602 scope.set(2, Motor2_Value);
sjoerdbarts 8:676646c50aeb 603 scope.set(3, m2_Position);
sjoerdbarts 8:676646c50aeb 604 scope.send();
sjoerdbarts 8:676646c50aeb 605 // Write the motors
sjoerdbarts 8:676646c50aeb 606 SetMotor(1, Motor1_Value);
sjoerdbarts 8:676646c50aeb 607 SetMotor(2, Motor2_Value);
sjoerdbarts 8:676646c50aeb 608 }
sjoerdbarts 8:676646c50aeb 609
sjoerdbarts 8:676646c50aeb 610 void ResetEncoders()
sjoerdbarts 8:676646c50aeb 611 {
sjoerdbarts 8:676646c50aeb 612 m1_EncoderCW.reset();
sjoerdbarts 8:676646c50aeb 613 m1_EncoderCCW.reset();
sjoerdbarts 8:676646c50aeb 614 m2_EncoderCW.reset();
sjoerdbarts 8:676646c50aeb 615 m2_EncoderCCW.reset();
sjoerdbarts 8:676646c50aeb 616 }
sjoerdbarts 8:676646c50aeb 617
sjoerdbarts 8:676646c50aeb 618 void Button1Switch()
sjoerdbarts 8:676646c50aeb 619 {
sjoerdbarts 8:676646c50aeb 620 button1_value = not button1_value;
sjoerdbarts 8:676646c50aeb 621 }
sjoerdbarts 8:676646c50aeb 622
sjoerdbarts 8:676646c50aeb 623 void Button2Switch()
sjoerdbarts 8:676646c50aeb 624 {
sjoerdbarts 8:676646c50aeb 625 button2_value = not button2_value;
s1600907 1:e1267e72ade8 626 }
s1600907 0:c96cf9760185 627
s1600907 0:c96cf9760185 628 ////////////////////////// main loop ////////////////////////////////////////////
s1600907 0:c96cf9760185 629 int main()
sjoerdbarts 8:676646c50aeb 630 {
sjoerdbarts 8:676646c50aeb 631 // Setup
sjoerdbarts 8:676646c50aeb 632 // Set baud connection with PC
s1600907 0:c96cf9760185 633 pc.baud(SERIAL_BAUD);
sjoerdbarts 8:676646c50aeb 634 pc.printf("\r\n ***THERMONUCLEAR WARFARE COMMENCES*** \r\n");
s1600907 0:c96cf9760185 635
s1600907 0:c96cf9760185 636 /* making the biquad chain for filtering the emg
s1600907 0:c96cf9760185 637 notch and high pass */
s1600907 0:c96cf9760185 638 bqc.add( &bq1 ).add( &bq2 );
s1600907 0:c96cf9760185 639 bqc2.add( &bq4 ).add( &bq5 );
s1600907 0:c96cf9760185 640 bqc3.add( &bq7 ).add( &bq8 );
s1600907 0:c96cf9760185 641
sjoerdbarts 8:676646c50aeb 642 // Setup Tickers
sjoerdbarts 8:676646c50aeb 643 Ticker CalculationsTicker;
sjoerdbarts 8:676646c50aeb 644 Ticker BlinkLedTicker;
sjoerdbarts 8:676646c50aeb 645 Ticker PIDControlTicker;
sjoerdbarts 8:676646c50aeb 646 Ticker PotControlTicker;
sjoerdbarts 7:bafc32b576c4 647
sjoerdbarts 8:676646c50aeb 648 // Setup Blinking LED to blink every 0.5 sec
sjoerdbarts 8:676646c50aeb 649 led = 1;
sjoerdbarts 8:676646c50aeb 650 BlinkLedTicker.attach(BlinkLed,0.5);
sjoerdbarts 8:676646c50aeb 651
sjoerdbarts 8:676646c50aeb 652 // Setup motor PWM speeds
sjoerdbarts 8:676646c50aeb 653 motor1_pwm.period(1.0/1000);
sjoerdbarts 8:676646c50aeb 654 motor2_pwm.period(1.0/1000);
sjoerdbarts 7:bafc32b576c4 655
sjoerdbarts 8:676646c50aeb 656 // Setup Interruptin.fall
sjoerdbarts 8:676646c50aeb 657 button1.fall(Button1Switch);
sjoerdbarts 12:9b0451105991 658 //button2.fall(Panic);
sjoerdbarts 8:676646c50aeb 659 //button3.fall(xxx);
sjoerdbarts 8:676646c50aeb 660 //button4.fall(xxx);
s1600907 0:c96cf9760185 661
sjoerdbarts 8:676646c50aeb 662 // Actual code starts here
sjoerdbarts 8:676646c50aeb 663 // Start positioning the arms
sjoerdbarts 8:676646c50aeb 664 pc.printf("\r\n ***************** \r\n");
sjoerdbarts 8:676646c50aeb 665 pc.printf("\r\n Press Button 1 to start positioning the arms using PotControl\r\n");
sjoerdbarts 8:676646c50aeb 666 pc.printf("\r\n Make sure both potentiometers are positioned halfway! \r\n");
sjoerdbarts 8:676646c50aeb 667 pc.printf("\r\n ***************** \r\n");
sjoerdbarts 8:676646c50aeb 668 while (button1_value == false){}
sjoerdbarts 8:676646c50aeb 669 PotControlTicker.attach(&PotControl,CONTROLLER_TS);
s1600907 0:c96cf9760185 670
sjoerdbarts 8:676646c50aeb 671 pc.printf("\r\n ***************** \r\n");
sjoerdbarts 8:676646c50aeb 672 pc.printf("\r\n When done positioning, press Button 1 to detach Potcontrol");
sjoerdbarts 8:676646c50aeb 673 pc.printf("\r\n ***************** \r\n");
sjoerdbarts 8:676646c50aeb 674 while (button1_value == true){}
sjoerdbarts 8:676646c50aeb 675
sjoerdbarts 8:676646c50aeb 676 // Detach potcontrol
sjoerdbarts 8:676646c50aeb 677 PotControlTicker.detach();
sjoerdbarts 8:676646c50aeb 678
sjoerdbarts 8:676646c50aeb 679 // Set motors to 0
sjoerdbarts 8:676646c50aeb 680 SetMotor(1,0.0);
sjoerdbarts 8:676646c50aeb 681 SetMotor(2,0.0);
sjoerdbarts 8:676646c50aeb 682 // Wait a bit to let the movement stop
sjoerdbarts 8:676646c50aeb 683 wait(0.5);
sjoerdbarts 8:676646c50aeb 684 // Reset the encoders to set the 0 position
sjoerdbarts 8:676646c50aeb 685 ResetEncoders();
sjoerdbarts 8:676646c50aeb 686
sjoerdbarts 8:676646c50aeb 687 // PID control starts
sjoerdbarts 8:676646c50aeb 688 pc.printf("\r\n ***************** \r\n");
sjoerdbarts 8:676646c50aeb 689 pc.printf("\r\n When done positioning, press button SW3 to start potmeter PID control");
sjoerdbarts 8:676646c50aeb 690 pc.printf("\r\n Make sure both potentiometers are positioned halfway!!! \r\n");
sjoerdbarts 8:676646c50aeb 691 pc.printf("\r\n ***************** \r\n");
sjoerdbarts 8:676646c50aeb 692 while (button1_value == false){}
sjoerdbarts 8:676646c50aeb 693
sjoerdbarts 8:676646c50aeb 694 // Attach sample, calc and control tickers
sjoerdbarts 8:676646c50aeb 695 CalculationsTicker.attach(&CalculationsForTheta,CALC_TS);
sjoerdbarts 12:9b0451105991 696 PIDControlTicker.attach(&Controller_motor, CONTROLLER_TS);
sjoerdbarts 8:676646c50aeb 697 while(true){}
s1600907 0:c96cf9760185 698 }