issue with encoder ticks in auto -- help!

Hi. I am trying to create some autonomous code with the SampleRobot base class. My goal is to make the robot drive forward a certain number of inches (converted from counts that my left encoder gets using the Get() method) and then stops when the number of counts is greater than my goal.

The issue: the robot never stops, and when I put a printf command in there to print the number of counts that my left encoder’s getting (Get() gets the number of counts, right?) it always prints “489456”. Any help would be greatly appreciated. Thanks!


void Autonomous() {
while (IsAutonomous() && IsEnabled())
{

auto autoSelected = chooser.GetSelected();
// std::string autoSelected = frc::SmartDashboard::GetString("Auto Selector", autoNameDefault);
std::cout << "Auto selected: " << autoSelected << std::endl;

	if (autoSelected == autoNameCustom) 
        {
	// Custom Auto goes here
	std::cout << "Running custom Autonomous" << std::endl;
	drivetrain->SetSafetyEnabled(false);

	leftenc->Reset();
	rightenc->Reset();
	int goal_dist = 10;

	while(1)
	{
		leftenc->Get();
		printf("value of current counts %d 
", (int)leftenc);

		     if(leftenc->Get() < goal_dist/((pi*6)/360))
		     {
	                drivetrain->Drive(-0.4, 0.0);
		     }
		    else
		    {
			drivetrain->Drive(0.0, 0.0);
		     }
		}
	}

In your print statement, you’re trying to use the pointer to the left encoder as an integer, so yes, that will always be the same value. Do leftenc->Get() there, too.

Also, as I understand SampleRobot you must put some way to break the loop, or you’ll never get to teleop.

Hi Parker, did you get this working ?

  • Dennis

Hi,

Still working on it. Thanks a ton for the help, though! It tuns out part of our issue was encoder wiring, so we’re definitely getting closer.

Thanks again!

Looks like you are printing out the pointer to the left encoder object–not the value retrieved by the Get(). From a programming standpoint, your compiler may be saying “Lvalue is missing”–that is the variable you are assigning the Get() to (leftcnts below).

Try these lines:

while(1)
{
	int leftcnts;

	leftcnts = leftenc-&gt;Get();
	printf("value of current counts %d 

", (int)leftcnts);

Good Luck,

Just to restate the warning attached with SampleRobot:

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.

And I’ll pile on that recommendation–since I only commented on the error in my earlier post.

We have used RobotBuilder and the Command-based Robot template for 5+ years. It is a great place to start, and it keeps our team (of typically inexperienced C++ programmers) focused on programming robot functionality and not learning how to set up a nice C++ file structure.

Jeff

I’m not sure that the command based robot is the best thing since sliced bread, but it’s in the running.

Hi everyone,

Thanks for the ideas. The issue turned out to be in the while loop. Once I replaced while(1) in my functions with while((IsAutonomous && IsEnabled)), the Driver Station stopped “crashing” and now the encoders read out the correct values.

Thanks again!