![]() |
Help with simple program
Hello, I'm definitely not a programmer, but as a mentor to the team, I'm trying to get some simple C++ code working. I started with the sample code, and added a few things to get our robot running. The tank drive code works well, but I wanted to use the joystick triggers to operate the intake rollers on the robot. The idea is to run the bottom two rollers to pick balls up, then run all of the rollers to spew the balls out. Pressing the trigger on the right joystick runns all of the rollers as expected. But for some reason the left joystick trigger won't run the bottom two rollers. I know the joystick is okay, because I tested the code to run all of the rollers with the left trigger, and it worked fine. For some reason it won't run just the bottom two rollers. I have stared at my code for a while, and I don't see what is wrong. Any comments or tips would be much appreciated. See the code below:
#include "WPILib.h" /** * This is a demo program showing the use of the RobotBase class. * The SimpleRobot class is the base of a robot application that will automatically call your * Autonomous and OperatorControl methods at the right time as controlled by the switches on * the driver station or the field controls. */ class MainRobot : public SimpleRobot { RobotDrive myRobot; // robot drive system Joystick leftstick; // left joystick Joystick rightstick; // right joystick Relay intakeroller; Relay feedroller1; // relay for first feed roller Relay feedroller2; // relay for second feed roller Relay feedroller3; // relay for top feed roller Victor topshooter; Victor bottomshooter; Gyro gyro; public: MainRobot(void): // these must be initialized in the same order as above myRobot(1, 2), //left motor on PWM port 1, right motor on port 2 leftstick(2), //USB port 2 rightstick(1), //USB port 1 intakeroller(4, 6, intakeroller.kBothDirections), //relay port 6 feedroller1(4, 8, feedroller1.kBothDirections), //relay port 8 feedroller2(4, 3, feedroller2.kBothDirections), //relay port 3 feedroller3(4, 1, feedroller3.kBothDirections), //relay port 1 topshooter(4), //PWM port 4 bottomshooter(3), //PWM port 3 gyro(1,1) //analog port 1 { myRobot.SetInvertedMotor(myRobot.kRearRightMotor, true); GetWatchdog().SetExpiration(0.1); } /** * Drive left & right motors for 1 second then stop */ void Autonomous(void) { GetWatchdog().SetEnabled(false); myRobot.Drive(-0.5, 0.0); // drive forwards half speed Wait(1.0); // for 1 second myRobot.Drive(0.0, 0.0); // stop robot } /** * Runs the motors with tank steering. */ void OperatorControl(void) { GetWatchdog().SetEnabled(true); while (IsOperatorControl()) { GetWatchdog().Feed(); myRobot.TankDrive(leftstick,rightstick); // drive with tank style if (leftstick.GetTrigger()) { intakeroller.Set(intakeroller.kForward); // pressing button 1L runs pickup rollers feedroller1.Set(feedroller1.kForward); } else { intakeroller.Set(intakeroller.kOff); // releasing button stops rollers feedroller1.Set(feedroller1.kOff); } if (rightstick.GetTrigger()) { intakeroller.Set(intakeroller.kForward); // pressing button 1R runs all rollers feedroller1.Set(feedroller1.kForward); feedroller2.Set(feedroller2.kForward); feedroller3.Set(feedroller3.kForward); bottomshooter.Set(0.75); topshooter.Set(0.75); } else { intakeroller.Set(intakeroller.kOff); // releasing button stops rollers feedroller1.Set(feedroller1.kOff); feedroller2.Set(feedroller2.kOff); feedroller3.Set(feedroller3.kOff); bottomshooter.Set(0.0); topshooter.Set(0.0); } Wait(0.005); // wait for a motor update time } } }; START_ROBOT_CLASS(MainRobot); |
Re: Help with simple program
Your logic has a problem
The right joystick, because it comes second, overrides all your left joystick logic. You need to turn off the bottom rollers only if both left AND right joysticks are off. Along the line of: Code:
if (rightjoystick) { |
Re: Help with simple program
Brilliant, Mark. Thanks so much for your help. It works as expected now!
|
| All times are GMT -5. The time now is 11:59. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi