library for 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:   m3pi ADXL345_I2C HMC5583L ITG3200 PCA9547 TLC59116 VL6180x RGB-fun xbee

Dependents:   m3Dpi-helloworld

M3Dpi.cpp

Committer:
sillevl
Date:
2015-12-03
Revision:
0:9f02ae958e20
Child:
4:b2fe3a2545bf

File content as of revision 0:9f02ae958e20:


#include "M3Dpi.h"

M3Dpi::M3Dpi() :
    status(STATUS_RED,STATUS_GREEN,STATUS_BLUE),
    compass(SDA, SCL, 0x3D),
    gyro(SDA, SCL),
    accelerometer(SDA,SCL),
    leds(SDA, SCL),
    distance(SDA, SCL),
    xbee(XBEE_TX, XBEE_RX, XBEE_RESET)
{
    xbee.baud(XBEE_BAUD);
    xbee.reset();
    
    gyro.setLpBandwidth(LPFBW_42HZ);

    //Go into standby mode to configure the device.
    accelerometer.setPowerControl(0x00);
    wait(.001);
    //Full resolution, +/-16g, 4mg/LSB.
    accelerometer.setDataFormatControl(0x0B);
    wait(.001);
    //3.2kHz data rate.
    accelerometer.setDataRate(ADXL345_3200HZ);
    wait(.001);
    //Measurement mode.
    accelerometer.setPowerControl(0x08);
    wait(.001);
}

void M3Dpi::setStatus(Color* color)
{
    status.setColor(color);
}

void M3Dpi::setStatus(int color)
{
    status.setColor(color);
}


void M3Dpi::setLeds(int* colors)
{
    leds.setAll(colors);
}

int* M3Dpi::getDistance(int data[])
{
    return distance.getAllDistance(data);;
}

int* M3Dpi::getDirection(int data[])
{
    coord c;
    c = compass.getCompass();
    data[0] = c.x;
    data[1] = c.y;
    data[2] = c.z;
    return data;
}

int* M3Dpi::getRotation(int data[])
{
    data[0] = gyro.getGyroX();
    data[1] = gyro.getGyroY();
    data[2] = gyro.getGyroZ();
    return data;
}

int* M3Dpi::getAcceleration(int data[])
{
    int values[3] = {0};
    accelerometer.getOutput(values);
    data[0] = (int16_t) values[0];
    data[1] = (int16_t) values[1];
    data[2] = (int16_t) values[2];
    return data;
}

int M3Dpi::_putc(int c)
{
    return xbee.putc(c);
}

int M3Dpi::_getc()
{
    return xbee.getc();
}

char* M3Dpi::getTime(char buffer[])
{
    time_t seconds = time(NULL);
    strftime(buffer, 32, "%d-%m-%Y %T", localtime(&seconds));
    return buffer;
}