K K / Mbed 2 deprecated Movement_code

Dependencies:   MODSERIAL QEI mbed

Committer:
Technical_Muffin
Date:
Wed Oct 07 17:33:57 2015 +0000
Revision:
3:b913f8ea69e8
Parent:
2:27fe9488ba61
Child:
4:60bc2ee4d838
Working original code, with responsive leds.

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 0:e2de1fe5b34c 17 float cycle = 0.6;//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 int motor1_cw=0;
Technical_Muffin 0:e2de1fe5b34c 21 int motor1_ccw=1;
Technical_Muffin 0:e2de1fe5b34c 22
Technical_Muffin 3:b913f8ea69e8 23 while(1){
Technical_Muffin 3:b913f8ea69e8 24 led3.write(0);
Technical_Muffin 3:b913f8ea69e8 25 led1.write(1);
Technical_Muffin 3:b913f8ea69e8 26 led2.write(1);
Technical_Muffin 3:b913f8ea69e8 27 pc.baud(115200);
Technical_Muffin 3:b913f8ea69e8 28
Technical_Muffin 3:b913f8ea69e8 29 while(button1.read() != motor1_on){// turn on motor 1 when the button is being pressed
Technical_Muffin 2:27fe9488ba61 30 led3.write(1);
Technical_Muffin 2:27fe9488ba61 31 led1.write(0);
Technical_Muffin 2:27fe9488ba61 32
Technical_Muffin 0:e2de1fe5b34c 33 direction1.write(motor1_dir);//turn motor CCW or CW if set to 0
Technical_Muffin 0:e2de1fe5b34c 34 //motor CW = 0
Technical_Muffin 0:e2de1fe5b34c 35 //motor CCW = 1
Technical_Muffin 0:e2de1fe5b34c 36 speed1.period_us(100);//Set period of PWM to 100 us.
Technical_Muffin 0:e2de1fe5b34c 37 speed1.write(cycle);//write the speed to the motor
Technical_Muffin 0:e2de1fe5b34c 38 }
Technical_Muffin 0:e2de1fe5b34c 39 }
Technical_Muffin 3:b913f8ea69e8 40 }
Technical_Muffin 0:e2de1fe5b34c 41 //determine speed of movement from the encoder signal
Technical_Muffin 0:e2de1fe5b34c 42 // the amount of counts for one revolution is 32
Technical_Muffin 0:e2de1fe5b34c 43 //this is X2 encodng, as the QEI uses X2 by default
Technical_Muffin 0:e2de1fe5b34c 44 //and the motor encoder has a X4 encoder and thus 64 counts per revolution
Technical_Muffin 0:e2de1fe5b34c 45 //thus keeping in mind the gearbox with 1:131,25 gear ratio:
Technical_Muffin 0:e2de1fe5b34c 46 // the amount of counts for a gearbox revolution or resolution is 8400
Technical_Muffin 0:e2de1fe5b34c 47 // a revolution is 2 pi rad, resolution is : 8400/2 pi = 1336.90 counts/rad
Technical_Muffin 0:e2de1fe5b34c 48 //or for the encoder x2 its 668,45 counts/rad
Technical_Muffin 0:e2de1fe5b34c 49 /*
Technical_Muffin 0:e2de1fe5b34c 50 Rotational position in degrees can be calculated by:
Technical_Muffin 0:e2de1fe5b34c 51 *
Technical_Muffin 0:e2de1fe5b34c 52 * (pulse count / X * N) * 360
Technical_Muffin 0:e2de1fe5b34c 53 *
Technical_Muffin 0:e2de1fe5b34c 54 * Where X is the encoding type [e.g. X4 encoding => X=4], and N is the number
Technical_Muffin 0:e2de1fe5b34c 55 * of pulses per revolution.
Technical_Muffin 0:e2de1fe5b34c 56 *
Technical_Muffin 0:e2de1fe5b34c 57 * Linear position can be calculated by:
Technical_Muffin 0:e2de1fe5b34c 58 *
Technical_Muffin 0:e2de1fe5b34c 59 * (pulse count / X * N) * (1 / PPI)
Technical_Muffin 0:e2de1fe5b34c 60 *
Technical_Muffin 0:e2de1fe5b34c 61 * Where X is encoding type [e.g. X4 encoding => X=44], N is the number of
Technical_Muffin 0:e2de1fe5b34c 62 * pulses per revolution, and PPI is pulses per inch, or the equivalent for
Technical_Muffin 0:e2de1fe5b34c 63 * any other unit of displacement. PPI can be calculated by taking the
Technical_Muffin 0:e2de1fe5b34c 64 * circumference of the wheel or encoder disk and dividing it by the number
Technical_Muffin 0:e2de1fe5b34c 65 * of pulses per revolution.
Technical_Muffin 0:e2de1fe5b34c 66
Technical_Muffin 0:e2de1fe5b34c 67 * Reset the encoder.
Technical_Muffin 0:e2de1fe5b34c 68 *
Technical_Muffin 0:e2de1fe5b34c 69 * Sets the pulses and revolutions count to zero.
Technical_Muffin 0:e2de1fe5b34c 70
Technical_Muffin 0:e2de1fe5b34c 71 void reset(void);
Technical_Muffin 0:e2de1fe5b34c 72 */
Technical_Muffin 0:e2de1fe5b34c 73