im trying to create a code that needs to stop a motor from moving and move it backwards for a short time, when a limit switch is pressed, first we are trying to start a motor using a limit switch, but we cant, any help is appreciated.
#include "WPILib.h"
class Robot: public SampleRobot
{
Talon Motor;
DigitalInput limitSwitch;
public:
Robot():
limitSwitch(1),
Motor(6)
{
}
void OperatorControl()
{
while (IsOperatorControl() && IsEnabled())
{
if(limitSwitch.Get()>0)
{
Set.Motor(1.5,0);
}
}
}
};
START_ROBOT_CLASS(Robot);
Firstly, you need to say Motor.Set() instead of Set.Motor(). Also, you are giving the motor a speed of 1.5, when the range of values possible is {-1.0, 1.0}. Finally, a digital input returns a boolean, so you should be checking if the limit switch value is true instead of if it is greater than 0.
#include "WPILib.h"
class Robot: public SampleRobot
{
Talon Motor;
DigitalInput limitSwitch;
public:
Robot():
limitSwitch(1),
Motor(6)
{
}
void OperatorControl()
{
while (IsOperatorControl() && IsEnabled())
{
if(limitSwitch.Get()) {
Motor.Set(1.0);
Wait(0.005);
}
}
}
};
START_ROBOT_CLASS(Robot);
limitSwitch.Get() returns a boolean (true if the limit switch is pressed, false if it isn’t). If you want it to move when the limit switch is pressed, then you want to move when the get method is returning true.
the motors do not stop when the limit switch is pressed, we double checked the cables and all to make sure it isnt the code,any idea of what could be wrong?
Also, check that the switches are working. Depending on your specific setup, a generic way to include a monitor of Switch.get() on your smart dashboard. Then unplug your motors but otherwise run the robot. Then,
Manually engage the limit switch to see if the result gets to the robot
If not, unplug the switch and test for continuity each way…
If so, manually move the actuator so that the limit switch should engage/disengage. Does it?
If so, look for software issues
If not, look for hardware issues - specifically mechanical adjustments so that the switch engages/disengages at the right time.
Are you limit switches wired correctly? the switch needs to be wired from signal too ground. the signal pin is held high until you ground it. so the value that you read will be inverted. you either need to rewire the switch, or put a not in front of the limit switch, such as
Is the Wait() in WPIlib multi-thread friendly? Not being sure, I have advised our programmers against using it, but depending on somewhat more histrionic timeout measures.
It’s multi-thread friendly, but there’s no additional thread here. I think he’s ok to do the Wait (small) at the moment - first things first … an ability to know the switch is working right. I’d suggest not worrying about the motor control yet. I’d say use the smart dashboard to put the value of the limit switch up on the dashboard and see it flitter from 1 to 0 when it is closed and opened.