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.
main.cpp@0:fb7201282a86, 2021-03-21 (annotated)
- Committer:
- P3nguin18
- Date:
- Sun Mar 21 19:54:07 2021 +0000
- Revision:
- 0:fb7201282a86
Motor Stuff;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| P3nguin18 | 0:fb7201282a86 | 1 | #include "mbed.h" |
| P3nguin18 | 0:fb7201282a86 | 2 | #include <Motor.h> |
| P3nguin18 | 0:fb7201282a86 | 3 | |
| P3nguin18 | 0:fb7201282a86 | 4 | Motor motor0(p5,p6,p7); |
| P3nguin18 | 0:fb7201282a86 | 5 | Motor motor1(p8,p11,p12); |
| P3nguin18 | 0:fb7201282a86 | 6 | Motor motor2(p13,p14,p15); //pull,drive,enable |
| P3nguin18 | 0:fb7201282a86 | 7 | Motor motor3(p16,p17,p18); |
| P3nguin18 | 0:fb7201282a86 | 8 | Motor motor4(p21,p22,p23); |
| P3nguin18 | 0:fb7201282a86 | 9 | Motor motor5(p24,p25,p26); |
| P3nguin18 | 0:fb7201282a86 | 10 | |
| P3nguin18 | 0:fb7201282a86 | 11 | const int HIGH = 1; |
| P3nguin18 | 0:fb7201282a86 | 12 | const int LOW = 0; |
| P3nguin18 | 0:fb7201282a86 | 13 | |
| P3nguin18 | 0:fb7201282a86 | 14 | //@@@@@@@@@@@@@@@__CONTROL__@@@@@@@@@@@@@@@@@@@ |
| P3nguin18 | 0:fb7201282a86 | 15 | const int PPR = 400; //Pulses Per Revolution |
| P3nguin18 | 0:fb7201282a86 | 16 | int ROTATION = 36000; //3600 full rotation |
| P3nguin18 | 0:fb7201282a86 | 17 | int RPM = 10; //Rotations Per Minute |
| P3nguin18 | 0:fb7201282a86 | 18 | int MOVEMENT = 0; //Movement Type |0=F ,1=B ,2=R ,3=L| |
| P3nguin18 | 0:fb7201282a86 | 19 | int NUMSTEPS = 10; //Number of Steps |
| P3nguin18 | 0:fb7201282a86 | 20 | //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
| P3nguin18 | 0:fb7201282a86 | 21 | |
| P3nguin18 | 0:fb7201282a86 | 22 | int pulseLength = 0; |
| P3nguin18 | 0:fb7201282a86 | 23 | |
| P3nguin18 | 0:fb7201282a86 | 24 | //Converts desired RPM to required Pulse Length (In Microseconds) |
| P3nguin18 | 0:fb7201282a86 | 25 | void Configure_Pulse(float rpm){ |
| P3nguin18 | 0:fb7201282a86 | 26 | float holder = 0; //Create holder variable (MBED is Stupid) |
| P3nguin18 | 0:fb7201282a86 | 27 | |
| P3nguin18 | 0:fb7201282a86 | 28 | holder = rpm; //Set to RPM |
| P3nguin18 | 0:fb7201282a86 | 29 | holder = holder/60; //Brain.exe is no longer responding ;( |
| P3nguin18 | 0:fb7201282a86 | 30 | holder = holder*PPR; //Multiply by PPR |
| P3nguin18 | 0:fb7201282a86 | 31 | holder = holder/2; //Brain.exe is no longer responding ;( |
| P3nguin18 | 0:fb7201282a86 | 32 | holder = 1/holder; //Convert to Seconds |
| P3nguin18 | 0:fb7201282a86 | 33 | holder = holder*1000000; //Convert to Microseconds |
| P3nguin18 | 0:fb7201282a86 | 34 | holder = holder/60; //Dividing by Gear Ratio |
| P3nguin18 | 0:fb7201282a86 | 35 | holder = holder/4; //Math Magic |
| P3nguin18 | 0:fb7201282a86 | 36 | |
| P3nguin18 | 0:fb7201282a86 | 37 | pulseLength = holder; |
| P3nguin18 | 0:fb7201282a86 | 38 | } |
| P3nguin18 | 0:fb7201282a86 | 39 | |
| P3nguin18 | 0:fb7201282a86 | 40 | //Sets Drive Pins to Movement Configuration |
| P3nguin18 | 0:fb7201282a86 | 41 | void Configure_Movement(int input){ |
| P3nguin18 | 0:fb7201282a86 | 42 | switch(input){ |
| P3nguin18 | 0:fb7201282a86 | 43 | //Move Forward |
| P3nguin18 | 0:fb7201282a86 | 44 | case 0: motor0.dir = HIGH; |
| P3nguin18 | 0:fb7201282a86 | 45 | motor1.dir = HIGH; |
| P3nguin18 | 0:fb7201282a86 | 46 | motor2.dir = HIGH; |
| P3nguin18 | 0:fb7201282a86 | 47 | motor3.dir = HIGH; |
| P3nguin18 | 0:fb7201282a86 | 48 | motor4.dir = HIGH; |
| P3nguin18 | 0:fb7201282a86 | 49 | motor5.dir = HIGH; |
| P3nguin18 | 0:fb7201282a86 | 50 | break; |
| P3nguin18 | 0:fb7201282a86 | 51 | //Move Backward |
| P3nguin18 | 0:fb7201282a86 | 52 | case 1: motor0.dir = LOW; |
| P3nguin18 | 0:fb7201282a86 | 53 | motor1.dir = LOW; |
| P3nguin18 | 0:fb7201282a86 | 54 | motor2.dir = LOW; |
| P3nguin18 | 0:fb7201282a86 | 55 | motor3.dir = LOW; |
| P3nguin18 | 0:fb7201282a86 | 56 | motor4.dir = LOW; |
| P3nguin18 | 0:fb7201282a86 | 57 | motor5.dir = LOW; |
| P3nguin18 | 0:fb7201282a86 | 58 | break; |
| P3nguin18 | 0:fb7201282a86 | 59 | //Turn Right |
| P3nguin18 | 0:fb7201282a86 | 60 | case 2: motor0.dir = HIGH; |
| P3nguin18 | 0:fb7201282a86 | 61 | motor1.dir = HIGH; |
| P3nguin18 | 0:fb7201282a86 | 62 | motor2.dir = HIGH; |
| P3nguin18 | 0:fb7201282a86 | 63 | motor3.dir = LOW; |
| P3nguin18 | 0:fb7201282a86 | 64 | motor4.dir = LOW; |
| P3nguin18 | 0:fb7201282a86 | 65 | motor5.dir = LOW; |
| P3nguin18 | 0:fb7201282a86 | 66 | break; |
| P3nguin18 | 0:fb7201282a86 | 67 | //Turn Left |
| P3nguin18 | 0:fb7201282a86 | 68 | case 3: motor0.dir = LOW; |
| P3nguin18 | 0:fb7201282a86 | 69 | motor1.dir = LOW; |
| P3nguin18 | 0:fb7201282a86 | 70 | motor2.dir = LOW; |
| P3nguin18 | 0:fb7201282a86 | 71 | motor3.dir = HIGH; |
| P3nguin18 | 0:fb7201282a86 | 72 | motor4.dir = HIGH; |
| P3nguin18 | 0:fb7201282a86 | 73 | motor5.dir = HIGH; |
| P3nguin18 | 0:fb7201282a86 | 74 | break; |
| P3nguin18 | 0:fb7201282a86 | 75 | } |
| P3nguin18 | 0:fb7201282a86 | 76 | } |
| P3nguin18 | 0:fb7201282a86 | 77 | |
| P3nguin18 | 0:fb7201282a86 | 78 | int main() { |
| P3nguin18 | 0:fb7201282a86 | 79 | Configure_Movement(MOVEMENT); |
| P3nguin18 | 0:fb7201282a86 | 80 | Configure_Pulse(RPM); |
| P3nguin18 | 0:fb7201282a86 | 81 | |
| P3nguin18 | 0:fb7201282a86 | 82 | bool altSet = false; // |
| P3nguin18 | 0:fb7201282a86 | 83 | int inv0 = 0, inv1 = 0; //Creates inverter variables for pulse controll |
| P3nguin18 | 0:fb7201282a86 | 84 | |
| P3nguin18 | 0:fb7201282a86 | 85 | //Loop for Number of Steps Made |
| P3nguin18 | 0:fb7201282a86 | 86 | for(int i=0; i<NUMSTEPS; i++){ |
| P3nguin18 | 0:fb7201282a86 | 87 | if(altSet == false){ |
| P3nguin18 | 0:fb7201282a86 | 88 | //Pulses for total desired Rotation for set0 |
| P3nguin18 | 0:fb7201282a86 | 89 | for(int j=0; j<ROTATION; j++){ |
| P3nguin18 | 0:fb7201282a86 | 90 | inv0 =~ inv0; //Inverts Value |
| P3nguin18 | 0:fb7201282a86 | 91 | motor0.pul = inv0; |
| P3nguin18 | 0:fb7201282a86 | 92 | motor2.pul = inv0; |
| P3nguin18 | 0:fb7201282a86 | 93 | motor4.pul = inv0; |
| P3nguin18 | 0:fb7201282a86 | 94 | |
| P3nguin18 | 0:fb7201282a86 | 95 | //Pulses set1 every 3 pulses of set0 |
| P3nguin18 | 0:fb7201282a86 | 96 | if(j%3 == 0){ |
| P3nguin18 | 0:fb7201282a86 | 97 | inv1 =~ inv1; //Inverts Value |
| P3nguin18 | 0:fb7201282a86 | 98 | motor1.pul = inv1; |
| P3nguin18 | 0:fb7201282a86 | 99 | motor3.pul = inv1; |
| P3nguin18 | 0:fb7201282a86 | 100 | motor5.pul = inv1; |
| P3nguin18 | 0:fb7201282a86 | 101 | } |
| P3nguin18 | 0:fb7201282a86 | 102 | wait_us(pulseLength); |
| P3nguin18 | 0:fb7201282a86 | 103 | } |
| P3nguin18 | 0:fb7201282a86 | 104 | altSet = true; |
| P3nguin18 | 0:fb7201282a86 | 105 | }else if(altSet == true){ |
| P3nguin18 | 0:fb7201282a86 | 106 | //Pulses for total desired Rotation for set1 |
| P3nguin18 | 0:fb7201282a86 | 107 | for(int j=0; j<ROTATION; j++){ |
| P3nguin18 | 0:fb7201282a86 | 108 | inv1 =~ inv1; //Inverts Value |
| P3nguin18 | 0:fb7201282a86 | 109 | motor1.pul = inv1; |
| P3nguin18 | 0:fb7201282a86 | 110 | motor3.pul = inv1; |
| P3nguin18 | 0:fb7201282a86 | 111 | motor5.pul = inv1; |
| P3nguin18 | 0:fb7201282a86 | 112 | |
| P3nguin18 | 0:fb7201282a86 | 113 | //Pulses set0 every 3 pulses of set1 |
| P3nguin18 | 0:fb7201282a86 | 114 | if(j%3 == 0){ |
| P3nguin18 | 0:fb7201282a86 | 115 | inv0 =~ inv0; //Inverts Value |
| P3nguin18 | 0:fb7201282a86 | 116 | motor0.pul = inv0; |
| P3nguin18 | 0:fb7201282a86 | 117 | motor2.pul = inv0; |
| P3nguin18 | 0:fb7201282a86 | 118 | motor4.pul = inv0; |
| P3nguin18 | 0:fb7201282a86 | 119 | } |
| P3nguin18 | 0:fb7201282a86 | 120 | wait_us(pulseLength); |
| P3nguin18 | 0:fb7201282a86 | 121 | } |
| P3nguin18 | 0:fb7201282a86 | 122 | altSet = false; |
| P3nguin18 | 0:fb7201282a86 | 123 | } |
| P3nguin18 | 0:fb7201282a86 | 124 | } |
| P3nguin18 | 0:fb7201282a86 | 125 | } |