m3Dpi robot, based on the Pololu 3pi and m3pi. m3Dpi has multiple distance sensors, gyroscope, compass and accelerometer sensor to be fully aware of its environment. With the addition of xbee or nrf24n01 module it has wireless communication capabilities.

Dependencies:   m3Dpi mbed-rtos mbed MbedJSONValue

main.cpp

Committer:
kudrom
Date:
2015-12-17
Revision:
10:a5f43326e0b0
Parent:
9:5292bf545459

File content as of revision 10:a5f43326e0b0:

#include "mbed.h"
#include "rtos.h"
#include "M3Dpi.h"
#include "jsonReporter.h"

const char M3DPI_ID[] = "first";

M3Dpi robot;
Serial pc(USBTX,USBRX);
JsonReporter report(&robot.xbee, M3DPI_ID);

static float r_wheel = 0.0;
static float l_wheel = 0.0;
char direction = 'f';


void setLeds(m3dpi::Distance distance)
{
    int colors[8] = {distance.front, distance.front_right, distance.right, distance.back_right, distance.back, distance.back_left, distance.left, distance.front_left};
    // shift distance value to get red colors
    for(int i = 0; i < 8; i++){
        colors[i] = colors[i] << 16;
    }
    robot.setLeds(colors);
}

void read_sensors_thread(void const * args)
{
    robot.setStatus(Color::GREEN);
    m3dpi::Distance distance = robot.getDistance();
    setLeds(distance);
    
    report.distance(distance);
    report.direction(robot.getDirection());
    report.rotation(robot.getRotation());
    report.acceleration(robot.getAcceleration());
    
    report.time(robot.getTime());
    robot.setStatus(Color::BLACK);
}

void read_commands_thread(void const *args)
{
    while(robot.xbee.readable()){
        char command = robot.xbee.getc();
        switch(command){
            case 'l':
                // Left
                l_wheel = -0.10;
                r_wheel = 0.10;
                break;
            case 'r':
                // Right
                l_wheel = 0.10;
                r_wheel = -0.10;
                break;
            case 'f':
                // Frontward
                l_wheel = 0.1;
                r_wheel = 0.1;
                break;
            case 'b':
                // Backward
                l_wheel = -0.1;
                r_wheel = -0.1;
                break;
            case 's':
                // Brake
                l_wheel = 0;
                r_wheel = 0;
                break;
            default:
                robot.xbee.printf("Unknown command %c.\n", command);
        }
            robot.left_motor(l_wheel);
    robot.right_motor(r_wheel);
    }
    
        // Stop if the robot doesn't receive anything in 100 ms.
        l_wheel = 0;
        r_wheel = 0;
}

int main()
{            
    pc.baud(115200);
    
    RtosTimer readSensorsThread(read_sensors_thread, osTimerPeriodic);
    readSensorsThread.start(1000);

    Thread readCommandsThread(read_commands_thread);
    //readCommandsThread.start(100);
    
    Thread::wait(osWaitForever);
}