Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Encoder HIDScope MODSERIAL QEI biquadFilter mbed
Diff: main.cpp
- Revision:
- 36:da07b5c2984d
- Parent:
- 34:c672f5c0763f
- Child:
- 37:23660d12d772
- Child:
- 38:17a1622a27f7
--- a/main.cpp Tue Oct 13 21:18:01 2015 +0000 +++ b/main.cpp Tue Oct 13 21:37:23 2015 +0000 @@ -52,8 +52,10 @@ volatile bool looptimerflag; const double cw=0; // zero is clockwise (front view) const double ccw=1; // one is counterclockwise (front view) +const double off=1; //Led off +const double on=0; //Led on const int Fs = 512; // sampling frequency (512 Hz) -const double sample_time = 1/512; // sampling frequency (512 Hz) +const double sample_time = 0.001953125; // sampling frequency (512 Hz) // PID motor constants double integrate_error_turn=0; // integration error turn motor @@ -79,7 +81,6 @@ // One revolution = 360 degrees // degrees_per_encoder_tick = 360/(gear_ratio*ticks_per_magnet_rotation)=360/131*32=0.085877862594198 - // ___________________________ // // \\ @@ -99,16 +100,17 @@ // // // /////////////////////////////// // int main() { - debug_led_red=1; - debug_led_blue=1; - debug_led_green=1; - double reference_turn=0; // Set constant to store reference value of the Turn motor + debug_led_red=off; + debug_led_blue=off; + debug_led_green=off; + double position_turn; // Set constant to store current position of the Turn motor double error; double pwm_to_motor_turn; double pwm_motor_turn_P; double pwm_motor_turn_I; double pwm_motor_turn_D; + double reference_turn=0; // Set constant to store reference value of the Turn motor //START OF CODE pc.printf("Start of code \n\r"); @@ -133,14 +135,14 @@ // \\___________________________// // interrupt button Disbalances the current motor position - //if button L2 pressed => disbalance motor \\ + //if button L2 pressed => disbalance motor if (buttonL2.read() < 0.5){ motordirection_turn = cw; pwm_motor_turn = 0.5f; pc.printf("positie = %d \r\n", motor_turn.getPulses()); } - // if button L1 pressed => shut down motor for 1000 seconds \\ + // if button L1 pressed => shut down motor for 1000 seconds if (buttonL1.read() < 0.5){ motordirection_turn = cw; pwm_motor_turn = 0; @@ -153,7 +155,7 @@ // // \\ // || [Wait for go signal] || // \\___________________________// -// // Wait until looptimer flag is true then execute PID controller loop. \\ +// // Wait until looptimer flag is true then execute PID controller loop. while(looptimerflag != true); @@ -165,7 +167,7 @@ // // \\ // || [Calibrate position] || // \\___________________________// -// // Keep motor position between -4200 and 4200 counts \\ +// // Keep motor position between -4200 and 4200 counts if ((motor_turn.getPulses()>4200) || (motor_turn.getPulses()<-4200)) // If value is outside -4200 and 4200 (number of counts equal to one revolution) reset to zero { @@ -221,7 +223,7 @@ // // \\ // || [PID error -> pwm motor] || // \\___________________________// -// // Keep Pwm between -1 and 1 -> and decide the direction of the motor to compensate the error \\ +// // Keep Pwm between -1 and 1 -> and decide the direction of the motor to compensate the error keep_in_range(&pwm_to_motor_turn, -1,1); // Pass to motor controller but keep it in range! pc.printf("pwm %f \n\r", pwm_to_motor_turn); @@ -252,9 +254,9 @@ // \\___________________________// // // Check signals inside HIDSCOPE \\ - scope.set(0,reference_turn); // HIDSCOPE channel 0 : Current Reference - scope.set(0,position_turn); // HIDSCOPE channel 0 : Position_turn - scope.set(1,pwm_to_motor_turn); // HIDSCOPE channel 1 : Pwm_to_motor_turn + scope.set(0,error); // HIDSCOPE channel 0 : Current Reference + scope.set(1,position_turn); // HIDSCOPE channel 1 : Position_turn + scope.set(2,pwm_to_motor_turn); // HIDSCOPE channel 2 : Pwm_to_motor_turn scope.send(); // Send channel info to HIDSCOPE } }