code for basic movement of robot
Dependencies: MODSERIAL QEI mbed
main.cpp@5:a1376ab695f3, 2015-10-07 (annotated)
- Committer:
- Technical_Muffin
- Date:
- Wed Oct 07 18:33:16 2015 +0000
- Revision:
- 5:a1376ab695f3
- Parent:
- 4:60bc2ee4d838
- Child:
- 6:206fff3b20b0
door snelheid uit te lezen pakt hij wel goede pwm.
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:a1376ab695f3 | 17 | float cycle = 0.6f;//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 | 3:b913f8ea69e8 | 27 | |
Technical_Muffin | 3:b913f8ea69e8 | 28 | while(button1.read() != motor1_on){// turn on motor 1 when the button is being pressed |
Technical_Muffin | 2:27fe9488ba61 | 29 | led3.write(1); |
Technical_Muffin | 2:27fe9488ba61 | 30 | led1.write(0); |
Technical_Muffin | 5:a1376ab695f3 | 31 | pc.printf("%f", speed1.read()); |
Technical_Muffin | 5:a1376ab695f3 | 32 | direction1.write(motor1_dir);//turn motor CCW or CW |
Technical_Muffin | 0:e2de1fe5b34c | 33 | //motor CW = 0 |
Technical_Muffin | 0:e2de1fe5b34c | 34 | //motor CCW = 1 |
Technical_Muffin | 0:e2de1fe5b34c | 35 | speed1.period_us(100);//Set period of PWM to 100 us. |
Technical_Muffin | 0:e2de1fe5b34c | 36 | speed1.write(cycle);//write the speed to the motor |
Technical_Muffin | 0:e2de1fe5b34c | 37 | } |
Technical_Muffin | 0:e2de1fe5b34c | 38 | } |
Technical_Muffin | 3:b913f8ea69e8 | 39 | } |
Technical_Muffin | 0:e2de1fe5b34c | 40 | //determine speed of movement from the encoder signal |
Technical_Muffin | 0:e2de1fe5b34c | 41 | // the amount of counts for one revolution is 32 |
Technical_Muffin | 0:e2de1fe5b34c | 42 | //this is X2 encodng, as the QEI uses X2 by default |
Technical_Muffin | 0:e2de1fe5b34c | 43 | //and the motor encoder has a X4 encoder and thus 64 counts per revolution |
Technical_Muffin | 0:e2de1fe5b34c | 44 | //thus keeping in mind the gearbox with 1:131,25 gear ratio: |
Technical_Muffin | 0:e2de1fe5b34c | 45 | // the amount of counts for a gearbox revolution or resolution is 8400 |
Technical_Muffin | 0:e2de1fe5b34c | 46 | // a revolution is 2 pi rad, resolution is : 8400/2 pi = 1336.90 counts/rad |
Technical_Muffin | 0:e2de1fe5b34c | 47 | //or for the encoder x2 its 668,45 counts/rad |
Technical_Muffin | 0:e2de1fe5b34c | 48 | /* |
Technical_Muffin | 0:e2de1fe5b34c | 49 | Rotational position in degrees can be calculated by: |
Technical_Muffin | 0:e2de1fe5b34c | 50 | * |
Technical_Muffin | 0:e2de1fe5b34c | 51 | * (pulse count / X * N) * 360 |
Technical_Muffin | 0:e2de1fe5b34c | 52 | * |
Technical_Muffin | 0:e2de1fe5b34c | 53 | * Where X is the encoding type [e.g. X4 encoding => X=4], and N is the number |
Technical_Muffin | 0:e2de1fe5b34c | 54 | * of pulses per revolution. |
Technical_Muffin | 0:e2de1fe5b34c | 55 | * |
Technical_Muffin | 0:e2de1fe5b34c | 56 | * Linear position can be calculated by: |
Technical_Muffin | 0:e2de1fe5b34c | 57 | * |
Technical_Muffin | 0:e2de1fe5b34c | 58 | * (pulse count / X * N) * (1 / PPI) |
Technical_Muffin | 0:e2de1fe5b34c | 59 | * |
Technical_Muffin | 0:e2de1fe5b34c | 60 | * Where X is encoding type [e.g. X4 encoding => X=44], N is the number of |
Technical_Muffin | 0:e2de1fe5b34c | 61 | * pulses per revolution, and PPI is pulses per inch, or the equivalent for |
Technical_Muffin | 0:e2de1fe5b34c | 62 | * any other unit of displacement. PPI can be calculated by taking the |
Technical_Muffin | 0:e2de1fe5b34c | 63 | * circumference of the wheel or encoder disk and dividing it by the number |
Technical_Muffin | 0:e2de1fe5b34c | 64 | * of pulses per revolution. |
Technical_Muffin | 0:e2de1fe5b34c | 65 | |
Technical_Muffin | 0:e2de1fe5b34c | 66 | * Reset the encoder. |
Technical_Muffin | 0:e2de1fe5b34c | 67 | * |
Technical_Muffin | 0:e2de1fe5b34c | 68 | * Sets the pulses and revolutions count to zero. |
Technical_Muffin | 0:e2de1fe5b34c | 69 | |
Technical_Muffin | 0:e2de1fe5b34c | 70 | void reset(void); |
Technical_Muffin | 0:e2de1fe5b34c | 71 | */ |
Technical_Muffin | 0:e2de1fe5b34c | 72 |