CE201 Embedded : 2022-23 / Mbed 2 deprecated RCCar7

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 PwmOut steering(p21);
00004 PwmOut velocity(p22);
00005 
00006 float vo=0;
00007 
00008 // Velocity expects -1 (reverse) to +1 (forward)
00009 void Velocity(float v) {
00010     v=v+1;
00011     if (v>=0 && v<=2) {
00012         if (vo>=1 && v<1) {                 //
00013             velocity.pulsewidth(0.0014);    // this is required to
00014             wait(0.1);                      //
00015             velocity.pulsewidth(0.0015);    // move into reverse
00016             wait(0.1);                      //
00017         }                                   //
00018         velocity.pulsewidth(v/2000+0.001);
00019         vo=v;
00020     }
00021 }
00022 // Steering expects -1 (left) to +1 (right)
00023 void Steering(float s) {
00024     s=s+1;
00025     if (s>=0 && s<=2) {
00026         steering.pulsewidth(s/2000+0.001);
00027     }
00028 }
00029 
00030 int main() { 
00031     velocity.period(0.02);
00032     steering.period(0.02);
00033     Velocity(0); // initiate the drive motor (this must be done)
00034     Steering(0); // centre steering 
00035     wait(0.5);
00036     while(1) {
00037         for(int i=0; i<100; i++) {          // stop to full forward
00038             Velocity(i/100.0);
00039             wait(0.1);
00040         }
00041         for(int i=100; i>-100; i--) {       // full forward to full reverse
00042             Velocity(i/100.0);
00043             wait(0.1);
00044         }
00045         for(int i=-100; i<0; i++) {         // full reverse to stop
00046             Velocity(i/100.0);
00047             wait(0.1);
00048         }
00049         for(int i=0; i<100; i++) {          // centre to full right
00050             Steering(i/100.0);
00051             wait(0.1);
00052         }
00053         for(int i=100; i>-100; i--) {       // full right to full left
00054             Steering(i/100.0);
00055             wait(0.1);
00056         }
00057         for(int i=-100; i<0; i++) {         // full left to centre
00058             Steering(i/100.0);
00059             wait(0.1);
00060         }
00061     }
00062 }