Go to Post Compact gearboxes are a wonderful thing. - Cothron Theiss [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 06-02-2017, 12:19
Pikaton659 Pikaton659 is offline
Registered User
no team
 
Join Date: Jan 2017
Location: United States
Posts: 2
Pikaton659 is an unknown quantity at this point
Trouble With Variables

I should start off by saying I started this season with no experience in programming and thus learned all I know form the FRC website so please try to answer in easy to understand terms if you can. Anyway, I am trying to make a varying shooter for shooting balls and I use the variable x and when I hit the button it doesn't work. I have tried using a basic number value and it does so it has to be an issue with the variable. Here is the code:
#include <WPILib.h>
/**
* This is a demo program showing the use of the RobotDrive class.
* The SampleRobot 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.
*
* WARNING: While it may look like a good choice to use for your code if you're inexperienced,
* don't. Unless you know what you are doing, complex code will be much more difficult under
* this system. Use IterativeRobot or Command-Based instead if you're new.
*/
class Robot: public frc::SampleRobot {
frc::RobotDrive myRobot { 0, 1 }; // robot drive system
frc::Joystick stick { 0 }; // only joystick
public:
Robot() {
myRobot.SetExpiration(0.1);
}

void OperatorControl() {
int x = .50;
Talon *exampleTalon = new Talon(4);
int buttonValue0 = stick.GetRawButton(1);
int buttonValue1 = stick.GetRawButton(2);
int buttonValue2 = stick.GetRawButton(3);
while (IsOperatorControl() && IsEnabled()) {
buttonValue0 = stick.GetRawButton(1);
buttonValue1 = stick.GetRawButton(2);
buttonValue2 = stick.GetRawButton(3);
myRobot.ArcadeDrive(stick); // drive with arcade style (use right stick)
frc::Wait(0.005); // wait for a motor update time
if (x <= 1.0 and buttonValue0 == 1)
{
x = x + 0.005;
//frc::Wait(0.005);
}
if (x >= 0.0 and buttonValue1 == 1)
{
x = x - 0.005;
//frc::Wait(0.005);
}
if(buttonValue2 == 1)
{
exampleTalon->Set(x);
}
}
}
};
START_ROBOT_CLASS(Robot)
Any help would be greatly appreciated.
Reply With Quote
  #2   Spotlight this post!  
Unread 06-02-2017, 12:25
SamCarlberg's Avatar
SamCarlberg SamCarlberg is offline
GRIP, WPILib. 2084 alum
FRC #2084
Team Role: Mentor
 
Join Date: Nov 2015
Rookie Year: 2009
Location: MA
Posts: 164
SamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to behold
Re: Trouble With Variables

If you're new to programming, you should really use RobotBuilder. It'll do all the hard work for you.

Here's the documentation
__________________
WPILib
GRIP, RobotBuilder
Reply With Quote
  #3   Spotlight this post!  
Unread 06-02-2017, 12:56
Pikaton659 Pikaton659 is offline
Registered User
no team
 
Join Date: Jan 2017
Location: United States
Posts: 2
Pikaton659 is an unknown quantity at this point
Re: Trouble With Variables

I tried Robot Builder but it seemed more confusing than the normal one. I also said it weird in my post above; I meant to say that I have some programming experience just never for FRC or this C++.
Reply With Quote
  #4   Spotlight this post!  
Unread 06-02-2017, 23:23
BrianK BrianK is offline
Registered User
FRC #1622
 
Join Date: Oct 2012
Location: San Diego
Posts: 5
BrianK is an unknown quantity at this point
Re: Trouble With Variables

You are attempting to assign floating point values to an integer.
Reply With Quote
  #5   Spotlight this post!  
Unread 07-02-2017, 11:50
euhlmann's Avatar
euhlmann euhlmann is offline
CTO, Programmer
AKA: Erik Uhlmann
FRC #2877 (LigerBots)
Team Role: Leadership
 
Join Date: Dec 2015
Rookie Year: 2015
Location: United States
Posts: 410
euhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud of
Re: Trouble With Variables

Time for a repost:

Quote:
Originally Posted by euhlmann View Post
The number of teams I've been seeing this season on CD who are using C++ but need help with basic WPILib tasks is really frightening.
If you need help [with such tasks], you're going to have a lot of fun with manual memory management, segfaults, the cryptic error messages from GCC...
Please consider switching to Java or LabVIEW
__________________
Creator of SmartDashboard.js, an extensible nodejs/webkit replacement for SmartDashboard


https://ligerbots.org
Reply With Quote
  #6   Spotlight this post!  
Unread Yesterday, 17:58
Pay Me $2's Avatar
Pay Me $2 Pay Me $2 is offline
Registered User
AKA: La-Ya
FRC #4159 (CardinalBotics)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2017
Location: San Francisco, CA
Posts: 4
Pay Me $2 is an unknown quantity at this point
Re: Trouble With Variables

I wouldn't be discouraged if I were you. As BrianK said, you are assigning floating-point values to an integer variable. Since you're new to programming, I'll explain that in more basic terms. A variable that is of type int must be assigned a whole-number value. Therefore, I would suggest changing "int x = 0.5" to "double x = 0.5". Also, "and" doesn't work in C++. Instead of "and", use "&&".

Hope this helps!
Reply With Quote
  #7   Spotlight this post!  
Unread Today, 00:09
bob.wolff68's Avatar
bob.wolff68 bob.wolff68 is offline
Da' Mentor Man
FRC #1967
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2007
Location: United States
Posts: 162
bob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nice
Re: Trouble With Variables

Great advice on the 'double' and the '&&' ... one more note on the 'and' verus '&&'... in C/C++, be sure to use TWO '&&' and not ONE. The language is happy to allow you to use a single '&' but it will not evaluate the logical expression as you desire. It's for a different purpose. Similarly, C can be confusing with the use of TWO '==' versus ONE '='. ONE is for assignment and the other for comparison. It was a major mistake in the language (IMHO) as beginners often accidentally write an if/then expression and use a single '=' which ultimately creates a bug that's hard to find. Sigh.

Be careful - it looks like you're off to a good start, frankly.

bob
__________________
~~~~~~~~~~~~~~~~~~~
Bob Wolff - Software from the old-school
Mentor / C / C++ guy
Team 1967 - The Janksters - San Jose, CA
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 03:01.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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