Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Limit switch help (http://www.chiefdelphi.com/forums/showthread.php?t=134646)

PredaFran 02-15-2015 03:58 PM

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);


nandeeka 02-15-2015 06:17 PM

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.

PredaFran 02-15-2015 06:27 PM

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);


nandeeka 02-15-2015 06:33 PM

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);

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.

PredaFran 02-15-2015 06:50 PM

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?

nandeeka 02-15-2015 09:42 PM

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);
If you are trying to start the motor when the limit switch is pressed, use this line of code:

Code:

Motor.Set(0.5);

GeeTwo 02-16-2015 12:24 AM

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,
  1. Manually engage the limit switch to see if the result gets to the robot
  2. If not, unplug the switch and test for continuity each way...
  3. 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.

teslalab2 02-17-2015 11:09 PM

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);
}


GeeTwo 02-18-2015 01:29 AM

Re: Limit switch help
 
Quote:

Originally Posted by teslalab2 (Post 1446130)
Code:

If(!limitswitch.Get())
{
motor.Set(.5);
Wait(sometime);
motor.Set(0);
}


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.

bob.wolff68 02-18-2015 02:13 AM

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());
...



All times are GMT -5. The time now is 10:10 AM.

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