Not finished. This is my first attemp to make an autonomous vehicle for the Sparkfun Autonomous Competition (https://avc.sparkfun.com/) of June 2015.
Dependencies: FXOS8700CQ SDFileSystem mbed
Fork of AVC_Robot_Controled_Navigation by
- For my autonomous robot I will use a GPS, magnometer, accelerometer, and encoders.
- I control my robot with a remote control to save the GPS points (detect turns) using a xBee radio and save them to a file in a SD card.
Revision 3:bd16e43ad7be, committed 2014-10-17
- Comitter:
- gerardo_carmona
- Date:
- Fri Oct 17 06:21:52 2014 +0000
- Parent:
- 2:94059cb643be
- Child:
- 4:c60636c95b80
- Commit message:
- v1.3
Changed in this revision
--- a/main.cpp Thu Oct 16 17:30:52 2014 +0000 +++ b/main.cpp Fri Oct 17 06:21:52 2014 +0000 @@ -5,12 +5,16 @@ the magnometer. It will report data from the encoders and limit the movement by the ultrasonic sensors + COMPONENTS: * Bluetooth * Magnometer (FXOS8700CQ & magnometer.h) * Motors (motors.h) - * Encoder + * Encoder (encoder.h) * Ultrasonic sensors (motors.h) + * SD Card (sd_card.h & SDFileSystem) + * xbee (xbee.h) + CONTROLS: w: foward @@ -20,16 +24,20 @@ o: 90° left turn p: 90° right turn + Credits: * Mayumi Haro * Sarahí Morán * Gerardo Carmona VERSION: - 1.2 - 10/16/14 + 1.3 - 10/17/14 + - HISTORY: + 1.3 - 10/17/14 + (+) Added nuw functions + 1.2 - 10/16/14 (+) Added xbee, ultrasonic, sd_card @@ -50,31 +58,44 @@ #include "encoder.h" #include "sd_card.h" #include "xbee.h" +#include "encoders.h" +#include "ultrasonic.h" // ----- Constants ------------------------------------------------------------------ #define LED_ERROR 6 #define LED_NORMAL 5 #define LED_BUSY 3 -#define BT_TIME 100 +// Motores +#define MOVE_FWD_F 'W' +#define MOVE_REV_F 'S' +#define MOVE_LEF_F 'A' +#define MOVE_RIG_F 'D' +#define MOVE_FWD_S 'w' +#define MOVE_REV_S 's' +#define MOVE_LEF_S 'a' +#define MOVE_RIG_S 'd' +#define MOVE_STO 'q' +#define MOVE_90L 'o' +#define MOVE_90R 'p' +// Otros +#define reset_encoders 'r' +#define request_data 'm' +#define encoder_rigth 1 +#define encoder_left 2 // ----- I/O Pins ------------------------------------------------------------------- Serial pc(USBTX, USBRX); Serial bt(PTC15, PTC14); -//FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1) // Leds for status BusOut leds(LED_RED, LED_GREEN, LED_BLUE); // Sensors -//AnalogIn current_left(A0); -//AnalogIn current_left(A1); AnalogIn ultra_left(A2); AnalogIn ultra_right(A3); -DigitalIn encoder_left(D3); -DigitalIn encoder_right(D4); +//DigitalIn encoder_left(D3); +//DigitalIn encoder_right(D4); // ----- Others --------------------------------------------------------------------- -//SRAWDATA accel_data; -//SRAWDATA magn_data; Timer bt_timer; Timer gps_timer; @@ -82,35 +103,84 @@ char bt_data; // ----- Function prototypes -------------------------------------------------------- -//double get_mag_x(); -//double get_mag_y(); -//double get_mag_angle(); // Read angle from the compass -char read_bt(); +void read_bt(); +void command_bt(char _command); +void send_all_data(); // ----- Main program --------------------------------------------------------------- int main(){ + init_encoders(); bt.baud(9600); pc.baud(9600); - //fxos.enable(); leds = LED_NORMAL; - bt_timer.start(); while (true){ - if (bt_timer.read_ms() >= BT_TIME){ - bt_data = read_bt(); - if (bt_data != ' '){ - move_motors(bt_data, 100, 100); - } - } + read_bt(); } } // ----- Functions ------------------------------------------------------------------ - -char read_bt(){ +void read_bt(){ char c = ' '; if (bt.readable()){ c = bt.getc(); bt.printf("%c\n\r", c); + //} + //if (c != ' '){ + command_bt(c); } - return c; +} + +void command_bt(char _command){ + switch (_command){ + case MOVE_FWD_F: + motor_fwd(FAST, FAST); + break; + case MOVE_REV_F: + motor_rev(FAST, FAST); + break; + case MOVE_LEF_F: + motor_left(FAST, FAST); + break; + case MOVE_RIG_F: + motor_right(FAST, FAST); + break; + case MOVE_FWD_S: + motor_fwd(SLOW, SLOW); + break; + case MOVE_REV_S: + motor_rev(SLOW, SLOW); + break; + case MOVE_LEF_S: + motor_left(SLOW, SLOW); + break; + case MOVE_RIG_S: + motor_right(SLOW, SLOW); + break; + case MOVE_STO: + motor_stop(); + break; + //case MOVE_90L: + // move_90(1); + // break; + //case MOVE_90R: + // move_90(2); + // break; + case reset_encoders: + encoders_to_zero(); + break; + case request_data: + send_all_data(); + break; + default: + motor_stop(); + // bt.printf("%f\r\n", get_mag_angle()); + break; + } +} + +void send_all_data(){ + bt.printf("sending all data...\n"); + bt.printf("Right encoder: %f cms\tLeft encoder: %f cms\n", encoder(encoder_rigth), encoder(encoder_rigth)); + bt.printf("Rigt distance: %fcms\tLeft distance: %fcms\n", ultrasonicos(ULTRA_R), ultrasonicos(ULTRA_L)); + bt.printf("Angle: %f\n", get_mag_angle()); } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/my_libraries/encoders.cpp Fri Oct 17 06:21:52 2014 +0000 @@ -0,0 +1,46 @@ +// ----- Libraries ------------------------------------------------------------------ +#include "mbed.h" +#include "encoders.h" + +// ----- I/O Pins ------------------------------------------------------------------- +InterruptIn interrupt_encoder_der(D2); +InterruptIn interrupt_encoder_izq(D3); + + +int flanco_bajada_rigth; +int flanco_bajada_left; + +// ----- Functions ------------------------------------------------------------------ +void encoders_to_zero(){ + flanco_bajada_rigth = 0; + flanco_bajada_left = 0; +} + +void falling_encoder_rigth(void){ + flanco_bajada_rigth++; +} + +void falling_encoder_left(void){ + flanco_bajada_left++; +} + +void init_encoders(){ + interrupt_encoder_der.fall(&falling_encoder_rigth); + interrupt_encoder_izq.fall(&falling_encoder_left); +} + +float encoder(int sensor_encoder){ + int read_encoder = 0; + float distancia; + + if(sensor_encoder==encoder_rigth){ + read_encoder= flanco_bajada_rigth; + + }else if(sensor_encoder==encoder_left){ + read_encoder= flanco_bajada_left; + + } + distancia= (read_encoder/5)*2.2580; + + return distancia; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/my_libraries/encoders.h Fri Oct 17 06:21:52 2014 +0000 @@ -0,0 +1,13 @@ +#ifndef encoders_h +#define encoders_h + +#define encoder_rigth 1 +#define encoder_left 2 + +void encoders_to_zero(); +void falling_encoder_right(void); +void falling_encoder_left(void); +float encoder(int sensor_encoder); +void init_encoders(); + +#endif \ No newline at end of file
--- a/my_libraries/motors.cpp Thu Oct 16 17:30:52 2014 +0000 +++ b/my_libraries/motors.cpp Fri Oct 17 06:21:52 2014 +0000 @@ -10,34 +10,6 @@ PwmOut pwm_right(D10); // ----- Functions ------------------------------------------------------------------ -void move_motors(char _move_command, int _power_left, int _power_right){ - switch (_move_command){ - case MOVE_FWD: - motor_fwd(_power_left, _power_right); - break; - case MOVE_REV: - motor_rev(_power_left, _power_right); - break; - case MOVE_LEF: - motor_left(_power_left, _power_right); - break; - case MOVE_RIG: - motor_right(_power_left, _power_right); - break; - case MOVE_STO: - motor_stop(); - break; - //case MOVE_90L: - // move_90(1); - // break; - //case MOVE_90R: - // move_90(2); - // break; - default: - // bt.printf("%f\r\n", get_mag_angle()); - break; - } -} void motor_fwd(int _power_left, int _power_right){ dir_left = 1; @@ -77,4 +49,4 @@ pwm_left = 0; pwm_right = 0; //pc.printf("STOP\n"); -} \ No newline at end of file +}
--- a/my_libraries/motors.h Thu Oct 16 17:30:52 2014 +0000 +++ b/my_libraries/motors.h Fri Oct 17 06:21:52 2014 +0000 @@ -2,16 +2,11 @@ #define motors_h // ----- Constants ------------------------------------------------------------------ -#define MOVE_FWD 'w' -#define MOVE_REV 's' -#define MOVE_LEF 'a' -#define MOVE_RIG 'd' -#define MOVE_STO 'q' -#define MOVE_90L 'o' -#define MOVE_90R 'p' +#define FAST 100 +#define SLOW 50 // ----- Functions ------------------------------------------------------------------ -void move_motors(char _move_command, int _power_left, int _power_right); // Move motor function +//void move_motors(char _move_command); // Move motor function void motor_fwd(int _power_left, int _power_right); void motor_rev(int _power_left, int _power_right); void motor_move_90(int direction); @@ -19,4 +14,4 @@ void motor_right(int _power_left, int _power_right); void motor_stop(); -#endif \ No newline at end of file +#endif
--- a/my_libraries/ultrasonic.cpp Thu Oct 16 17:30:52 2014 +0000 +++ b/my_libraries/ultrasonic.cpp Fri Oct 17 06:21:52 2014 +0000 @@ -3,8 +3,7 @@ #include "ultrasonic.h" // ----- Constants ------------------------------------------------------------------ -#define ULTRA_R 1 -#define ULTRA_L 2 + // ----- I/O Pins ------------------------------------------------------------------- AnalogIn ultra_lef(A2);
--- a/my_libraries/ultrasonic.h Thu Oct 16 17:30:52 2014 +0000 +++ b/my_libraries/ultrasonic.h Fri Oct 17 06:21:52 2014 +0000 @@ -1,6 +1,9 @@ #ifndef ultrasonic_h #define ultrasonic_h +#define ULTRA_R 1 +#define ULTRA_L 2 + float ultrasonicos(int sensor); #endif \ No newline at end of file