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: mbed
main.cpp
- Committer:
- cmcmaster
- Date:
- 2015-02-04
- Revision:
- 0:59d25bb7f825
- Child:
- 1:2bab3a0bc3bc
File content as of revision 0:59d25bb7f825:
#include "mbed.h"
//motor select pins
DigitalOut motor_lf(PTE2);
DigitalOut motor_lb(PTE3);
DigitalOut motor_rf(PTE4);
DigitalOut motor_rb(PTE5);
//Frequency of Pulse Width Modulated signal in Hz
#define PWM_FREQ 1000
//PWM pin (Enable 1 and 2)
PwmOut motor_l (PTC2);
PwmOut motor_r (PTE29);
//LED to test
DigitalOut myled(LED_RED);
/*
Pivot Right: //Change duties for speed
motor_rf=0;
motor_rb=0;
motor_lf=1;
motor_lb=0;
Pivot Left: //Change duties for speed
motor_rf=1;
motor_rb=0;
motor_lf=0;
motor_lb=0;
Forwards: //Change duties for speed & direction
motor_rf=1;
motor_rb=0;
motor_lf=1;
motor_lb=0;
Backwards: //Change duties for speed & direction
motor_rf=0;
motor_rb=1;
motor_lf=0;
motor_lb=1;
*/
void set_direction( int nature_of_direction, float speed)
{
switch(nature_of_direction) {
case "forward": {
motor_r.write(speed + angle); //where angle can be postive and negative and will TURN the robot
motor_l.write(speed - angle);
//if turning left, you'd make the angle postive (right motor goes faster left motor goes slwoer)
motor_rf=1;
motor_rb=0;
motor_lf=1;
motor_lb=0;
}
case "backwards": {
motor_r.write(speed + angle); //where angle can be postive and negative and will TURN the robot
motor_l.write(speed - angle);
//if turning left, you'd make the angle postive (right motor goes faster left motor goes slwoer)
motor_rf=0;
motor_rb=1;
motor_lf=0;
motor_lb=1;
}
case "pivot right": {
motor_r.write(0); //motor doesn't need to be enabled
motor_l.write(speed);
motor_rf=0;
motor_rb=0;
motor_lf=1;
motor_lb=0;
}
case "pivot left": {
motor_r.write(speed);
motor_l.write(0); //motor doesn't need to be enabled
motor_rf=1;
motor_rb=0;
motor_lf=0;
motor_lb=0;
}
}
}
int main()
{
//Set PWM frequency to 1000Hz
motor_l.period( 1.0f / (float) PWM_FREQ);
motor_r.period( 1.0f / (float) PWM_FREQ);
//Initialise direction to nothing.
motor_rf=0;
motor_rb=0;
motor_lf=0;
motor_lb=0;
while(1) { //infinite loop
//***********************************************
myled = !myled;
//Set duty cycle at 100% AKA Full Speed
motor_r.write(1.0);
motor_l.write(1.0);
motor_rf=1;
motor_rb=0;
motor_lf=1;
motor_lb=0; //go forwards
wait(2);
//**************************************
myled = !myled;
motor_r.write(0.75);
motor_l.write(0.75);
motor_rf=1;
motor_rb=0;
motor_lf=1;
motor_lb=0; //go forwards
//
wait(2);
myled = !myled;
motor_r.write(0.5);
motor_l.write(0.5);
motor_rf=1;
motor_rb=0;
motor_lf=1;
motor_lb=0;
wait(2);
myled = !myled;
motor_r.write(0.30);
motor_l.write(0.30);
motor_rf=1;
motor_rb=0;
motor_lf=1;
motor_lb=0;
wait(2);
myled = !myled;
motor_r.write(1.0);
motor_l.write(1.0);
motor_rf=0;
motor_rb=1;
motor_lf=0;
motor_lb=1; //go forwards
//
wait(2);
myled = !myled;
motor_r.write(0.5);
motor_l.write(0.5);
motor_rf=0;
motor_rb=0;
motor_lf=0;
motor_lb=1; //
wait(2);
myled = !myled;
motor_rf=0;
motor_rb=1;
motor_lf=0;
motor_lb=0; //reverse turn
wait(2);
myled = !myled;
motor_r.write(0.5);
motor_l.write(1.0);
motor_rf=1;
motor_rb=0;
motor_lf=1;
motor_lb=0; //go forwards
//
wait(4);
}
}