Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   very basic code help (http://www.chiefdelphi.com/forums/showthread.php?t=78223)

mikelowry 02-09-2009 21:25

very basic code help
 
Team 1771's only programmer last year was a senior, and now he is gone. Myself and one other teammate are trying to learn how to program in time for next season.

We just got windriver installed on our team laptop and are still figuring out how to use it. No one on our team currently knows how to use it.

We both have very basic C++ skills.

We downloaded the basic starter code supplied by FIRST.

We can currently do teleop tank drive with 2 joysticks and an autonomous that goes straight forward without stopping.

Can anyone point me toward some resources for learning about windriver and about controlling basic functions of the robot, or just walk me through?

EHaskins 03-09-2009 00:51

Re: very basic code help
 
The FRC C/C++ Users Guide is a good starting point. It should be included in the windriver install (IIRC, c:\windriver\docs\), otherwise it should be available from http://usfirst.org/roboticsprograms/....aspx?id=14532.

Start there then when you have specific issues/question come back and I'm sure someone here will help.

mikelowry 03-09-2009 12:35

Re: very basic code help
 
Thanks for the help, but now we have a more specific question.

We are experimenting with autonomous and we have this code


Code:

void Autonomous(void)
{
        GetWatchdog().SetEnabled(false);
        myRobot.Drive(0.5,0.5);
}


we dont really know what GetWatchdog is, we know that the sample code has it.

What we have so far makes it drive straight at half speed indefinitely as far as we can tell. We looked at RobotDrive.cpp and .h in the WPILib but we cant figure out how to end the .drive command and move on to the next line.

EHaskins 03-09-2009 13:38

Re: very basic code help
 
NOTE: Some of the function/class/template names have changed since I began working with this, so I may accidentally use a old name.
Quote:

Originally Posted by mikelowry (Post 872752)
Thanks for the help, but now we have a more specific question.

We are experimenting with autonomous and we have this code


Code:

void Autonomous(void)
{
        GetWatchdog().SetEnabled(false);
        myRobot.Drive(0.5,0.5);
}


we dont really know what GetWatchdog is, we know that the sample code has it.

What we have so far makes it drive straight at half speed indefinitely as far as we can tell. We looked at RobotDrive.cpp and .h in the WPILib but we cant figure out how to end the .drive command and move on to the next line.

The watchdog is a safety feature. When enabled, you call Feed(Timeout) where Timeout is the amount of time before the robot will be disabled. If you call Feed again within the timeout period the robot continues normally. If you don't all outputs are disabled.

When you are debugging you will often disable the watchdog.

The myRobot.Drive() function sets a speed, and continues at that speed until told otherwise.

If you are using the Simple robot template you just insert a delay (I believe there is a Wait() function in there somewhere), then call Drive again.

If you are using the Itterative robot template you have a little more work todo. Essentially you would create a state machine using the number of itterations and/or sensor inputs to move between states (forward until hit wall, left turn 90 degree, ect.).

mikelowry 04-09-2009 13:44

Re: very basic code help
 
Thanks for all the help so far. One more question.
We are using the simple robot template, and as far as I can tell, everything is right, but it wont build.

Code:

#include "WPILib.h"

RobotDrive myRobot(1,2);
Joystick leftStick(1);
Joystick rightStick(2);
class RobotDemo : public SimpleRobot
{
        RobotDemo(void)
        {
                GetWatchdog().SetEnabled(false);
        }

        void Autonomous(void)
        {
                GetWatchdog().SetEnabled(false);
                while(1)
                {
                        for(int i = 0; i < 4; i++)
                        {
                                myRobot.TankDrive(0.5,-0.5);
                                Wait(4);
                                myRobot.TankDrive(0.5,0.5);
                                Wait(2);
                        }
                        myRobot.TankDrive(0.0,0.0);
                        Wait(4);
                }
        }
       
        void OperatorControl(void)
        {
                while(1)
                {
                        GetWatchdog().SetEnabled(false);
                        while (IsOperatorControl())
                        {
                                myRobot.TankDrive(leftStick, rightStick);
                                Wait(0.005);
                        }
                }
        }
};
START_ROBOT_CLASS(RobotDemo);

we get the following errors at build:
"RobotDemo::RobotDemo()' is private" in reference to line 7
and
a nonspecific error at the very last line.

Can you tell what I need to do to fix it?

Jared Russell 04-09-2009 14:06

Re: very basic code help
 
Quote:

Originally Posted by mikelowry (Post 872881)
Thanks for all the help so far. One more question.
We are using the simple robot template, and as far as I can tell, everything is right, but it wont build.

Code:

#include "WPILib.h"

RobotDrive myRobot(1,2);
Joystick leftStick(1);
Joystick rightStick(2);
class RobotDemo : public SimpleRobot
{
        RobotDemo(void)
        {
                GetWatchdog().SetEnabled(false);
        }

        void Autonomous(void)
        {
                GetWatchdog().SetEnabled(false);
                while(1)
                {
                        for(int i = 0; i < 4; i++)
                        {
                                myRobot.TankDrive(0.5,-0.5);
                                Wait(4);
                                myRobot.TankDrive(0.5,0.5);
                                Wait(2);
                        }
                        myRobot.TankDrive(0.0,0.0);
                        Wait(4);
                }
        }
       
        void OperatorControl(void)
        {
                while(1)
                {
                        GetWatchdog().SetEnabled(false);
                        while (IsOperatorControl())
                        {
                                myRobot.TankDrive(leftStick, rightStick);
                                Wait(0.005);
                        }
                }
        }
};
START_ROBOT_CLASS(RobotDemo);

we get the following errors at build:
"RobotDemo::RobotDemo()' is private" in reference to line 7
and
a nonspecific error at the very last line.

Can you tell what I need to do to fix it?

Your compiler is complaining because the START_ROBOT_CLASS macro attempts to instantiate an instance of your RobotDemo class. In order to instantiate a class, the class' constructor is called. You have a constructor for your RobotDemo class, but it is declared "private" by default. This means that outside entities (like the START_ROBOT_CLASS macro) cannot call the constructor, hence the error you are seeing. To fix the behavior, your constructor (and all of the other functions required to implement a SimpleRobot, like Autonomous and OperatorControl) need to be declared as "public".

To do this:

Code:

class RobotDemo : public SimpleRobot
{
public:
        RobotDemo(void)
        {
                ...
        };

        ... the rest of your functions here
};
START_ROBOT_CLASS(RobotDemo);

I believe that's the only error that prevents this code from compiling (although without trying to compile it myself I may have missed something).

mikelowry 04-09-2009 15:03

Re: very basic code help
 
Oh ok, thanks. I saw that when i was reading a fragment of last years code that was left on the desktop of the laptop we use, but i didnt think we needed it because it wasn't included in the FIRST supplied code.
I cant test it now because im at home, but im pretty sure it will work.
thanks.

byteit101 04-09-2009 15:07

Re: very basic code help
 
Quote:

Originally Posted by mikelowry (Post 872881)
Thanks for all the help so far. One more question.
We are using the simple robot template, and as far as I can tell, everything is right, but it wont build.

Code:

#include "WPILib.h"

RobotDrive myRobot(1,2);
Joystick leftStick(1);
Joystick rightStick(2);
class RobotDemo : public SimpleRobot
{
        RobotDemo(void)
        {
                GetWatchdog().SetEnabled(false);
        }

        void Autonomous(void)
        {
                GetWatchdog().SetEnabled(false);
                while(1)
                {
                        for(int i = 0; i < 4; i++)
                        {
                                myRobot.TankDrive(0.5,-0.5);
                                Wait(4);
                                myRobot.TankDrive(0.5,0.5);
                                Wait(2);
                        }
                        myRobot.TankDrive(0.0,0.0);
                        Wait(4);
                }
        }
       
        void OperatorControl(void)
        {
                while(1)
                {
                        GetWatchdog().SetEnabled(false);
                        while (IsOperatorControl())
                        {
                                myRobot.TankDrive(leftStick, rightStick);
                                Wait(0.005);
                        }
                }
        }
};
START_ROBOT_CLASS(RobotDemo);

we get the following errors at build:
"RobotDemo::RobotDemo()' is private" in reference to line 7
and
a nonspecific error at the very last line.

Can you tell what I need to do to fix it?

you should also put the Joystics and RobotDrive inside the class (and move the constructors for them, you SHOULD use the watchdog (unless debugging), and you have infinite loops (and auto is longer than 15 seconds, but i am assuming it is not for a real competition):
Code:

#include "WPILib.h"

class RobotDemo : public SimpleRobot
{

RobotDrive myRobot;
Joystick leftStick;
Joystick rightStick;
public:

        RobotDemo():myRobot(1,2), leftStick(1), rightStick(2)//need to put them here
        {
                //GetWatchdog().SetEnabled(false); dont need to do this here
        }

        void Autonomous()
        {
                GetWatchdog().SetEnabled(false);
                //while(1)
                //{

                        for(int i = 0; i < 4; i++)
                        {
                                myRobot.TankDrive(0.5,-0.5);
                                Wait(4);
                                myRobot.TankDrive(0.5,0.5);
                                Wait(2);
                        }
                        myRobot.TankDrive(0.0,0.0);
                        Wait(4);
                //}
        }
       
        void OperatorControl()
        {
                //while(1) and you should use while(true) in C++, while(1) is so C
                //{

                        GetWatchdog().SetEnabled(true);
                        while (IsOperatorControl())
                        {
                                GetWatchdog().Feed();
                                myRobot.TankDrive(leftStick, rightStick);
                                Wait(0.005);
                        }
                //}
        }
};
START_ROBOT_CLASS(RobotDemo);


Jared Russell 04-09-2009 15:15

Re: very basic code help
 
Quote:

Originally Posted by byteit101 (Post 872900)
you should also put the Joystics and RobotDrive inside the class (and move the constructors for them, you SHOULD use the watchdog (unless debugging), and you have infinite loops (and auto is longer than 15 seconds, but i am assuming it is not for a real competition):
Code:

#include "WPILib.h"

class RobotDemo : public SimpleRobot
{

RobotDrive myRobot;
Joystick leftStick;
Joystick rightStick;
public:

        RobotDemo():myRobot(1,2), leftStick(1), rightStick(2)//need to put them here
        {
                //GetWatchdog().SetEnabled(false); dont need to do this here
        }

        void Autonomous()
        {
                GetWatchdog().SetEnabled(false);
                //while(1)
                //{

                        for(int i = 0; i < 4; i++)
                        {
                                myRobot.TankDrive(0.5,-0.5);
                                Wait(4);
                                myRobot.TankDrive(0.5,0.5);
                                Wait(2);
                        }
                        myRobot.TankDrive(0.0,0.0);
                        Wait(4);
                //}
        }
       
        void OperatorControl()
        {
                //while(1) and you should use while(true) in C++, while(1) is so C
                //{

                        GetWatchdog().SetEnabled(true);
                        while (IsOperatorControl())
                        {
                                GetWatchdog().Feed();
                                myRobot.TankDrive(leftStick, rightStick);
                                Wait(0.005);
                        }
                //}
        }
};
START_ROBOT_CLASS(RobotDemo);


Just so nobody gets confused, while these suggestions are certainly reflective of good practice and may reflect the actual coder's intent, they aren't strictly necessary for your code to compile and work (work as in not crash as opposed to exactly what you want it to do).

mikelowry 04-09-2009 17:39

Re: very basic code help
 
Code:

while (IsOperatorControl())
                        {
                                GetWatchdog().Feed();
                                myRobot.TankDrive(leftStick, rightStick);
                                Wait(0.005);
                        }

Is the while(IsOperatorControl()) line really necessary? because all of the code inside it is inside the teleop section of the code anyway.

And what is the difference between putting the RobotDrive and the Joysticks inside the class and moving the constructors as opposed to how i had it?

nighterfighter 04-09-2009 17:43

Re: very basic code help
 
Hey, I am the other programmer working on this.

We have another problem also.

During our tele-op period, it runs our autonomous code!

Any idea as to why this could be?

At first it worked fine, then when we ran it again, it started doing autonomous in teleop, and wouldn't accept any input from us.

I changed the battery and did a power cycle, and that fixed it; once. After I ran it again in teleop, it went back to doing autonomous.

Any idea?

byteit101 06-09-2009 10:25

Re: very basic code help
 
you need to remove the infinite loops, the while(1) like i said above

byteit101 06-09-2009 16:37

Re: very basic code help
 
Quote:

Originally Posted by mikelowry (Post 872919)
Code:

while (IsOperatorControl())
                        {
                                GetWatchdog().Feed();
                                myRobot.TankDrive(leftStick, rightStick);
                                Wait(0.005);
                        }

Is the while(IsOperatorControl()) line really necessary? because all of the code inside it is inside the teleop section of the code anyway.

And what is the difference between putting the RobotDrive and the Joysticks inside the class and moving the constructors as opposed to how i had it?

Yes, but remove the while(1) around that, because if you dont, it will do it once and exit. the function is called and not called again untill it switches state, so if it stays there, then your program stays there

mikelowry 06-09-2009 17:32

Re: very basic code help
 
Ok, thank you.

But what about the constructors for the Joystick and RobotDrive? Why does it make a difference wether or not they are inside the class. It compiles fine either way.

EHaskins 06-09-2009 17:44

Re: very basic code help
 
Quote:

Originally Posted by mikelowry (Post 873147)
Ok, thank you.

But what about the constructors for the Joystick and RobotDrive? Why does it make a difference wether or not they are inside the class. It compiles fine either way.

In general, it is best practice to encapsulate the state of a class (i.e. the joysticks and RobotDrive) within the class. This allows you to create multiple instances of the class safely.

It also makes it easier to debug and modify since if they are contained within the class no other objects have direct access to them.

In this case it shouldn't matter since you should only ever have a single instance of the robot class. However it is still best practice.


All times are GMT -5. The time now is 12:32.

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