K K / Mbed 2 deprecated Movement_code

Dependencies:   MODSERIAL QEI mbed

Committer:
Technical_Muffin
Date:
Mon Oct 05 21:00:15 2015 +0000
Revision:
0:e2de1fe5b34c
Child:
1:17e250b48fe0
Child:
2:27fe9488ba61
first part of movement code

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