Go to Post Students, mentors, schools, corporations, teams, and communities - they are all critical to the success of FIRST. . - dlavery [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
  #16   Spotlight this post!  
Unread 16-01-2013, 20:11
mrklempae mrklempae is offline
Registered User
no team
 
Join Date: Jan 2013
Location: Montana
Posts: 19
mrklempae is an unknown quantity at this point
Re: Programming ultrasonic rangefinders

I tried using the code you provided, and it compiled. My next problem is printing the distance to the Driver Station in real time to prove the code and/or our sensor works. We copied and pasted the .cpp ad .h file into the BuiltInDefaultCode, then copied your code into the main .cpp file. We made sure the robot was correctly wired. We tried changing the code a little to get data prited, but couldn't print any data (or at least not the right data).
Code:
#include "AnalogRangeFinder.h"
#include <WPILib.h>
#define ANALOG_CHANNEL_FOR_SONAR 1

class Robot2012 : public IterativeRobot
{
   AnalogRangeFinder sonar;
   public:
   Robot2012(void):sonar(ANALOG_CHANNEL_FOR_SONAR)
   {
   }

   void AutonomousPeriodic(void)
   {
   }
	void OperatorControl(void)
	{
		DriverStationLCD *dsLCD = DriverStationLCD::GetInstance();
		float distance = sonar.GetRangeInches();
		while (IsOperatorControl())
		{
			dsLCD->PrintfLine(DriverStationLCD::kUser_Line1, "My Value is: %d", distance);
			dsLCD->UpdateLCD();
			Wait(0.1);
		}}}
}

START_ROBOT_CLASS(Robot2012);
I'm really sorry about this, but I just threw together the code, untested. I don't currently have access to the code we used. Does anyone know how to do this? We tried different things for two hours or so to no avail. We returned data one time (something like 22354698), which I'm pretty sure is WAY out of the sensors range. P.S, ScottW, THANKS! Once we get this part working, that information will be perfect!
Reply With Quote
  #17   Spotlight this post!  
Unread 17-01-2013, 10:33
DjScribbles DjScribbles is offline
Programming Mentor
AKA: Joe S
FRC #2474 (Team Excel)
Team Role: Mentor
 
Join Date: Oct 2011
Rookie Year: 2012
Location: Niles MI
Posts: 284
DjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to behold
Re: Programming ultrasonic rangefinders

In the code you posted here, you only read the distance once, then constantly send it to the driver station, this could cause some odd behavior (assuming the sonar has a bit of turn on transient).

Also, if you are using iterative robot, I would suggest using it's Disabled/Autonomous/Teleop functions, instead of the ones used by simple robot (which may not even work). This code should do what you want to do, and uses IterativeRobot functions:

Code:
#include "WPILib.h"
#include "AnalogRangeFinder.h"


typedef enum
{
    ANALOG_CHANNEL_1_RANGE_FINDER = 1,
    ANALOG_CHANNEL_2_UNUSED,
    ANALOG_CHANNEL_3_UNUSED,
    ANALOG_CHANNEL_4_UNUSED,
    ANALOG_CHANNEL_5_UNUSED,
    ANALOG_CHANNEL_6_UNUSED,
    ANALOG_CHANNEL_7_UNUSED,
    ANALOG_CHANNEL_8_UNUSED,
    ANALOG_CHANNEL_9_UNUSED
} ANALOG_CHANNEL_TYPE;

class Robot2012 : public IterativeRobot
{
    AnalogRangeFinder rangeFinder;

    Timer timeInState;
    Timer timeSinceBoot;

    DriverStation *driverStation;
    DriverStationLCD *driverStationLCD;

    // Local variables to count the number of periodic loops performed
    UINT32 m_autoPeriodicLoops;
    UINT32 m_disabledPeriodicLoops;
    UINT32 m_telePeriodicLoops;

public:

    Robot2012(void):
    rangeFinder(ANALOG_CHANNEL_2_RANGE_FINDER),
    timeInState(),
    timeSinceBoot()
    {
        printf("Robot2012 Constructor Started\n");

        // Acquire the Driver Station object
        driverStation = DriverStation::GetInstance();
        driverStationLCD = DriverStationLCD::GetInstance();

        // Initialize counters to record the number of loops completed in autonomous and teleop modes
        m_autoPeriodicLoops = 0;
        m_disabledPeriodicLoops = 0;
        m_telePeriodicLoops = 0;

        printf("Robot2012 Constructor Completed\n");
    }

    /********************************** Init Routines *************************************/

    void RobotInit(void)
    {
        // Actions which would be performed once (and only once) upon initialization of the
        // robot would be put here.
        timeSinceBoot.Start();
        printf("RobotInit() completed.\n");
    }

    void DisabledInit(void)
    {
        m_autoPeriodicLoops = 0;
        m_telePeriodicLoops = 0;
        m_disabledPeriodicLoops = 0;
        timeInState.Reset();
        timeInState.Start();
    }

    void AutonomousInit(void)
    {
        m_autoPeriodicLoops = 0;
        m_telePeriodicLoops = 0;
        m_disabledPeriodicLoops = 0;
        timeInState.Reset();
        timeInState.Start();
    }

    void TeleopInit(void)
    {
        m_autoPeriodicLoops = 0;
        m_telePeriodicLoops = 0;
        m_disabledPeriodicLoops = 0;
        timeInState.Reset();
        timeInState.Start();
    }

    /********************************** Periodic Routines *************************************/
    //These routines run at a determined rate (several times per second, but not sure how many ms each) as long as periodic and continuous routines don't take longer to execute than expected
    void DisabledPeriodic(void)
    {
        m_disabledPeriodicLoops++;
        driverStationLCD->PrintfLine((DriverStationLCD::Line) 4, "Range to target: %f", rangeFinder.GetRangeInches());
    }

    void AutonomousPeriodic(void)
    {
        m_autoPeriodicLoops++;
        driverStationLCD->PrintfLine((DriverStationLCD::Line) 4, "Range to target: %f", rangeFinder.GetRangeInches());
    }

    void TeleopPeriodic(void)
    {
        // increment the number of teleop periodic loops completed
        m_telePeriodicLoops++;
        driverStationLCD->PrintfLine((DriverStationLCD::Line) 4, "Range to target: %f", rangeFinder.GetRangeInches());
    }

    /********************************** Continuous Routines *************************************/
    //These run more frequently than Periodic loops, and should only contain work that requires frequent execution (and doesn't take long to complete)
    //Uncomment these if needed
//    void DisabledContinuous(void)
//     {
//     }
//
//     void AutonomousContinuous(void)
//     {
//     }
//     void TeleopContinuous(void)
//     {
//     }
};

START_ROBOT_CLASS( Robot2012);
Reply With Quote
  #18   Spotlight this post!  
Unread 17-01-2013, 13:26
ekapalka's Avatar
ekapalka ekapalka is offline
Registered User
FRC #3216
 
Join Date: Dec 2012
Location: Bermuda
Posts: 277
ekapalka has a spectacular aura aboutekapalka has a spectacular aura about
Re: Programming ultrasonic rangefinders

Is there a benefit to using IterativeRobot instead of SimpleRobot? I'm just wondering. We've been writing all of our simple programs using SimpleRobot to demonstrate the various functions of the robot. Thanks for this, by the way! Our primary problem was a bad PWM cable. We'll be testing to see if we can get the new sensor working today.
Reply With Quote
  #19   Spotlight this post!  
Unread 18-01-2013, 20:11
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 592
BradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant future
Re: Programming ultrasonic rangefinders

With the recently posted build of WPILib you should be able to see the AnalogChannel show up on the SmartDashboard in Test mode. From there you'll see the voltages that correspond to distances and it will verify that the sensor is working.

See this page for more information about viewing the Test mode values:

http://wpilib.screenstepslive.com/s/...ode-livewindow

As it shows, you can either explicitly set up the test mode values yourself and get meaningful names for the sensors or you can let WPILib automatically add them. There are examples of each case on that page.

Hope this helps.

Brad
__________________
Brad Miller
Robotics Resource Center
Worcester Polytechnic Institute
Reply With Quote
  #20   Spotlight this post!  
Unread 21-01-2013, 10:25
DjScribbles DjScribbles is offline
Programming Mentor
AKA: Joe S
FRC #2474 (Team Excel)
Team Role: Mentor
 
Join Date: Oct 2011
Rookie Year: 2012
Location: Niles MI
Posts: 284
DjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to behold
Re: Programming ultrasonic rangefinders

Quote:
Originally Posted by ekapalka View Post
Is there a benefit to using IterativeRobot instead of SimpleRobot? I'm just wondering. We've been writing all of our simple programs using SimpleRobot to demonstrate the various functions of the robot. Thanks for this, by the way! Our primary problem was a bad PWM cable. We'll be testing to see if we can get the new sensor working today.
IterativeRobot is beneficial because it splits up the periodic and continuous functions for you, provides an init function, and discourages putting blocking while loops in your Autonomous/Teleop handlers.

It makes it easier to add all of your code to the periodic functions, and if you need to add some time sensitive code, it can be added into continuous without restructuring your code.

By removing while loops (or hiding them in the base class), it helps prevent getting stuck in auton mode and never getting to teleop, and in general it encourages writing non-blocking code.
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 13:39.

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