Mechatronics Robotics
/
BrobotV1
These are the core files for the Robot at Team conception.
Diff: Brobot.cpp
- Revision:
- 11:05d5539141c8
- Parent:
- 8:351b0b7b05b2
- Child:
- 12:9a763d149f61
- Child:
- 13:5c2a7dede65f
diff -r 351b0b7b05b2 -r 05d5539141c8 Brobot.cpp --- a/Brobot.cpp Mon May 29 13:03:28 2017 +0000 +++ b/Brobot.cpp Thu Jun 01 08:34:27 2017 +0000 @@ -5,69 +5,162 @@ #include <cmath> #include "Brobot.h" -#include "SpeedControl.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); -SpeedControl speedctrl(PwmOut* pwmLeft, PwmOut* pwmRight, EncoderCounter* counterLeft, EncoderCounter* counterRight); + // 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 -Brobot::Brobot(SpeedControl* speedctrl, int number) + 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() { - this->number = number; - //this->speedctrl = speedctrl; + 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::rotate( float phi) +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(phi>0.5f || phi<-0.5f) { - phi=0; + if( pixy.objectDetected() ) { + if( pixy.getSignature() == 1) { // 1 is the green brick + return true; + } } + return false; +} - *pwmL = 0.5f + phi; - *pwmR = 0.5f + phi; +bool Brobot::foundHome() +{ + if( pixy.objectDetected() ) { + if( pixy.getSignature() == 2) { // 2 is the home + return true; + } + } + return false; } -/* -void Brobot::setWheelSpeeds( float wheelL, float wheelR) +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() { - SpeedCtrl.setDesiredSpeed( wheelL, wheelR); + //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() { - //*pwmL=0.65f; // asterisk is dereferencing the pointer so - //*pwmR=0.36f; // you can access the variable at the pointers address - // also another way to dereference the pointer is: pwmR->write(0.xx) - speedctrl->setDesiredSpeed(0.65f, 0.35f); -} - -void Brobot::slow(float scale) -{ - if(scale>0.5f || scale<-0.5f) { - scale=0; - } - - *pwmL = *pwmL - scale; - *pwmR = *pwmR + scale; + speedctrl.setDesiredSpeed(20, -20); // rpms } void Brobot::turnleft() { - speedctrl->setDesiredSpeed(0.48f, 0.31f); + speedctrl.setDesiredSpeed(0.48f, 0.31f); // needs tuning wait(0.1); } void Brobot::turnright() { - speedctrl->setDesiredSpeed(0.69f, 0.52f); + speedctrl.setDesiredSpeed(0.69f, 0.52f); // needs tuning wait(0.1); } void Brobot::back() { - speedctrl->setDesiredSpeed(0.65f, 0.35f); + speedctrl.setDesiredSpeed(-15, 15); //rpms } void Brobot::stop() { - speedctrl->setDesiredSpeed(0.5f, 0.5f); + speedctrl.setDesiredSpeed(0.0f, 0.0f); } \ No newline at end of file