pid gecomment
Dependencies: HIDScope MODSERIAL QEI biquadFilter mbed
Fork of a_pid_kal_end_def by
main.cpp@17:4548efffe193, 2016-10-24 (annotated)
- Committer:
- daniQQue
- Date:
- Mon Oct 24 11:45:07 2016 +0000
- Revision:
- 17:4548efffe193
- Parent:
- 16:fd4521a4f0b3
- Child:
- 18:d7695ac04de3
sum bic tric, te compilen
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
daniQQue | 0:34c739fcc3e0 | 1 | //libraries |
daniQQue | 0:34c739fcc3e0 | 2 | #include "mbed.h" |
daniQQue | 0:34c739fcc3e0 | 3 | #include "HIDScope.h" |
daniQQue | 14:5b17697cf775 | 4 | #include "BiQuad.h" |
daniQQue | 0:34c739fcc3e0 | 5 | |
daniQQue | 0:34c739fcc3e0 | 6 | //Define objects |
daniQQue | 16:fd4521a4f0b3 | 7 | AnalogIn emg_biceps_right_in( A0 ); //analog in to get EMG from right biceps in to c++ |
daniQQue | 17:4548efffe193 | 8 | AnalogIn emg_triceps_right_in (A1); //analog in to get EMG from left biceps in to c++ |
daniQQue | 16:fd4521a4f0b3 | 9 | Ticker sample_timer; //ticker |
daniQQue | 14:5b17697cf775 | 10 | HIDScope scope( 3); //open 3 channels in hidscope |
daniQQue | 8:cd0cb71b69f2 | 11 | DigitalOut richting_motor1(D4); //motor1 direction output |
daniQQue | 8:cd0cb71b69f2 | 12 | PwmOut pwm_motor1(D5); //motor1 velocity output |
daniQQue | 0:34c739fcc3e0 | 13 | DigitalOut led(LED_GREEN); |
daniQQue | 0:34c739fcc3e0 | 14 | |
daniQQue | 0:34c739fcc3e0 | 15 | //define variables |
daniQQue | 7:42d0e38196f1 | 16 | int onoffsignal=0; |
daniQQue | 15:bb4a6c7836d8 | 17 | double cut_off_value=0.08; //gespecifeerd door floortje |
daniQQue | 16:fd4521a4f0b3 | 18 | double signalsum_right; |
daniQQue | 16:fd4521a4f0b3 | 19 | double signalsum_right_filter_high; |
daniQQue | 16:fd4521a4f0b3 | 20 | double signalsum_right_filter_high_abs; |
daniQQue | 16:fd4521a4f0b3 | 21 | double signalsum_right_filtered; |
daniQQue | 16:fd4521a4f0b3 | 22 | double biceps_right; |
daniQQue | 16:fd4521a4f0b3 | 23 | double triceps_right; |
daniQQue | 15:bb4a6c7836d8 | 24 | BiQuad filterhigh(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01); |
daniQQue | 15:bb4a6c7836d8 | 25 | BiQuad filterlow (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01); |
daniQQue | 10:7255b59224cc | 26 | |
daniQQue | 15:bb4a6c7836d8 | 27 | //functions which are called in ticker |
daniQQue | 0:34c739fcc3e0 | 28 | void filter(){ |
daniQQue | 16:fd4521a4f0b3 | 29 | |
daniQQue | 17:4548efffe193 | 30 | biceps_right = emg_biceps_right_in.read(); //inladen data |
daniQQue | 17:4548efffe193 | 31 | triceps_right = emg_triceps_right_in.read(); //inladen data |
daniQQue | 16:fd4521a4f0b3 | 32 | signalsum_right = biceps_right-triceps_right; // inladen data |
daniQQue | 16:fd4521a4f0b3 | 33 | |
daniQQue | 16:fd4521a4f0b3 | 34 | signalsum_right_filter_high = filterhigh.step(signalsum_right); |
daniQQue | 16:fd4521a4f0b3 | 35 | signalsum_right_filter_high_abs = fabs(signalsum_right_filter_high); |
daniQQue | 16:fd4521a4f0b3 | 36 | signalsum_right_filtered = filterlow.step(signalsum_right_filtered); |
daniQQue | 7:42d0e38196f1 | 37 | |
daniQQue | 16:fd4521a4f0b3 | 38 | if (signalsum_right_filtered>cut_off_value) |
daniQQue | 7:42d0e38196f1 | 39 | {onoffsignal=1;} |
daniQQue | 7:42d0e38196f1 | 40 | |
daniQQue | 7:42d0e38196f1 | 41 | else |
daniQQue | 7:42d0e38196f1 | 42 | {onoffsignal=0;} |
daniQQue | 7:42d0e38196f1 | 43 | |
daniQQue | 0:34c739fcc3e0 | 44 | //send signals to scope |
daniQQue | 16:fd4521a4f0b3 | 45 | scope.set(0, biceps_right ); //set emg signal to scope in channel 1 |
daniQQue | 16:fd4521a4f0b3 | 46 | scope.set(1, triceps_right); |
daniQQue | 16:fd4521a4f0b3 | 47 | scope.set(2, signalsum_right_filtered); |
daniQQue | 0:34c739fcc3e0 | 48 | scope.send(); //send all the signals to the scope |
daniQQue | 0:34c739fcc3e0 | 49 | } |
daniQQue | 0:34c739fcc3e0 | 50 | |
daniQQue | 0:34c739fcc3e0 | 51 | //program |
daniQQue | 0:34c739fcc3e0 | 52 | |
daniQQue | 0:34c739fcc3e0 | 53 | int main() |
daniQQue | 0:34c739fcc3e0 | 54 | { |
daniQQue | 0:34c739fcc3e0 | 55 | sample_timer.attach(&filter, 0.001); //continously execute the EMG reader and filter, it ensures that filter and sampling is executed every 1/frequency seconds |
daniQQue | 4:7d9ca9c1dcce | 56 | |
daniQQue | 0:34c739fcc3e0 | 57 | //endless loop |
daniQQue | 0:34c739fcc3e0 | 58 | |
daniQQue | 0:34c739fcc3e0 | 59 | while(1) |
daniQQue | 8:cd0cb71b69f2 | 60 | { |
daniQQue | 8:cd0cb71b69f2 | 61 | if (onoffsignal==1) |
daniQQue | 8:cd0cb71b69f2 | 62 | { |
daniQQue | 8:cd0cb71b69f2 | 63 | richting_motor1 = 0; //motordirection (ccw) |
daniQQue | 10:7255b59224cc | 64 | pwm_motor1 = 1; //motorspeed 1 |
daniQQue | 8:cd0cb71b69f2 | 65 | |
daniQQue | 8:cd0cb71b69f2 | 66 | } |
daniQQue | 8:cd0cb71b69f2 | 67 | else if(onoffsignal==0) |
daniQQue | 8:cd0cb71b69f2 | 68 | { |
daniQQue | 8:cd0cb71b69f2 | 69 | richting_motor1 = 0; //motordirection (ccw) |
daniQQue | 8:cd0cb71b69f2 | 70 | pwm_motor1 = 0; //motorspeed 0 |
daniQQue | 8:cd0cb71b69f2 | 71 | |
daniQQue | 8:cd0cb71b69f2 | 72 | } |
daniQQue | 8:cd0cb71b69f2 | 73 | |
daniQQue | 8:cd0cb71b69f2 | 74 | } |
daniQQue | 0:34c739fcc3e0 | 75 | |
daniQQue | 0:34c739fcc3e0 | 76 | } |