Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Button programin C++ (http://www.chiefdelphi.com/forums/showthread.php?t=89897)

tomy 23-01-2011 16:25

Button programin C++
 
so my team is using mecanum wheels this year. we would like to make it so when you hit the trigger on the left joystick it reduces the maximum speed of the motor by half. so im my code i have this

Code:

bool GetTrigger (Joystick hand = leftstick);
if im right that checks if the trigger on the left joystick it pushed or not.
i was trying to create an if statement to change the speed of the motors but its not working.

any suggestions?

krudeboy51 23-01-2011 18:33

Re: Button programin C++
 
..

tomy 23-01-2011 18:42

Re: Button programin C++
 
Wouldn't doubling the speed make it go faster unless you puss the trigger?

What I'm looking to do is while in operator control drive mecanum drive full power, but when you get close to the pegs you press the trigger and you drive at half the speed.

Does that make sense?

krudeboy51 23-01-2011 19:16

Re: Button programin C++
 
..

DiscoKittyPrime 23-01-2011 19:22

Re: Button programin C++
 
Try this:

if ( !stick.GetTrigger()){
motors.Set(x);
}
else{
motors.Set(x/2);
}

This code assumes that x is the input variable that you use for speed. For driver control, this would be the value you get from your joysticks. It works by checking to see if the trigger is pressed. If not, then the speed is just as normal. If the trigger is pressed, then the speed is half of what it would normally be. Hope this is what you meant.

tomy 23-01-2011 19:28

Re: Button programin C++
 
That's what I ment thanks :D

tomy 23-01-2011 19:35

Re: Button programin C++
 
would i put that before:

bool GetTrigger....

of after it
cause i just tried to build it and i got a boat load of errors

DiscoKittyPrime 23-01-2011 19:41

Re: Button programin C++
 
If your joystick's name in the program is "hand" then the portion of the code that needs to be the argument for the if statement is as follows: "hang.GetTrigger()".

The code segment would look like

if ( !hand.GetTrigger()){
motors.Set(x);
}
else{
motors.Set(x/2);
}

You don't need a boolean variable for this code, only the value coming directly from the joystick.

tomy 23-01-2011 19:45

Re: Button programin C++
 
it dosent like motors.set(x)

do i have to declare each motor

leftmotor.set(x),rightmotor....

DiscoKittyPrime 23-01-2011 19:52

Re: Button programin C++
 
I am sorry, I forgot to mention that the "motors.Set()" section was just a stand in for whatever functions you use to move. You would either replace the "motors" part with the motors that you declared earlier in your program or replace the "motors.Set()" function with whatever function you were using to drive.

For example, if you declared your motors previously as LeftMotor and RightMotor, then the code would look like:

if ( !stick.GetTrigger()){
LeftMotor.Set(x);
RightMotor.Set(x);
}
else{
LeftMotor.Set(x/2);
RightMotor.Set(x/2);
}

Sorry about forgetting to include that little tidbit. If you have any more problems with your code, post a copy of the driver section so we can see what might be the problem.

tomy 23-01-2011 20:00

Re: Button programin C++
 
i tried to put in left motor and other stuff

here is the code

Code:

#include "WPILib.h"
#include "Vision/AxisCameraParams.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 RobotDemo : public SimpleRobot
{
        RobotDrive myRobot; // robot drive system
        Joystick leftstick; // only joystick
        Joystick rightstick; // only joystick
        HSLImage image;

public:
        RobotDemo(void):
                myRobot(1, 2, 3, 4),        // these must be initialized in the same order
                leftstick(1),                // as they are declared above.
                rightstick(2)                // as they are declared above.
        {
                myRobot.SetExpiration(0.1);
        }

        /**
        * Drive left & right motors for 2 seconds then stop
        */
        void Autonomous(void)
        {
                myRobot.SetSafetyEnabled(false);
                myRobot.Drive(0.5, 0.0);        // drive forwards half speed
                Wait(2.0);                                //    for 2 seconds
                myRobot.Drive(0.0, 0.0);        // stop robot
        }

        /**
        * Runs the motors with arcade steering.
        */
        void OperatorControl(void)
        {
                myRobot.SetSafetyEnabled(true);
                AxisCamera &robocam = AxisCamera::GetInstance();
                robocam.WriteResolution((AxisCamera::Resolution_t)3);
                robocam.WriteBrightness(0);
                Wait(3.0);
               
                while (IsOperatorControl())
                {
                        myRobot.MecanumDrive_Cartesian(leftstick.GetX(), leftstick.GetY(), rightstick.GetY(), 0); //Mecanum drice, float x,float y, float rotation, gyro
                        bool GetTrigger(Joystick hand = leftstick);
                        if (!leftstick.GetTrigger()){
                                motors.Set(x));
                        }
                else{
                        motors.Set(x/2);
                }
                        robocam.GetImage();
                        image.GetImaqImage();
                        Wait(0.005);                                // wait for a motor update time
                }
        }
};

START_ROBOT_CLASS(RobotDemo);


Thanks for the help sorry to be a bothersum and ask so many question this is my first time using c++ :p

basicxman 23-01-2011 20:01

Re: Button programin C++
 
If you want it to toggle, (untested)

Code:

bool hasJoystickBeenClicked(Joystick &joystick, bool &prev, bool &cur) {
  prev = cur;
  cur  = joystick.GetTrigger();

  // Previous joystick value should be true (pressed trigger) and
  // current one should be false (released trigger).
  return (prev && !cur) ? true : false;
}

void getMotorSpeed(float &speed, bool half) {
  return (half) ? speed / 2 : speed;
}

void toggleHalfSpeed(bool &half) {
  half = !half;
}

// ...

if (hasJoystickBeenClicked(joystick, prev, cur))
  toggleHalfSpeed(isHalfSpeed);

// ...

motor.Set(getMotorSpeed(speed, isHalfSpeed));

EDIT: More code.

DiscoKittyPrime 23-01-2011 20:15

Re: Button programin C++
 
void OperatorControl(void)
{
myRobot.SetSafetyEnabled(true);
AxisCamera &robocam = AxisCamera::GetInstance();
robocam.WriteResolution((AxisCamera::Resolution_t) 3);
robocam.WriteBrightness(0);
Wait(3.0);

while (IsOperatorControl())
{
if (!leftstick.GetTrigger()){
myRobot.MecanumDrive_Cartesian(leftstick.GetX(), leftstick.GetY(), rightstick.GetY(), 0);// mecanum drive at full speed
}
else{
myRobot.MecanumDrive_Cartesian(leftstick.GetX()/2, leftstick.GetY()/2, rightstick.GetY()/2, 0);// mecanum drive at full speed
}
robocam.GetImage();
image.GetImaqImage();
Wait(0.005); // wait for a motor update time
}

Try this for your OperatorControl function. This should work for you. And have fun with the mecanum drive, it's alot of fun once you get used to driving it.

jwakeman 23-01-2011 20:17

Re: Button programin C++
 
You just need to replace the motor.set() with myRobot.Drive().

Here is your code with the replacements made. It compiles.


Code:

#include "WPILib.h"
#include "Vision/AxisCameraParams.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 RobotDemo : public SimpleRobot
{
        RobotDrive myRobot; // robot drive system
        Joystick leftstick; // only joystick
        Joystick rightstick; // only joystick
        HSLImage image;

public:
        RobotDemo(void):
                myRobot(1, 2, 3, 4),        // these must be initialized in the same order
                leftstick(1),                // as they are declared above.
                rightstick(2)                // as they are declared above.
        {
                myRobot.SetExpiration(0.1);
        }

        /**
        * Drive left & right motors for 2 seconds then stop
        */
        void Autonomous(void)
        {
                myRobot.SetSafetyEnabled(false);
                myRobot.Drive(0.5, 0.0);        // drive forwards half speed
                Wait(2.0);                                //    for 2 seconds
                myRobot.Drive(0.0, 0.0);        // stop robot
        }

        /**
        * Runs the motors with arcade steering.
        */
        void OperatorControl(void)
        {
                myRobot.SetSafetyEnabled(true);
                AxisCamera &robocam = AxisCamera::GetInstance();
                robocam.WriteResolution((AxisCamera::Resolution_t)3);
                robocam.WriteBrightness(0);
                Wait(3.0);
               
                while (IsOperatorControl())
                {
                        myRobot.MecanumDrive_Cartesian(leftstick.GetX(), leftstick.GetY(), rightstick.GetY(), 0); //Mecanum drice, float x,float y, float rotation, gyro
                        bool GetTrigger(Joystick hand = leftstick);
                        if (!leftstick.GetTrigger()){
                                myRobot.Drive(1.0,0.0);
                        }
                else{
                        myRobot.Drive(0.5,1.0);
                }
                        robocam.GetImage();
                        image.GetImaqImage();
                        Wait(0.005);                                // wait for a motor update time
                }
        }
};

START_ROBOT_CLASS(RobotDemo);


tomy 23-01-2011 20:21

Re: Button programin C++
 
discokittys worked


random question for you guys, i know it is off the topic but...

why isnt my camera sending an image to my driverstation?

jwakeman 23-01-2011 20:25

Re: Button programin C++
 
Oops i put in myRobot.Drive(0.5,1.0); for your half speed...that would make you turn sharply! you need myRobot.Drive(0.5,0.0);

AndrewD 02-03-2012 12:10

Re: Button programin C++
 
For the button, you could just use this:

Code:

if (leftStick.GetRawButton(1) == true) {

}

because the trigger on the joysticks is just button 1. All buttons on the Logitech ATK3's are labeled. That's why FIRST picked them. Also, that little roller thingy is the Z-axis. AKA, Axis 3.

X = 1
Y = 2
Z = 3

for controllers with 2 joysticks (like xbox or 360),

rotX = 4
rotY = 5

MamaSpoldi 02-03-2012 17:39

Re: Button programin C++
 
Quote:

Originally Posted by tomy (Post 1006777)
discokittys worked


random question for you guys, i know it is off the topic but...

why isnt my camera sending an image to my driverstation?

Have you configured the camera using the Camera Configuration Tool that was part of the Utilities Update? See the instruction in this document in the section named "How To Configure Your Camera": http://www.usfirst.org/sites/default...20System_2.pdf


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

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