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 AVR Competition

  • 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.

Files at this revision

API Documentation at this revision

Comitter:
gerardo_carmona
Date:
Thu Oct 16 01:59:21 2014 +0000
Child:
1:ab09b233da7b
Commit message:
First release.

Changed in this revision

FXOS8700CQ.lib Show annotated file Show diff for this revision Revisions of this file
lib/magnometer.cpp Show annotated file Show diff for this revision Revisions of this file
lib/magnometer.h Show annotated file Show diff for this revision Revisions of this file
lib/motors.cpp Show annotated file Show diff for this revision Revisions of this file
lib/motors.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FXOS8700CQ.lib	Thu Oct 16 01:59:21 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/trm/code/FXOS8700CQ/#e2fe752b881e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/magnometer.cpp	Thu Oct 16 01:59:21 2014 +0000
@@ -0,0 +1,46 @@
+// ----- Libraries ------------------------------------------------------------------
+#include "mbed.h"
+#include "FXOS8700CQ.h"
+
+// ----- I/O Pins -------------------------------------------------------------------
+FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
+
+// ----- Others ---------------------------------------------------------------------
+SRAWDATA accel_data;
+SRAWDATA magn_data;
+
+// ----- Variables ------------------------------------------------------------------
+double mag_x, mag_y;
+double average;
+
+// ----- Functions ------------------------------------------------------------------
+double get_mag_x(){
+    fxos.get_data(&accel_data, &magn_data);
+    return magn_data.x;
+}
+
+double get_mag_y(){
+    fxos.get_data(&accel_data, &magn_data);
+    return magn_data.y;
+}
+
+double get_mag_angle(){
+    double _angle;
+    int16_t _x=0;
+    int16_t _y=0;
+    
+    for (int i = 0; i < 10; i++){
+        fxos.get_data(&accel_data, &magn_data);
+        avg_x += magn_data.x;
+        avg_y += magn_data.y;
+        //pc.printf("X: %d \tY: %d\r\n", magn_data.x, magn_data.y);
+        //pc.printf("%d\n", magn_data.x);
+    }
+    
+    avg_x = avg_x / 10;
+    avg_y = avg_y / 10;
+    
+    pc.printf("%d\n", avg_x);
+    _angle = atan2((double)_x, (double)_y) * 180.0 / 3.14159;
+    return _angle;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/magnometer.h	Thu Oct 16 01:59:21 2014 +0000
@@ -0,0 +1,12 @@
+#ifndef magnometer_h 
+#define magnometer_h
+
+// ----- Constants ------------------------------------------------------------------
+
+
+// ----- Functions ------------------------------------------------------------------
+double get_mag_x();
+double get_mag_y();
+double get_mag_angle(); 
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/motors.cpp	Thu Oct 16 01:59:21 2014 +0000
@@ -0,0 +1,80 @@
+// ----- Libraries ------------------------------------------------------------------
+#include "mbed.h"
+#include "motors.h"
+
+// ----- I/O Pins -------------------------------------------------------------------
+// MOTORS (Control)
+DigitalOut dir_left(D7);
+DigitalOut dir_right(D8);
+PwmOut pwm_left(D9);
+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;
+    dir_right = 1;
+    pwm_left = (float)_power_left/100;
+    pwm_right = (float)_power_right/100;
+    //pc.printf("FWD\n");
+}
+
+void motor_rev(int _power_left, int _power_right){
+    dir_left = 0;
+    dir_right = 0;
+    pwm_left = (float)_power_left/100;
+    pwm_right = (float)_power_right/100;
+    //pc.printf("REV\n");
+}
+
+void motor_left(int _power_left, int _power_right){
+    dir_left = 0;
+    dir_right = 1;
+    pwm_left = (float)_power_left/100;
+    pwm_right = (float)_power_right/100;
+    //pc.printf("LEFT\n");
+}
+
+void motor_right(int _power_left, int _power_right){
+    dir_left = 1;
+    dir_right = 0;
+    pwm_left = (float)_power_left/100;
+    pwm_right = (float)_power_right/100;
+    //pc.printf("RIGHT\n");
+}
+
+void motor_stop(){
+    dir_left = 1;
+    dir_right = 1;
+    pwm_left = 0;
+    pwm_right = 0;
+    //pc.printf("STOP\n");
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/motors.h	Thu Oct 16 01:59:21 2014 +0000
@@ -0,0 +1,22 @@
+#ifndef motors_h 
+#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'
+
+// ----- Functions ------------------------------------------------------------------
+void move_motors(char _move_command, int _power_left, int _power_right);   // 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);
+void motor_left(int _power_left, int _power_right);
+void motor_right(int _power_left, int _power_right);
+void motor_stop();
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Oct 16 01:59:21 2014 +0000
@@ -0,0 +1,108 @@
+/* ==================================================================================
+    --- TEST 1 ---
+    OBJETIVE:
+    Move the motor via bluetooth and program 90° turns to the right and left using 
+    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
+    * Ultrasonics
+    
+    CONTROLS:
+    w: foward
+    s: reverse
+    a: left turn
+    d: right turn
+    o: 90° left turn
+    p: 90° right turn
+    
+    Credits:
+    * Mayumi Haro
+    * Sarahí Morán
+    * Gerardo Carmona
+    
+    VERSION:
+    1.0 - 10/14/14
+
+    HISTORY:
+    1.0 - 10/14/14
+    (+) Initial release.
+    
+    
+================================================================================== */
+
+// ----- Libraries ------------------------------------------------------------------
+#include "mbed.h"
+#include "motors.h"
+#include "magnometer.h"
+
+// ----- Constants ------------------------------------------------------------------
+#define LED_ERROR       6
+#define LED_NORMAL      5
+#define LED_BUSY        3
+#define BT_TIME         100
+
+// ----- 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);
+
+// ----- Others ---------------------------------------------------------------------
+//SRAWDATA accel_data;
+//SRAWDATA magn_data;
+Timer bt_timer;
+Timer gps_timer;
+
+// ----- Variables ------------------------------------------------------------------
+double mag_x, mag_y;
+double average;
+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();
+
+// ----- Main program ---------------------------------------------------------------
+int main(){
+    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);
+            }
+        }
+    }
+}
+
+// ----- Functions ------------------------------------------------------------------
+
+char read_bt(){
+    char c = ' ';
+    if (bt.readable()){
+        c = bt.getc();
+        bt.printf("%c\n\r", c);
+    }
+    return c;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Oct 16 01:59:21 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/552587b429a1
\ No newline at end of file