K K / Mbed 2 deprecated Movement_code

Dependencies:   MODSERIAL QEI mbed

Committer:
Technical_Muffin
Date:
Thu Oct 15 21:20:23 2015 +0000
Revision:
11:be7660614c5c
Parent:
10:2dd707673b3b
Child:
12:2d3d7a9ca496
untested motor revision

Who changed what in which revision?

UserRevisionLine numberNew 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 8:e0fc6dd187a2 6 QEI motor1(D13,D12,NC, 624);//encoder for motor 1
Technical_Muffin 8:e0fc6dd187a2 7 QEI motor2(D11,D10,NC, 624);//encoder for motor 2
Technical_Muffin 0:e2de1fe5b34c 8 MODSERIAL pc(USBTX,USBRX);
Technical_Muffin 8:e0fc6dd187a2 9 DigitalOut direction1(D7);//direction input for motor 1
Technical_Muffin 8:e0fc6dd187a2 10 DigitalOut direction2(D4);//direction input for motor 2
Technical_Muffin 8:e0fc6dd187a2 11 PwmOut speed1(D6);//speed input for motor 1
Technical_Muffin 8:e0fc6dd187a2 12 PwmOut speed2(D5);//speed input for motor 2
Technical_Muffin 8:e0fc6dd187a2 13 DigitalIn button1(PTC6);//test button for starting motor 1
Technical_Muffin 8:e0fc6dd187a2 14 DigitalIn button2(PTA4);//test button for starting motor 2
Technical_Muffin 2:27fe9488ba61 15 DigitalOut led1(LED_RED);
Technical_Muffin 2:27fe9488ba61 16 DigitalOut led2(LED_BLUE);
Technical_Muffin 2:27fe9488ba61 17 DigitalOut led3(LED_GREEN);
Technical_Muffin 0:e2de1fe5b34c 18
Technical_Muffin 0:e2de1fe5b34c 19 int main()
Technical_Muffin 3:b913f8ea69e8 20 {
Technical_Muffin 11:be7660614c5c 21 float cycle = 0.7;//define the speed of the motor
Technical_Muffin 8:e0fc6dd187a2 22 bool motor1_on = 1;//set the on variable of motor 1
Technical_Muffin 8:e0fc6dd187a2 23 int motor1_dir=0;//set the direction of motor 1
Technical_Muffin 8:e0fc6dd187a2 24 bool motor2_on =1;//set the on variable of motor 2
Technical_Muffin 8:e0fc6dd187a2 25 int motor2_dir = 0;//set the direction of motor 1
Technical_Muffin 11:be7660614c5c 26 int n1=1;
Technical_Muffin 11:be7660614c5c 27 int n2=1;
Technical_Muffin 0:e2de1fe5b34c 28
Technical_Muffin 3:b913f8ea69e8 29 while(1){
Technical_Muffin 3:b913f8ea69e8 30 led3.write(0);
Technical_Muffin 3:b913f8ea69e8 31 led1.write(1);
Technical_Muffin 3:b913f8ea69e8 32 led2.write(1);
Technical_Muffin 8:e0fc6dd187a2 33 speed1.write(0);//set motor 1 speed to 0
Technical_Muffin 8:e0fc6dd187a2 34 speed2.write(0);//set motor 2 speed to 0
Technical_Muffin 8:e0fc6dd187a2 35 pc.baud(115200);
Technical_Muffin 8:e0fc6dd187a2 36
Technical_Muffin 8:e0fc6dd187a2 37 int diffa1 = button1.read();//read out the button 1 signal and calculate if it is being pressed or released
Technical_Muffin 8:e0fc6dd187a2 38 wait(0.2);//from this we can determine if the rotation direction needs to be reversed.
Technical_Muffin 8:e0fc6dd187a2 39 int diffb1 = button1.read();
Technical_Muffin 8:e0fc6dd187a2 40 int button_toggle1 = diffa1-diffb1;
Technical_Muffin 8:e0fc6dd187a2 41 if(button_toggle1 == 1 && motor1_dir == 1){
Technical_Muffin 7:46d9f01afba2 42 motor1_dir = 0;
Technical_Muffin 7:46d9f01afba2 43 }
Technical_Muffin 8:e0fc6dd187a2 44 else if(button_toggle1 == 1 && motor1_dir == 0){
Technical_Muffin 7:46d9f01afba2 45 motor1_dir = 1;
Technical_Muffin 7:46d9f01afba2 46 }
Technical_Muffin 11:be7660614c5c 47
Technical_Muffin 11:be7660614c5c 48 if(button1.read()==false){
Technical_Muffin 11:be7660614c5c 49 while(n1 == 1){
Technical_Muffin 11:be7660614c5c 50 speed1.write(cycle);
Technical_Muffin 11:be7660614c5c 51 direction1.write(1);//turn motor CCW or CW
Technical_Muffin 11:be7660614c5c 52 led3.write(1);
Technical_Muffin 11:be7660614c5c 53 led1.write(0);
Technical_Muffin 11:be7660614c5c 54 n1=0;
Technical_Muffin 11:be7660614c5c 55 }
Technical_Muffin 11:be7660614c5c 56 }
Technical_Muffin 11:be7660614c5c 57 else if (button1.read() == true){
Technical_Muffin 11:be7660614c5c 58 while(n1==0){
Technical_Muffin 11:be7660614c5c 59 speed1.write(0);
Technical_Muffin 11:be7660614c5c 60 n1=1;
Technical_Muffin 11:be7660614c5c 61 }
Technical_Muffin 11:be7660614c5c 62 }
Technical_Muffin 8:e0fc6dd187a2 63
Technical_Muffin 8:e0fc6dd187a2 64 int diffa2 = button2.read();//read out the button 2 signal and calculate if it is being pressed or released
Technical_Muffin 8:e0fc6dd187a2 65 wait(0.2);//from this we can determine if the rotation direction needs to be reversed.
Technical_Muffin 8:e0fc6dd187a2 66 int diffb2 = button2.read();
Technical_Muffin 8:e0fc6dd187a2 67 int button_toggle2 = diffa2-diffb2;
Technical_Muffin 8:e0fc6dd187a2 68 if(button_toggle2 == 1 && motor2_dir == 1){
Technical_Muffin 8:e0fc6dd187a2 69 motor2_dir = 0;
Technical_Muffin 8:e0fc6dd187a2 70 }
Technical_Muffin 8:e0fc6dd187a2 71 else if(button_toggle2 == 1 && motor2_dir == 0){
Technical_Muffin 8:e0fc6dd187a2 72 motor2_dir = 1;
Technical_Muffin 8:e0fc6dd187a2 73 }
Technical_Muffin 11:be7660614c5c 74
Technical_Muffin 8:e0fc6dd187a2 75 //motor CW = 0
Technical_Muffin 8:e0fc6dd187a2 76 //motor CCW = 1
Technical_Muffin 11:be7660614c5c 77 if(button1.read()==false){
Technical_Muffin 11:be7660614c5c 78 while(n2 == 1){
Technical_Muffin 11:be7660614c5c 79 speed2.write(cycle);
Technical_Muffin 11:be7660614c5c 80 direction2.write(1);//turn motor CCW or CW
Technical_Muffin 11:be7660614c5c 81 led3.write(1);
Technical_Muffin 11:be7660614c5c 82 led2.write(0);
Technical_Muffin 11:be7660614c5c 83 n2=0;
Technical_Muffin 11:be7660614c5c 84 }
Technical_Muffin 11:be7660614c5c 85 }
Technical_Muffin 11:be7660614c5c 86 else if (button1.read() == true){
Technical_Muffin 11:be7660614c5c 87 while(n2==0){
Technical_Muffin 11:be7660614c5c 88 speed1.write(0);
Technical_Muffin 11:be7660614c5c 89 n2=1;
Technical_Muffin 11:be7660614c5c 90 }
Technical_Muffin 11:be7660614c5c 91 }
Technical_Muffin 3:b913f8ea69e8 92 }
Technical_Muffin 8:e0fc6dd187a2 93 }