control for robotic arm that can play chess using a granular gripper
Dependencies: Encoder mbed HIDScope Servo MODSERIAL
Fork of chessRobot by
Revision 92:12e2e57e900a, committed 2015-10-21
- Comitter:
- bjornnijhuis
- Date:
- Wed Oct 21 10:28:42 2015 +0000
- Parent:
- 90:1d0c96a5bc5f
- Child:
- 94:28e274481b60
- Commit message:
- Set EMG configuration to run before calibration of motors
Changed in this revision
--- a/emg.cpp Wed Oct 21 11:13:10 2015 +0200 +++ b/emg.cpp Wed Oct 21 10:28:42 2015 +0000 @@ -14,13 +14,7 @@ // Define program constants const int on = 0; // On-constant for LEDs for program readability const int off = 1; // Off-constant for LEDs for program readability -const int sample = 0; // Constant for mode switching for program readability -const int normalize = 1; // Constant for mode switching for program readability - bool ledsEnable = false; -//********************************************************************************************** -bool mode = normalize; // Set program mode -//********************************************************************************************** // Initialize sampling constants double emg_val1 = 0, emg_val2 = 0, emg_filt_val1 = 0, emg_filt_val2 = 0; @@ -214,13 +208,14 @@ } -void readEMG(){ - if(mode==normalize && normalizing_timer.read_ms() == 0) { // Start normalizing timer +void readEMG() +{ + if(emg_mode==normalize && normalizing_timer.read_ms() == 0) { // Start normalizing timer normalizing_timer.reset(); normalizing_timer.start(); } - if(mode ==normalize) { + if(emg_mode ==normalize) { emg_val1 = emg1.read(); // Sample EMG value 1 from AnalogIn emg_val2 = emg2.read(); // Sample EMG value 2 from AnalogIn @@ -239,7 +234,7 @@ // First normalizing step: channel 1 if (normalizing_timer.read_ms() <= normalize_time && channel == 1) { - if (ledsEnable){ + if (ledsEnable) { redLed.write(off); greenLed.write(on); } @@ -249,13 +244,13 @@ // Second normalizing step: wait time, switch channel } else if (normalizing_timer.read_ms() > normalize_time && channel == 1) { channel = 2; - if (ledsEnable){ + if (ledsEnable) { greenLed.write(off); redLed.write(on); } // Third normalizing step: channel 2 } else if (normalizing_timer.read_ms() >= (normalize_time + normalize_wait) && normalizing_timer.read_ms() <= (2*normalize_time + normalize_wait) && channel == 2) { - if (ledsEnable){ + if (ledsEnable) { redLed.write(off); greenLed.write(on); } @@ -266,17 +261,17 @@ } else if (normalizing_timer.read_ms() > (2*normalize_time + normalize_wait)) { normalizing_timer.stop(); normalizing_timer.reset(); - if (ledsEnable){ + if (ledsEnable) { greenLed.write(off); redLed.write(off); blueLed.write(on); } - mode = sample; + emg_mode = sample; } } - if(mode ==sample) { + if(emg_mode ==sample) { emg_val1 = emg1.read(); // Sample EMG value 1 from AnalogIn emg_val2 = emg2.read(); // Sample EMG value 2 from AnalogIn @@ -303,7 +298,7 @@ EMG_check(thr_pass1, thr_pass2, x_velocity, y_velocity, z_velocity, emg_filt_val1, emg_filt_val2, EMG_timer.read_ms()); } else { - if (ledsEnable){ + if (ledsEnable) { greenLed.write(off); redLed.write(on); blueLed.write(off); @@ -312,8 +307,5 @@ } // Graphical output to HIDScope for debugging/ program check - - - }
--- a/emg.h Wed Oct 21 11:13:10 2015 +0200 +++ b/emg.h Wed Oct 21 10:28:42 2015 +0000 @@ -10,6 +10,10 @@ extern DigitalOut greenLed; extern DigitalOut blueLed; +extern const int normalize; +extern const int sample; +extern bool emg_mode; + extern const double sample_frequency; extern const int normalize_time; @@ -44,5 +48,6 @@ extern const double a12_gain, a12_a1, a12_a2, a12_b0, a12_b1, a12_b2; extern const double a13_gain, a13_a1, a13_a2, a13_b0, a13_b1, a13_b2; + void readEMG(); #endif
--- a/main.cpp Wed Oct 21 11:13:10 2015 +0200 +++ b/main.cpp Wed Oct 21 10:28:42 2015 +0000 @@ -1,10 +1,10 @@ /* - ________ ____ __ __ + ________ ____ __ __ / ____/ /_ ___ __________ / __ \____ / /_ ____ / /_ / / / __ \/ _ \/ ___/ ___/ / /_/ / __ \/ __ \/ __ \/ __/ -/ /___/ / / / __(__ |__ ) / _, _/ /_/ / /_/ / /_/ / /_ -\____/_/ /_/\___/____/____/ /_/ |_|\____/_.___/\____/\__/ - +/ /___/ / / / __(__ |__ ) / _, _/ /_/ / /_/ / /_/ / /_ +\____/_/ /_/\___/____/____/ /_/ |_|\____/_.___/\____/\__/ + */ #include "mbed.h" @@ -14,57 +14,85 @@ #include "debug.h" #include "emg.h" +const int sample = 0; // Constant for EMG mode switching for program readability +const int normalize = 1; // Constant for EMG mode switching for program readability + bool start=false; +bool emg_mode = normalize; // Set EMG mode + Ticker switchesTick, debugTick, motorTick, EMGTick, safetyTick; volatile bool switches_go=false, debug_go=false, motor_go=false, emg_go=false, safety_go=false; -void switches_activate(){switches_go=true;}; -void debug_activate(){debug_go=true;}; -void motor_activate(){motor_go=true;}; -void emg_activate(){emg_go=true;}; -void safety_activate(){safety_go=true;}; - -double motorCall = 0.01; // set motor frequency global so it can be used for speed. -int main(){ -DigitalIn startButton(startPin); -while(start == false){ - if(startButton.read()==0){ - start=true; - } -} -else{ -motorInit(); -calibrateMotors(); // start calibration procedure +void switches_activate() +{ + switches_go=true; +}; +void debug_activate() +{ + debug_go=true; +}; +void motor_activate() +{ + motor_go=true; +}; +void emg_activate() +{ + emg_go=true; +}; +void safety_activate() +{ + safety_go=true; +}; -switchesTick.attach(&switches_activate, 0.02f); -debugTick.attach(&debug_activate, 0.03f); -motorTick.attach(&motor_activate, motorCall); -EMGTick.attach(&emg_activate, 0.005f); -safetyTick.attach(&safety_activate, 0.001f); - - - while (true) { - if(safety_go){ - safety_go=false; - safety(); - } - if(emg_go){ - emg_go=false; - readEMG(); - } - if(switches_go){ - switches_go=false; - checkSwitches(); - } - if(debug_go){ - debug_go=false; - debugProcess(); - } - if(motor_go){ - motor_go=false; - motorControl(); - // servoControl(); +double motorCall = 0.005; // set motor frequency global so it can be used for speed. +int main() +{ + DigitalIn startButton(startPin); + while(start == false) { + if(startButton.read()==0) { + start=true; } } -} + while (start == true) { + motorInit(); + + EMGTick.attach(&emg_activate, 0.005f); + switchesTick.attach(&switches_activate, 0.02f); + debugTick.attach(&debug_activate, 0.03f); + motorTick.attach(&motor_activate, motorCall); + safetyTick.attach(&safety_activate, 0.001f); + + while (emg_mode == normalize) { + if(emg_go) { + emg_go=false; + readEMG(); + } + } + + calibrateMotors(); // start calibration procedure + + while (true) { + if(safety_go) { + safety_go=false; + safety(); + } + if(emg_go) { + emg_go=false; + readEMG(); + } + if(switches_go) { + switches_go=false; + checkSwitches(); + } + if(debug_go) { + debug_go=false; + debugProcess(); + } + if(motor_go) { + motor_go=false; + motorControl(); + // servoControl(); + } + } + } } \ No newline at end of file