|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Limit switch help
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.
Code:
#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);
|
|
#2
|
||||
|
||||
|
Re: Limit switch help
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.
Last edited by nandeeka : 15-02-2015 at 18:19. |
|
#3
|
|||
|
|||
|
Re: Limit switch help
can you give me a hand with the bool?
Code:
#include "WPILib.h"
class Robot: public SampleRobot
{
Talon Motor;
DigitalInput limitSwitch;
bool limitValue
public:
Robot():
limitSwitch(1),
Motor(6)
{
}
void OperatorControl()
{
while (IsOperatorControl() && IsEnabled())
{
if(limitSwitch.Get()=1)
{
Motor.Set(1.0);
Wait(0.005);
}
}
}
};
START_ROBOT_CLASS(Robot);
|
|
#4
|
||||
|
||||
|
Re: Limit switch help
Try something like this:
Code:
#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);
|
|
#5
|
|||
|
|||
|
Re: Limit switch help
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?
|
|
#6
|
||||
|
||||
|
Re: Limit switch help
If you are trying to stop the motor when the limit switch is pressed, use this line of code:
Code:
Motor.Set(0.0); Code:
Motor.Set(0.5); |
|
#7
|
|||||
|
|||||
|
Re: Limit switch help
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,
Last edited by GeeTwo : 16-02-2015 at 00:25. Reason: nested lists |
|
#8
|
||||
|
||||
|
Re: Limit switch help
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
Code:
If(!limitswitch.Get())
{
motor.Set(.5);
Wait(sometime);
motor.Set(0);
}
|
|
#9
|
|||||
|
|||||
|
Re: Limit switch help
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.
|
|
#10
|
||||
|
||||
|
Re: Limit switch help
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.
Code:
SmartDashboard::PutNumber("Switch1", limitswitch.Get());
...
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|