Exercise of motor control
Dependencies: Encoder MODSERIAL mbed
main.cpp@5:6fda4448a428, 2015-09-23 (annotated)
- Committer:
- Technical_Muffin
- Date:
- Wed Sep 23 08:31:23 2015 +0000
- Revision:
- 5:6fda4448a428
- Parent:
- 4:646f8d3f0643
- Child:
- 6:19b8a8d255dd
Exercise of changing motor direction
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Technical_Muffin | 0:438a9d780499 | 1 | #include "mbed.h" |
Technical_Muffin | 0:438a9d780499 | 2 | #include "encoder.h" |
Technical_Muffin | 0:438a9d780499 | 3 | #include "MODSERIAL.h" |
Technical_Muffin | 0:438a9d780499 | 4 | |
Technical_Muffin | 0:438a9d780499 | 5 | DigitalOut led(LED_RED); |
Technical_Muffin | 4:646f8d3f0643 | 6 | DigitalOut direction1(D7); |
Technical_Muffin | 4:646f8d3f0643 | 7 | PwmOut speed1(D6); |
Technical_Muffin | 0:438a9d780499 | 8 | DigitalIn button(PTC6); |
Technical_Muffin | 5:6fda4448a428 | 9 | bool motorstart=false;//boolean to switch motor between CW an CCW |
Technical_Muffin | 3:8583035f898b | 10 | Encoder motor1(D13,D12,true);// call the encoder |
Technical_Muffin | 0:438a9d780499 | 11 | MODSERIAL pc(USBTX,USBRX); |
Technical_Muffin | 0:438a9d780499 | 12 | |
Technical_Muffin | 0:438a9d780499 | 13 | int main() |
Technical_Muffin | 0:438a9d780499 | 14 | { |
Technical_Muffin | 0:438a9d780499 | 15 | pc.baud(115200); |
Technical_Muffin | 0:438a9d780499 | 16 | |
Technical_Muffin | 1:0f99afbd578a | 17 | //motor CW = 0 |
Technical_Muffin | 1:0f99afbd578a | 18 | //motor CCW = 1 |
Technical_Muffin | 4:646f8d3f0643 | 19 | while(1){//as long as button is pressed turn on motor |
Technical_Muffin | 5:6fda4448a428 | 20 | direction1.write(motorstart);//turn motor CCW or CW if set to 0 |
Technical_Muffin | 5:6fda4448a428 | 21 | motorstart=!motorstart;// change direction |
Technical_Muffin | 5:6fda4448a428 | 22 | speed1.write(0.1);//Set speed of motor to 10%. |
Technical_Muffin | 5:6fda4448a428 | 23 | wait(1); |
Technical_Muffin | 5:6fda4448a428 | 24 | //pc.printf("pos: %d, speed %f \r\n",motor1.getPosition(), motor1.getSpeed()); |
Technical_Muffin | 1:0f99afbd578a | 25 | // X unit counts equals one magnet shaft rotation, with X being 64 |
Technical_Muffin | 4:646f8d3f0643 | 26 | |
Technical_Muffin | 0:438a9d780499 | 27 | } |
Technical_Muffin | 4:646f8d3f0643 | 28 | |
Technical_Muffin | 1:0f99afbd578a | 29 | } |