
Position calibration added to encoder code
Dependencies: Encoder MODSERIAL mbed
Fork of Assignment_3_3_encoder by
Diff: main.cpp
- Revision:
- 1:9330bbc04857
- Parent:
- 0:1907291f2887
--- a/main.cpp Mon Oct 01 14:27:53 2018 +0000 +++ b/main.cpp Tue Oct 16 19:42:27 2018 +0000 @@ -5,6 +5,7 @@ PwmOut pwmpin(D6); PwmOut led(D10); DigitalIn button(D2); +DigitalIn button_position_calibration (D3); //button for calibration (check which pin is free) DigitalOut directionpin(D7); Encoder motor1(D13,D12); MODSERIAL pc(USBTX, USBRX); @@ -17,19 +18,16 @@ pc.printf("** reset **\r\n"); directionpin = 1; //turn forward led = 1; //turn led on - -/* this could be placed in the while loop - //"lompe" functions to let the counter count from 0-8400 and from -8400 to 0, could possibly be done better - if (counts > max_counts) {counts = 0;} - else if (counts < 0) {counts = max_counts;} - else if (counts < -1*max_counts) {counts = 0;} - //converting the counts to degrees - degrees_per_count = 360/max_counts; - motor_angle = counts*degrees_per_counts; -*/ - - while (true) { +//______Position calibration______// +// first set the arm in the position you would like te be the neutral position, then press the button_position_calibration + while (button_position_calibration == false) { + counts = 0; + pc.printf("position is set to 0"); + wait(0.1f); + +//______Encoder for position______// + while (button_position_calibration == true) { //if the button is pressed, turn of the led and turn the motor if (button == false){ pwmpin = 0.6; @@ -43,5 +41,18 @@ counts = motor1.getPosition(); //get the number of counts from the encoder. Will keep counting up/down if not reset or stopped pc.printf("%i\r\n", counts); wait(0.1f); - } + } + +/* this could be placed in the while loop + //"lompe" functions to let the counter count from 0-8400 and from -8400 to 0, could possibly be done better + if (counts > max_counts) {counts = 0;} + else if (counts < 0) {counts = max_counts;} + else if (counts < -1*max_counts) {counts = 0;} + + //converting the counts to degrees + degrees_per_count = 360/max_counts; + motor_angle = counts*degrees_per_counts; +*/ +} + } \ No newline at end of file