press buttons on biorobotics shield to rotate motor in certain direction
Dependencies: Encoder HIDScope MODSERIAL mbed
main.cpp@2:1656c259189f, 2015-09-23 (annotated)
- Committer:
- Vigilance88
- Date:
- Wed Sep 23 09:16:11 2015 +0000
- Revision:
- 2:1656c259189f
- Parent:
- 1:e0c4625bbbab
using buttons on shield to run in a certain direction
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vigilance88 | 0:4bfe85fb30ab | 1 | #include "mbed.h" |
Vigilance88 | 0:4bfe85fb30ab | 2 | #include "encoder.h" |
Vigilance88 | 2:1656c259189f | 3 | #include "MODSERIAL.h" |
Vigilance88 | 0:4bfe85fb30ab | 4 | |
Vigilance88 | 2:1656c259189f | 5 | volatile bool looptimerflag; |
Vigilance88 | 2:1656c259189f | 6 | |
Vigilance88 | 2:1656c259189f | 7 | void setlooptimerflag(void) |
Vigilance88 | 0:4bfe85fb30ab | 8 | { |
Vigilance88 | 2:1656c259189f | 9 | looptimerflag = true; |
Vigilance88 | 2:1656c259189f | 10 | } |
Vigilance88 | 2:1656c259189f | 11 | |
Vigilance88 | 2:1656c259189f | 12 | int main(){ |
Vigilance88 | 2:1656c259189f | 13 | //VARIABLES |
Vigilance88 | 2:1656c259189f | 14 | AnalogIn potmeter(A1); |
Vigilance88 | 2:1656c259189f | 15 | AnalogIn potmeter2(A0); |
Vigilance88 | 2:1656c259189f | 16 | MODSERIAL pc(USBTX,USBRX); |
Vigilance88 | 2:1656c259189f | 17 | DigitalIn button(PTA1); |
Vigilance88 | 2:1656c259189f | 18 | DigitalIn button1(PTB9); |
Vigilance88 | 2:1656c259189f | 19 | |
Vigilance88 | 2:1656c259189f | 20 | Encoder motor1(D13,D12,true); // channel A and B from encoder, true - getSpeed works |
Vigilance88 | 2:1656c259189f | 21 | PwmOut pwm_motor1(D6); // D4 and D5 = motor 2, D7 and D6 = motor 2 |
Vigilance88 | 2:1656c259189f | 22 | DigitalOut dir_motor1(D7); // |
Vigilance88 | 2:1656c259189f | 23 | |
Vigilance88 | 2:1656c259189f | 24 | Encoder motor2(D10,D9,true); // channel A and B from encoder, true - getSpeed works |
Vigilance88 | 2:1656c259189f | 25 | PwmOut pwm_motor2(D5); // D4 and D5 = motor 2, D7 and D6 = motor 2 |
Vigilance88 | 2:1656c259189f | 26 | DigitalOut dir_motor2(D4); // |
Vigilance88 | 2:1656c259189f | 27 | |
Vigilance88 | 2:1656c259189f | 28 | // MOTOR1 |
Vigilance88 | 2:1656c259189f | 29 | float goal; |
Vigilance88 | 2:1656c259189f | 30 | float pwm_to_motor; |
Vigilance88 | 2:1656c259189f | 31 | // MOTOR2 |
Vigilance88 | 2:1656c259189f | 32 | float goal2; |
Vigilance88 | 2:1656c259189f | 33 | float pwm_to_motor2; |
Vigilance88 | 2:1656c259189f | 34 | |
Vigilance88 | 2:1656c259189f | 35 | //CODE |
Vigilance88 | 0:4bfe85fb30ab | 36 | pc.baud(9600); |
Vigilance88 | 0:4bfe85fb30ab | 37 | |
Vigilance88 | 2:1656c259189f | 38 | //pwm_motor1.write(0.2f); // Speed |
Vigilance88 | 2:1656c259189f | 39 | //dir_motor1.write(1); // Direction |
Vigilance88 | 2:1656c259189f | 40 | |
Vigilance88 | 2:1656c259189f | 41 | Ticker looptimer; |
Vigilance88 | 2:1656c259189f | 42 | looptimer.attach(setlooptimerflag,0.01); |
Vigilance88 | 2:1656c259189f | 43 | |
Vigilance88 | 2:1656c259189f | 44 | while (1) { |
Vigilance88 | 2:1656c259189f | 45 | while(looptimerflag != true); |
Vigilance88 | 2:1656c259189f | 46 | looptimerflag = false; |
Vigilance88 | 0:4bfe85fb30ab | 47 | |
Vigilance88 | 2:1656c259189f | 48 | //MOTOR1 |
Vigilance88 | 2:1656c259189f | 49 | if (button.read() < 0.5) { //button pressed - direction |
Vigilance88 | 2:1656c259189f | 50 | dir_motor2.write(1); |
Vigilance88 | 2:1656c259189f | 51 | pwm_motor2.write(0.5); |
Vigilance88 | 2:1656c259189f | 52 | pc.printf("position = %d \r\n", motor2.getPosition()); |
Vigilance88 | 2:1656c259189f | 53 | } |
Vigilance88 | 2:1656c259189f | 54 | else if(button1.read() < 0.5) { //button pressed - other direction |
Vigilance88 | 2:1656c259189f | 55 | dir_motor2.write(0); |
Vigilance88 | 2:1656c259189f | 56 | pwm_motor2.write(0.5); |
Vigilance88 | 2:1656c259189f | 57 | pc.printf("position = %d \r\n", motor2.getPosition()); |
Vigilance88 | 2:1656c259189f | 58 | } |
Vigilance88 | 2:1656c259189f | 59 | else { //button not pressed |
Vigilance88 | 2:1656c259189f | 60 | dir_motor2.write(0); |
Vigilance88 | 2:1656c259189f | 61 | pwm_motor2.write(0); |
Vigilance88 | 2:1656c259189f | 62 | pc.printf("position = %d \r\n", motor2.getPosition()); |
Vigilance88 | 2:1656c259189f | 63 | } |
Vigilance88 | 2:1656c259189f | 64 | |
Vigilance88 | 2:1656c259189f | 65 | // pc.printf("pos: %d, speed %f \r\n",motor1.getPosition(), motor1.getSpeed()); |
Vigilance88 | 0:4bfe85fb30ab | 66 | } |
Vigilance88 | 0:4bfe85fb30ab | 67 | } |