K K / Mbed 2 deprecated Movement_code

Dependencies:   MODSERIAL QEI mbed

Committer:
Technical_Muffin
Date:
Fri Oct 09 10:00:30 2015 +0000
Revision:
7:46d9f01afba2
Parent:
6:206fff3b20b0
Child:
8:e0fc6dd187a2
working toggle movement function for a single motor

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 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 2:27fe9488ba61 11 DigitalOut led1(LED_RED);
Technical_Muffin 2:27fe9488ba61 12 DigitalOut led2(LED_BLUE);
Technical_Muffin 2:27fe9488ba61 13 DigitalOut led3(LED_GREEN);
Technical_Muffin 0:e2de1fe5b34c 14
Technical_Muffin 0:e2de1fe5b34c 15 int main()
Technical_Muffin 3:b913f8ea69e8 16 {
Technical_Muffin 7:46d9f01afba2 17 float cycle = 0.7f;//define the speed of the motor
Technical_Muffin 3:b913f8ea69e8 18 bool motor1_on = 1;
Technical_Muffin 0:e2de1fe5b34c 19 int motor1_dir=0;
Technical_Muffin 0:e2de1fe5b34c 20
Technical_Muffin 3:b913f8ea69e8 21 while(1){
Technical_Muffin 3:b913f8ea69e8 22 led3.write(0);
Technical_Muffin 3:b913f8ea69e8 23 led1.write(1);
Technical_Muffin 3:b913f8ea69e8 24 led2.write(1);
Technical_Muffin 4:60bc2ee4d838 25 speed1.write(0);
Technical_Muffin 3:b913f8ea69e8 26 pc.baud(115200);
Technical_Muffin 7:46d9f01afba2 27 int diffa = button1.read();
Technical_Muffin 7:46d9f01afba2 28 wait(0.2);
Technical_Muffin 7:46d9f01afba2 29 int diffb = button1.read();
Technical_Muffin 7:46d9f01afba2 30 int button_toggle = diffa-diffb;
Technical_Muffin 7:46d9f01afba2 31 if(button_toggle == 1 && motor1_dir == 1){
Technical_Muffin 7:46d9f01afba2 32 motor1_dir = 0;
Technical_Muffin 7:46d9f01afba2 33 }
Technical_Muffin 7:46d9f01afba2 34 else if(button_toggle == 1 && motor1_dir == 0){
Technical_Muffin 7:46d9f01afba2 35 motor1_dir = 1;
Technical_Muffin 7:46d9f01afba2 36 }
Technical_Muffin 3:b913f8ea69e8 37
Technical_Muffin 3:b913f8ea69e8 38 while(button1.read() != motor1_on){// turn on motor 1 when the button is being pressed
Technical_Muffin 2:27fe9488ba61 39 led3.write(1);
Technical_Muffin 2:27fe9488ba61 40 led1.write(0);
Technical_Muffin 6:206fff3b20b0 41 speed1.write(cycle);//write the speed to the motor
Technical_Muffin 6:206fff3b20b0 42 speed1.period_us(100);//Set period of PWM to 100 us.
Technical_Muffin 6:206fff3b20b0 43 pc.printf("%f",speed1.read());//klopt nog niet, maar voorlopig zorgt het uitlezen ervoor dat het werkt.
Technical_Muffin 5:a1376ab695f3 44 direction1.write(motor1_dir);//turn motor CCW or CW
Technical_Muffin 0:e2de1fe5b34c 45 //motor CW = 0
Technical_Muffin 0:e2de1fe5b34c 46 //motor CCW = 1
Technical_Muffin 6:206fff3b20b0 47 }
Technical_Muffin 7:46d9f01afba2 48
Technical_Muffin 6:206fff3b20b0 49
Technical_Muffin 0:e2de1fe5b34c 50 }
Technical_Muffin 3:b913f8ea69e8 51 }
Technical_Muffin 0:e2de1fe5b34c 52 //determine speed of movement from the encoder signal
Technical_Muffin 0:e2de1fe5b34c 53 // the amount of counts for one revolution is 32
Technical_Muffin 0:e2de1fe5b34c 54 //this is X2 encodng, as the QEI uses X2 by default
Technical_Muffin 0:e2de1fe5b34c 55 //and the motor encoder has a X4 encoder and thus 64 counts per revolution
Technical_Muffin 0:e2de1fe5b34c 56 //thus keeping in mind the gearbox with 1:131,25 gear ratio:
Technical_Muffin 0:e2de1fe5b34c 57 // the amount of counts for a gearbox revolution or resolution is 8400
Technical_Muffin 0:e2de1fe5b34c 58 // a revolution is 2 pi rad, resolution is : 8400/2 pi = 1336.90 counts/rad
Technical_Muffin 0:e2de1fe5b34c 59 //or for the encoder x2 its 668,45 counts/rad
Technical_Muffin 0:e2de1fe5b34c 60 /*
Technical_Muffin 0:e2de1fe5b34c 61 Rotational position in degrees can be calculated by:
Technical_Muffin 0:e2de1fe5b34c 62 *
Technical_Muffin 0:e2de1fe5b34c 63 * (pulse count / X * N) * 360
Technical_Muffin 0:e2de1fe5b34c 64 *
Technical_Muffin 0:e2de1fe5b34c 65 * Where X is the encoding type [e.g. X4 encoding => X=4], and N is the number
Technical_Muffin 0:e2de1fe5b34c 66 * of pulses per revolution.
Technical_Muffin 0:e2de1fe5b34c 67 *
Technical_Muffin 0:e2de1fe5b34c 68 * Linear position can be calculated by:
Technical_Muffin 0:e2de1fe5b34c 69 *
Technical_Muffin 0:e2de1fe5b34c 70 * (pulse count / X * N) * (1 / PPI)
Technical_Muffin 0:e2de1fe5b34c 71 *
Technical_Muffin 0:e2de1fe5b34c 72 * Where X is encoding type [e.g. X4 encoding => X=44], N is the number of
Technical_Muffin 0:e2de1fe5b34c 73 * pulses per revolution, and PPI is pulses per inch, or the equivalent for
Technical_Muffin 0:e2de1fe5b34c 74 * any other unit of displacement. PPI can be calculated by taking the
Technical_Muffin 0:e2de1fe5b34c 75 * circumference of the wheel or encoder disk and dividing it by the number
Technical_Muffin 0:e2de1fe5b34c 76 * of pulses per revolution.
Technical_Muffin 0:e2de1fe5b34c 77
Technical_Muffin 0:e2de1fe5b34c 78 * Reset the encoder.
Technical_Muffin 0:e2de1fe5b34c 79 *
Technical_Muffin 0:e2de1fe5b34c 80 * Sets the pulses and revolutions count to zero.
Technical_Muffin 0:e2de1fe5b34c 81
Technical_Muffin 0:e2de1fe5b34c 82 void reset(void);
Technical_Muffin 0:e2de1fe5b34c 83 */
Technical_Muffin 0:e2de1fe5b34c 84