These are the core files for the Robot at Team conception.

Dependencies:   mbed UniServ

Brobot.cpp

Committer:
obrie829
Date:
2017-06-01
Revision:
11:05d5539141c8
Parent:
8:351b0b7b05b2
Child:
12:9a763d149f61
Child:
13:5c2a7dede65f

File content as of revision 11:05d5539141c8:

/*
 * BROBOT.cpp
 *
 */

#include <cmath>
#include "Brobot.h"

Brobot::Brobot(SpeedControl& speedctrl_, AnalogIn& distance_, DigitalOut& enable_, DigitalOut& bit0_, DigitalOut& bit1_, DigitalOut& bit2_, DigitalOut* led_ptr, Pixy& pixy_) :
    speedctrl(speedctrl_), distance(distance_), enable(enable_), bit0(bit0_), bit1(bit1_), bit2(bit2_), leds(led_ptr), pixy(pixy_) // assigning parameters to class variables
{
    //initialize distance sensors
    for( int ii = 0; ii<6; ++ii)
        sensors[ii].init(&distance, &bit0, &bit1, &bit2, ii);

    //defining the sensors for
    sensor_front.init( &distance, &bit0, &bit1, &bit2, 0);  // & give the address of the object
    sensor_left.init( &distance, &bit0, &bit1, &bit2, 5);
    sensor_right.init( &distance, &bit0, &bit1, &bit2, 1);

    //                      kp        ki       kd      min     max
    //pid.setPIDValues(      0.025f,    0.00005f,  0.001f,   0.5f, -0.5f, 1000);
    pid.setPIDValues(      0.4f,    0.001f,  0.001f,   0.5f, -0.5f, 1000);
    //pixypid.setPIDValues(  0.00001f, 0.0005f, 0.00015f, 0.3f, -0.3f, 1000);

}

void Brobot::avoidObstacleAndMove(int vtrans)
{
    float vrot = 0;   // rpm

    if(sensor_left.read() <0.3f || sensor_right.read() <0.3f) { // to avoid obstacles

        float e = sensor_left.read() - sensor_right.read();
        float diff = pid.calc( e, 0.05f );  // error and period are arguments

        vrot = diff*50; //turn

        if( vrot<= -20)
            vrot=-20;
        else if (vrot > 20)
            vrot=20;
    }

    speedctrl.setDesiredSpeed( vtrans - vrot, -vtrans - vrot );

    if(sensor_front.read() <=0.2f) { // when approaching normal to wall
        speedctrl.setDesiredSpeed( 0, 0 );

        int direction=rand()%2 ;    // so there is variablility in the robots path
        if(direction==0) {
            //stingray.rotate(10);
            speedctrl.setDesiredSpeed(15, 15 );
            wait(0.3);
        } else if (direction==1) {
            //stingray.rotate(-10);
            speedctrl.setDesiredSpeed( -15, -15 );
            wait(0.3);
        }
    }
}

void Brobot::startLeds()
{
    t1.attach( callback(this, &Brobot::ledShow), 0.05f );
}

void Brobot::ledDistance()
{
    for( int ii = 0; ii<6; ++ii)
        sensors[ii]< 0.1f ? leds[ii].write(1) : leds[ii].write(0);   // an if statement in one line
}

void Brobot::ledShow()
{
    static int timer = 0;   // static is only the first time/loop
    // for( int ii = 0; ii<6; ++ii)
    //   leds[ii] = !leds[ii];

    //quit ticker and start led distance show
    if( ++timer > 10) {
        t1.detach();
        t1.attach( callback(this, &Brobot::ledDistance), 0.01f );
    }
}

bool Brobot::foundGreenBrick()
{
    if( pixy.objectDetected() ) {
        if( pixy.getSignature() == 1) {  // 1 is the green brick
            return true;
        }
    }
    return false;
}

bool Brobot::foundHome()
{
    if( pixy.objectDetected() ) {
        if( pixy.getSignature() == 2) {  // 2 is the home
            return true;
        }
    }
    return false;
}

void Brobot::approachBrick()
{
    //insert Crystal's code

    float e = 160-pixy.getX();
    float diff = pixypid.calc( e, 0.005f );
    while( pixy.getY() <= 200) { // in [ixels
        speedctrl.setDesiredSpeed( 8 - diff, 8 - diff) ; // 8 rpms
    }
    // function assumes Brick is in open grips

}

void Brobot::closeGrip()
{
    //insert Paul's code here
    //create Servo type object (ex. servMotor) in Brobot.h
}

void Brobot::openGrip()
{
    //insert adjusted Paul's code here
    //create Servo type object (ex. servMotor) in Brobot.h
}


void Brobot::rotate(int phi)
{
    if(phi>25|| phi<-25) {
        phi=10;
    }
    speedctrl.setDesiredSpeed(phi, phi);
}

void Brobot::forward()
{
    speedctrl.setDesiredSpeed(20, -20);  // rpms
}

void Brobot::turnleft()
{
    speedctrl.setDesiredSpeed(0.48f, 0.31f);   // needs tuning
    wait(0.1);
}

void Brobot::turnright()
{
    speedctrl.setDesiredSpeed(0.69f, 0.52f);  // needs tuning
    wait(0.1);
}

void Brobot::back()
{
    speedctrl.setDesiredSpeed(-15, 15);  //rpms
}

void Brobot::stop()
{
    speedctrl.setDesiredSpeed(0.0f, 0.0f);
}