code for basic movement of robot
Dependencies: MODSERIAL QEI mbed
main.cpp@1:17e250b48fe0, 2015-10-07 (annotated)
- Committer:
- Technical_Muffin
- Date:
- Wed Oct 07 17:16:05 2015 +0000
- Revision:
- 1:17e250b48fe0
- Parent:
- 0:e2de1fe5b34c
Changed program, but it doesn't work anymore. Motor and led don't activate when button is pressed.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Technical_Muffin | 0:e2de1fe5b34c | 1 | #include "mbed.h" |
Technical_Muffin | 0:e2de1fe5b34c | 2 | #include "QEI.h" |
Technical_Muffin | 0:e2de1fe5b34c | 3 | #include "MODSERIAL.h" |
Technical_Muffin | 0:e2de1fe5b34c | 4 | //#include <"math.h"> |
Technical_Muffin | 0:e2de1fe5b34c | 5 | |
Technical_Muffin | 0:e2de1fe5b34c | 6 | QEI motor1(D13,D12,NC, 624); |
Technical_Muffin | 0:e2de1fe5b34c | 7 | MODSERIAL pc(USBTX,USBRX); |
Technical_Muffin | 0:e2de1fe5b34c | 8 | DigitalOut direction1(D7); |
Technical_Muffin | 0:e2de1fe5b34c | 9 | PwmOut speed1(D6); |
Technical_Muffin | 0:e2de1fe5b34c | 10 | DigitalIn button1(PTC6); |
Technical_Muffin | 1:17e250b48fe0 | 11 | DigitalOut led(LED_GREEN); |
Technical_Muffin | 1:17e250b48fe0 | 12 | DigitalOut led1(LED_RED); |
Technical_Muffin | 1:17e250b48fe0 | 13 | /* |
Technical_Muffin | 1:17e250b48fe0 | 14 | DigitalOut direction2(X1);//choose correct input |
Technical_Muffin | 1:17e250b48fe0 | 15 | PwmOut speed2(X2);//choose correct input |
Technical_Muffin | 1:17e250b48fe0 | 16 | DigitalIn button2(X3);//choose correct input |
Technical_Muffin | 1:17e250b48fe0 | 17 | */ |
Technical_Muffin | 0:e2de1fe5b34c | 18 | int main() |
Technical_Muffin | 0:e2de1fe5b34c | 19 | { |
Technical_Muffin | 0:e2de1fe5b34c | 20 | pc.baud(115200); |
Technical_Muffin | 0:e2de1fe5b34c | 21 | float cycle = 0.6;//define the speed of the motor |
Technical_Muffin | 0:e2de1fe5b34c | 22 | int motor1_on = 0; |
Technical_Muffin | 0:e2de1fe5b34c | 23 | int motor1_dir=0; |
Technical_Muffin | 1:17e250b48fe0 | 24 | int button_toggle = 0; |
Technical_Muffin | 1:17e250b48fe0 | 25 | int motor2_on = 0; |
Technical_Muffin | 1:17e250b48fe0 | 26 | int motor2_dir = 0; |
Technical_Muffin | 0:e2de1fe5b34c | 27 | |
Technical_Muffin | 1:17e250b48fe0 | 28 | while(1){ |
Technical_Muffin | 1:17e250b48fe0 | 29 | led.write(1); |
Technical_Muffin | 1:17e250b48fe0 | 30 | led1.write(1); |
Technical_Muffin | 1:17e250b48fe0 | 31 | speed1.write(0);//write the speed to the motor |
Technical_Muffin | 1:17e250b48fe0 | 32 | |
Technical_Muffin | 1:17e250b48fe0 | 33 | //int button1_on = button1.read(); |
Technical_Muffin | 1:17e250b48fe0 | 34 | |
Technical_Muffin | 1:17e250b48fe0 | 35 | while(button1.read() == motor1_on){ |
Technical_Muffin | 1:17e250b48fe0 | 36 | // turn on motor 1 when the button is being pressed |
Technical_Muffin | 1:17e250b48fe0 | 37 | led.write(0); |
Technical_Muffin | 1:17e250b48fe0 | 38 | direction1.write(motor1_dir);//turn motor CCW or CW |
Technical_Muffin | 0:e2de1fe5b34c | 39 | //motor CW = 0 |
Technical_Muffin | 0:e2de1fe5b34c | 40 | //motor CCW = 1 |
Technical_Muffin | 0:e2de1fe5b34c | 41 | speed1.write(cycle);//write the speed to the motor |
Technical_Muffin | 1:17e250b48fe0 | 42 | } |
Technical_Muffin | 1:17e250b48fe0 | 43 | while(1){ |
Technical_Muffin | 1:17e250b48fe0 | 44 | pc.printf("%d", button1.read()); |
Technical_Muffin | 1:17e250b48fe0 | 45 | wait(1); |
Technical_Muffin | 1:17e250b48fe0 | 46 | } |
Technical_Muffin | 1:17e250b48fe0 | 47 | } |
Technical_Muffin | 1:17e250b48fe0 | 48 | }/* |
Technical_Muffin | 1:17e250b48fe0 | 49 | if (button2.read()==!motor2_on){//alternate between ccw and cw when the button is no longer being pressed. |
Technical_Muffin | 1:17e250b48fe0 | 50 | motor2_dir=!motor2_dir; |
Technical_Muffin | 1:17e250b48fe0 | 51 | } |
Technical_Muffin | 1:17e250b48fe0 | 52 | while(button2.read() == motor2_on){// turn on motor 1 when the button is being pressed |
Technical_Muffin | 1:17e250b48fe0 | 53 | DigitalOut led(LED_BLUE); |
Technical_Muffin | 1:17e250b48fe0 | 54 | direction2.write(motor2_dir);//turn motor CCW or CW if set to 0 |
Technical_Muffin | 1:17e250b48fe0 | 55 | //motor CW = 0 |
Technical_Muffin | 1:17e250b48fe0 | 56 | //motor CCW = 1 |
Technical_Muffin | 1:17e250b48fe0 | 57 | speed2.period_us(100);//Set period of PWM to 100 us. |
Technical_Muffin | 1:17e250b48fe0 | 58 | speed2.write(cycle);//write the speed to the motor |
Technical_Muffin | 1:17e250b48fe0 | 59 | }*/ |
Technical_Muffin | 0:e2de1fe5b34c | 60 |