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-16
Revision:
9:5292bf545459
Parent:
8:5c0833506d67
Child:
10:a5f43326e0b0

File content as of revision 9:5292bf545459:

#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)
{
    if(robot.xbee.readable()){
        char command = robot.xbee.getc();
        switch(command){
            case 'l':
                // Left
                if(direction == 'f'){
                    l_wheel += 0.1;
                }else{
                    l_wheel -= 0.1;
                }
                break;
            case 'r':
                // Right
                if(direction == 'f'){
                    r_wheel += 0.1;
                }else{
                    r_wheel -= 0.1;
                }
                break;
            case 'f':
                // Frontward
                r_wheel += 0.1;
                l_wheel += 0.1;
                direction = 'f';
                break;
            case 'b':
                // Backward
                r_wheel -= 0.1;
                l_wheel -= 0.1;
                direction = 'b';
                break;
            case 's':
                // Brake
                r_wheel = 0;
                l_wheel = 0;
                direction = 'f';
                break;
            default:
                pc.printf("Unknown command %c.\n", command);
        }
        
        robot.left_motor(l_wheel);
        robot.right_motor(r_wheel);
    }
}

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

    RtosTimer readCommandsThread(read_commands_thread, osTimerPeriodic);
    readCommandsThread.start(50);
    
    Thread::wait(osWaitForever);
}