Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   roboRIO Command Based Problems (http://www.chiefdelphi.com/forums/showthread.php?t=133698)

kylelanman 04-02-2015 00:51

Re: roboRIO Command Based Problems
 
Code:

48void DriveBase::AutoDriveTurn(float speed, float angle){
 49        while(drive_gyro->GetAngle() >= angle){
 50                fl_motor->Set(speed);
 51                fr_motor->Set(speed);
 52                bl_motor->Set(speed);
 53                br_motor->Set(speed);
 54        }
 55 }

This function is going to cause you problems. This is called a "tight loop" and will consume all of your CPU power while blocking your main thread. Because there is no delay the CPU is going to execute the loop as fast as it possibly can. It will likely take the CPU to 100% and then start dropping control packets from the driver station. On top of that while you are in the loop none of your other commands or code in the main thread will be running.

The better way to do this that won't cause you problems is to use a command.

Code:

32 class AutoDriveTurnCommand() {
 33 private:
 34    double mSpeed;
 35    double mAngle;
 36 public:
 37    AutoDriveTurnCommand(double speed, double angle) {
 38        mSpeed = speed;
 39        mAngle = angle;
 40    }
 41    void Initialize() {
 42        Robot::drivebase->DriveTank(speed, speed);
 43    }
 44    void Execute() {}
 45    bool IsFinished() {
 46        return Robot::drivebase->GetGyroAngle() < mAngle;
 47    }
 48    void End() {
 49        Robot::drivebase->DriveTank(0, 0);
 50    }
 51    void Interrupted() {
 52        End();
 53    }
 54 };

For this to work you will need to define a function in DriveBase called GetGyroAngle that returns the angle of the gyro.

King Nerd III 04-02-2015 00:57

Re: roboRIO Command Based Problems
 
Quote:

Originally Posted by kylelanman (Post 1438064)
Code:

48void DriveBase::AutoDriveTurn(float speed, float angle){
 49        while(drive_gyro->GetAngle() >= angle){
 50                fl_motor->Set(speed);
 51                fr_motor->Set(speed);
 52                bl_motor->Set(speed);
 53                br_motor->Set(speed);
 54        }
 55 }

This function is going to cause you problems. This is called a "tight loop" and will consume all of your CPU power while blocking your main thread. Because there is no delay the CPU is going to execute the loop as fast as it possibly can. It will likely take the CPU to 100% and then start dropping control packets from the driver station. On top of that while you are in the loop none of your other commands or code in the main thread will be running.

The better way to do this that won't cause you problems is to use a command.

Code:

32 class AutoDriveTurnCommand() {
 33 private:
 34    double mSpeed;
 35    double mAngle;
 36 public:
 37    AutoDriveTurnCommand(double speed, double angle) {
 38        mSpeed = speed;
 39        mAngle = angle;
 40    }
 41    void Initialize() {
 42        Robot::drivebase->DriveTank(speed, speed);
 43    }
 44    void Execute() {}
 45    bool IsFinished() {
 46        return Robot::drivebase->GetGyroAngle() < mAngle;
 47    }
 48    void End() {
 49        Robot::drivebase->DriveTank(0, 0);
 50    }
 51    void Interrupted() {
 52        End();
 53    }
 54 };

For this to work you will need to define a function in DriveBase called GetGyroAngle that returns the angle of the gyro.

Thanks for the tip, but that couldn't be what's causing the error with not updating fast enough. That method isn't even called in the code yet, plus ita autonomous and we only get the error in teleop. Thanks for the tip, though! I'll probably use that!

King Nerd III 10-12-2015 15:22

Re: roboRIO Command Based Problems
 
I know this is an old thread, and I'm replying to myself, but yesterday we figured out the problem. In the Robot.cpp we were calling auto_command->End(); in the TeleOpInit Phase, but we had not given a specific command to auto_command, so when TeleOp was enabled it couldn't end the command and just died... At least that's how I believe we fixed it. Commenting out the auto_command->End() got rid of this problem, so we believe that's what it was.
Hope this helps!
Isaac


All times are GMT -5. The time now is 14:05.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi