Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MODSERIAL QEI mbed
main.cpp@6:206fff3b20b0, 2015-10-09 (annotated)
- Committer:
- Technical_Muffin
- Date:
- Fri Oct 09 09:33:24 2015 +0000
- Revision:
- 6:206fff3b20b0
- Parent:
- 5:a1376ab695f3
- Child:
- 7:46d9f01afba2
start of toggle/movement combination
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 | 6:206fff3b20b0 | 31 | speed1.write(cycle);//write the speed to the motor |
| Technical_Muffin | 6:206fff3b20b0 | 32 | speed1.period_us(100);//Set period of PWM to 100 us. |
| Technical_Muffin | 6:206fff3b20b0 | 33 | pc.printf("%f",speed1.read());//klopt nog niet, maar voorlopig zorgt het uitlezen ervoor dat het werkt. |
| Technical_Muffin | 5:a1376ab695f3 | 34 | direction1.write(motor1_dir);//turn motor CCW or CW |
| Technical_Muffin | 0:e2de1fe5b34c | 35 | //motor CW = 0 |
| Technical_Muffin | 0:e2de1fe5b34c | 36 | //motor CCW = 1 |
| Technical_Muffin | 6:206fff3b20b0 | 37 | } |
| Technical_Muffin | 6:206fff3b20b0 | 38 | pc.printf("toggle on or off %d \n ", button1.read()); |
| Technical_Muffin | 6:206fff3b20b0 | 39 | int diffa = button1.read(); |
| Technical_Muffin | 6:206fff3b20b0 | 40 | wait(0.2); |
| Technical_Muffin | 6:206fff3b20b0 | 41 | int diffb = button1.read(); |
| Technical_Muffin | 6:206fff3b20b0 | 42 | int button_toggle = diffa-diffb; |
| Technical_Muffin | 6:206fff3b20b0 | 43 | if(button_toggle == 1 && led1.read() ==1){ |
| Technical_Muffin | 6:206fff3b20b0 | 44 | led1.write(0); |
| Technical_Muffin | 6:206fff3b20b0 | 45 | led2.write(1); |
| Technical_Muffin | 6:206fff3b20b0 | 46 | } |
| Technical_Muffin | 6:206fff3b20b0 | 47 | else if(button_toggle == 1 && led2.read() == 1){ |
| Technical_Muffin | 6:206fff3b20b0 | 48 | led1.write(1); |
| Technical_Muffin | 6:206fff3b20b0 | 49 | led2.write(0); |
| Technical_Muffin | 6:206fff3b20b0 | 50 | } |
| Technical_Muffin | 6:206fff3b20b0 | 51 | |
| Technical_Muffin | 0:e2de1fe5b34c | 52 | } |
| Technical_Muffin | 3:b913f8ea69e8 | 53 | } |
| Technical_Muffin | 0:e2de1fe5b34c | 54 | //determine speed of movement from the encoder signal |
| Technical_Muffin | 0:e2de1fe5b34c | 55 | // the amount of counts for one revolution is 32 |
| Technical_Muffin | 0:e2de1fe5b34c | 56 | //this is X2 encodng, as the QEI uses X2 by default |
| Technical_Muffin | 0:e2de1fe5b34c | 57 | //and the motor encoder has a X4 encoder and thus 64 counts per revolution |
| Technical_Muffin | 0:e2de1fe5b34c | 58 | //thus keeping in mind the gearbox with 1:131,25 gear ratio: |
| Technical_Muffin | 0:e2de1fe5b34c | 59 | // the amount of counts for a gearbox revolution or resolution is 8400 |
| Technical_Muffin | 0:e2de1fe5b34c | 60 | // a revolution is 2 pi rad, resolution is : 8400/2 pi = 1336.90 counts/rad |
| Technical_Muffin | 0:e2de1fe5b34c | 61 | //or for the encoder x2 its 668,45 counts/rad |
| Technical_Muffin | 0:e2de1fe5b34c | 62 | /* |
| Technical_Muffin | 0:e2de1fe5b34c | 63 | Rotational position in degrees can be calculated by: |
| Technical_Muffin | 0:e2de1fe5b34c | 64 | * |
| Technical_Muffin | 0:e2de1fe5b34c | 65 | * (pulse count / X * N) * 360 |
| Technical_Muffin | 0:e2de1fe5b34c | 66 | * |
| Technical_Muffin | 0:e2de1fe5b34c | 67 | * Where X is the encoding type [e.g. X4 encoding => X=4], and N is the number |
| Technical_Muffin | 0:e2de1fe5b34c | 68 | * of pulses per revolution. |
| Technical_Muffin | 0:e2de1fe5b34c | 69 | * |
| Technical_Muffin | 0:e2de1fe5b34c | 70 | * Linear position can be calculated by: |
| Technical_Muffin | 0:e2de1fe5b34c | 71 | * |
| Technical_Muffin | 0:e2de1fe5b34c | 72 | * (pulse count / X * N) * (1 / PPI) |
| Technical_Muffin | 0:e2de1fe5b34c | 73 | * |
| Technical_Muffin | 0:e2de1fe5b34c | 74 | * Where X is encoding type [e.g. X4 encoding => X=44], N is the number of |
| Technical_Muffin | 0:e2de1fe5b34c | 75 | * pulses per revolution, and PPI is pulses per inch, or the equivalent for |
| Technical_Muffin | 0:e2de1fe5b34c | 76 | * any other unit of displacement. PPI can be calculated by taking the |
| Technical_Muffin | 0:e2de1fe5b34c | 77 | * circumference of the wheel or encoder disk and dividing it by the number |
| Technical_Muffin | 0:e2de1fe5b34c | 78 | * of pulses per revolution. |
| Technical_Muffin | 0:e2de1fe5b34c | 79 | |
| Technical_Muffin | 0:e2de1fe5b34c | 80 | * Reset the encoder. |
| Technical_Muffin | 0:e2de1fe5b34c | 81 | * |
| Technical_Muffin | 0:e2de1fe5b34c | 82 | * Sets the pulses and revolutions count to zero. |
| Technical_Muffin | 0:e2de1fe5b34c | 83 | |
| Technical_Muffin | 0:e2de1fe5b34c | 84 | void reset(void); |
| Technical_Muffin | 0:e2de1fe5b34c | 85 | */ |
| Technical_Muffin | 0:e2de1fe5b34c | 86 |