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-07
Revision:
6:b60a3c005d3c
Parent:
4:b2fe3a2545bf

File content as of revision 6:b60a3c005d3c:


#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);
}

void M3Dpi::setLed(int index, int color)
{
    leds.setColor(index, color);
}

m3dpi::Distance M3Dpi::getDistance()
{
    return distance.getAllDistance();
}

m3dpi::Direction M3Dpi::getDirection()
{
    m3dpi::Direction direction;
    coord c;
    c = compass.getCompass();
    direction.x = c.x;
    direction.y = c.y;
    direction.z = c.z;
    return direction;
}

m3dpi::Rotation M3Dpi::getRotation()
{
    m3dpi::Rotation rotation;
    rotation.x = gyro.getGyroX();
    rotation.y = gyro.getGyroY();
    rotation.z = gyro.getGyroZ();
    return rotation;
}

m3dpi::Acceleration M3Dpi::getAcceleration()
{
    m3dpi::Acceleration acc;
    int values[3] = {0};
    accelerometer.getOutput(values);
    acc.x = (int16_t) values[0];
    acc.y = (int16_t) values[1];
    acc.z = (int16_t) values[2];
    return acc;
}

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

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

time_t M3Dpi::getTime()
{
    return time(NULL);
}