Exercise of motor control

Dependencies:   Encoder MODSERIAL mbed

Committer:
Technical_Muffin
Date:
Wed Sep 23 09:42:07 2015 +0000
Revision:
7:2142e17a49e8
Parent:
6:19b8a8d255dd
speed control exercise working

Who changed what in which revision?

UserRevisionLine numberNew 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 6:19b8a8d255dd 9
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 6:19b8a8d255dd 16 float maximum=1.0;
Technical_Muffin 6:19b8a8d255dd 17 direction1.write(false);//turn motor CCW or CW if set to 0
Technical_Muffin 1:0f99afbd578a 18 //motor CW = 0
Technical_Muffin 1:0f99afbd578a 19 //motor CCW = 1
Technical_Muffin 6:19b8a8d255dd 20 while(1){//while on, turn on motor
Technical_Muffin 6:19b8a8d255dd 21 speed1.period_us(100);//Set period of PWM to 100 us.
Technical_Muffin 7:2142e17a49e8 22 for(float cycle=0.5; cycle < maximum; cycle=cycle + 0.1){
Technical_Muffin 7:2142e17a49e8 23 speed1.write(cycle);//write the new speed to the motor
Technical_Muffin 7:2142e17a49e8 24 wait(1.0);// wait 1 second to switch to higher speed
Technical_Muffin 7:2142e17a49e8 25 } //it only turns at the last 5 speeds, so about 0.6 till 1.0
Technical_Muffin 7:2142e17a49e8 26 }// the cause of this is the friction it has to work against during rotation
Technical_Muffin 6:19b8a8d255dd 27 }
Technical_Muffin 4:646f8d3f0643 28