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-11
- Revision:
- 6:dd0a91c2994f
- Parent:
- 5:22d7fee2e26e
File content as of revision 6:dd0a91c2994f:
#include "mbed.h" Serial blue(PTC4,PTC3); //motor select pins DigitalOut motor_lf(PTE2); DigitalOut motor_lb(PTE3); DigitalOut motor_rf(PTE4); DigitalOut motor_rb(PTE5); DigitalIn input(PTC1); //input from sensor array DigitalOut Line_right(LED_GREEN);//no line detected //DigitalOut blue(LED_BLUE); BusOut sensor(PTD7,PTE1,PTE0);//multiplex sensors AnalogOut level(PTE30);//set comparator level needs to be tuned for each sensor (could create program) //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); void sensor_select(int x)//converts int to hex for display { switch(x) { case 0: sensor = 0x2; break; case 1: sensor = 0x1; break; case 2: sensor = 0x0; break; case 3: sensor = 0x3; break; case 4: sensor = 0x5; break; case 5: sensor = 0x7; break; case 6: sensor = 0x6; break; case 7: sensor = 0x4; break; } } int sensor_read() { int val=0; level = 0.3; int x = 0; //static int values[8]; while( x <= 3 ) { //why 3? Don't you want to select through 8 sensors therefore x<=7? sensor_select(x); //blue = !blue; wait(0.1); if (input == 1) { val=val+2^x; //could you comment on what this is for? } else { val=val;//+2^x; } x=x+1; } blue.printf("%i\n",val); return val; } void set_direction( int direction, float speed, float angle) { float r_result = (speed + angle); //where angle can be postive and negative and will TURN the robot float l_result = (speed - angle); //if turning left, you'd make the angle postive (right motor goes faster left motor goes slwoer) switch( direction ) { case 0x11: { //forward motor_r.write( r_result < 0 ? 0 : r_result > 1 ? 1 : r_result); //Look up ternary statements -- {condition ? value_if_true : value_if_false} This makes 0<result<1 motor_l.write( l_result < 0 ? 0 : l_result > 1 ? 1 : l_result); motor_rf=1; motor_rb=0; motor_lf=1; motor_lb=0; } case 0x00: { //backward motor_r.write( r_result < 0 ? 0 : r_result > 1 ? 1 : r_result); motor_l.write( l_result < 0 ? 0 : l_result > 1 ? 1 : l_result); motor_rf=0; motor_rb=1; motor_lf=0; motor_lb=1; } case 0x01: { //spin left -- Right forward, left backward motor_r.write( r_result < 0 ? 0 : r_result > 1 ? 1 : r_result); motor_l.write( l_result < 0 ? 0 : l_result > 1 ? 1 : l_result); motor_rf=1; motor_rb=0; motor_lf=0; motor_lb=1; } case 0x10: { //spin right motor_r.write( r_result < 0 ? 0 : r_result > 1 ? 1 : r_result); motor_l.write( l_result < 0 ? 0 : l_result > 1 ? 1 : l_result); motor_rf=0; motor_rb=1; motor_lf=1; motor_lb=0; } } } void change_direction( int sensor_values) //right hand sensors only { //Line_right = !Line_right; switch( sensor_values ) { case 0x00: { //no lines robot is lost!!!! set_direction(0x11, 0.0, 0.0); wait(1); break; } case 0x01: { //to far left turn right 0001 set_direction(0x11, 0.5, 0.0); //this doesn't turn right; this goes straight at half speed wait(1); Line_right = !Line_right; break; } case 0x02: { //left turn right 0010 set_direction(0x11, 0.0, 0.0); wait(1); break; } case 0x03: { //far left turn right 0011 set_direction(0x11, 0.0, 0.0); wait(1); break; } case 0x4: { //right turn left 0100 set_direction(0x11, 0.0, 0.0); wait(1); break; } case 0x5: { //unknown ** 0101 set_direction(0x11, 0.0, 0.0); break; } case 0x6: { //perfect 0110 set_direction(0x11, 0.0, 0.0); wait(1); break; } case 0x7: {//corner junction or plate 0111 set_direction(0x00, 0.0, 0.0); wait(1); break; } case 0x8: {//to far right turn left 1000 set_direction(0x11, 0.0, 0.0); wait(1); break; } case 0x9: { //unknown 1001 set_direction(0x11, 0.0, 0.0); wait(1); break; } case 0xA: { //unknown 1010 set_direction(0x11, 0.0, 0.0); break; } case 0xB: { //unknown 1011 set_direction(0x11, 0.0, 0.0); wait(1); break; } case 0xC: { //far right turn left 1100 set_direction(0x11, 0.0, 0.0); wait(1); break; } case 0xD: { //unknown 1101 set_direction(0x11, 0.0, 0.0); break; } case 0xE: { //corner or plate 1110 set_direction(0x00, 0.0, 0.0); wait(1); break; } case 0xF: { //corner 1111 set_direction(0x11, 0.0, 0.0); wait(1); break; } } } 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) { led = !led; int values; values = sensor_read(); change_direction(values); } }