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:
- Dbee16
- Date:
- 2015-03-13
- Revision:
- 12:2847b345b5cf
- Parent:
- 8:eefbd3880d28
- Child:
- 13:c22e150048ae
File content as of revision 12:2847b345b5cf:
#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 led(LED_RED); //Hall Effect Sensors InterruptIn hall_l (PTA1); //Hall Effect Sensor Output RobotLeft InterruptIn hall_r (PTA2); //Hall Effect Sensor Output RobotRight float time1_l, time2_l, time3_l, time1_r, time2_r, time3_r; //Bluetooth Module to debug Serial blue(PTC4,PTC3); void set_direction( int direction, float duty_l, float duty_r) { switch( direction ) { case 0x11: { //forward motor_r.write( duty_r); //Look up ternary statements -- {condition ? value_if_true : value_if_false} This makes 0<result<1 motor_l.write( duty_l); motor_rf=1; motor_rb=0; motor_lf=1; motor_lb=0; blue.printf("Going forward, right:%f ; left:%f\n", duty_r, duty_l); break; } case 0x00: { //backward motor_r.write( duty_r); motor_l.write( duty_l); motor_rf=0; motor_rb=1; motor_lf=0; motor_lb=1; blue.printf("Going backward, right:%f ; left:%f\n", duty_r, duty_l); break; } case 0x01: { //spin left -- Right forward, left backward motor_r.write( duty_r); motor_l.write( duty_l); motor_rf=1; motor_rb=0; motor_lf=0; motor_lb=1; blue.printf("Spinning Left, right:%f ; left:%f\n", duty_r, duty_l); break; } case 0x10: { //spin right motor_r.write( duty_r); motor_l.write( duty_l); motor_rf=0; motor_rb=1; motor_lf=1; motor_lb=0; blue.printf("Spinning Right, right:%f ; left:%f\n", duty_r, duty_l); break; } } } //HALL EFFECT INITIALISATIONS Timer h_r, h_l; float hall_rt1, hall_lt1, hall_rt2, hall_lt2 = 0; //hall right time & left time float hall_rdt, hall_ldt; int h_f_r, h_f_l; //hallflagright and left void hallr() //this just reading the various times. { if (h_f_r==2) { h_r.start(); hall_rt1=0; hall_rt2 =0; blue.printf("Timer R Starts\n"); h_f_r =1; } else if(h_f_r == 1) { hall_rt1 = h_r.read(); //blue.printf("rt1 : %f\n", hall_rt1); h_f_r = 0; hall_rdt = hall_rt1-hall_rt2; } else if (h_f_r == 0) { hall_rt2 = h_r.read(); //blue.printf("rt2 : %f\n", hall_rt2); h_f_r = 1; hall_rdt = hall_rt2-hall_rt1; } //blue.printf("Hall_R dt : %f\n", hall_rdt); } void halll() { blue.printf("Left been triggered\n"); if (h_f_l==2) { h_l.start(); hall_lt1=0; hall_lt2 =0; blue.printf("Timer L starts\n"); h_f_l = 1; } else if(h_f_l == 1) { hall_lt1 = h_l.read(); //blue.printf("lt1 : %f\n", hall_lt1); h_f_l = 0; hall_ldt = hall_lt1-hall_lt2; } else if (h_f_l == 0) { hall_lt2 = h_l.read(); // blue.printf("lt1 : %f\n", hall_lt1); h_f_l = 1; hall_ldt = hall_lt2-hall_lt1; } //blue.printf("Hall_L dt : %f\n", hall_ldt); } int main() { h_f_r = 2; h_f_l = 2; //attaching hall_r and hall_r so that it calls hall() on a rising and falling edge hall_r.rise(&hallr); hall_l.rise(&halll); //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; wait(1); set_direction(0x11, 0.3,0.3); while(1) { led = !led; wait(0.5); } }