Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Programming ultrasonic rangefinders (http://www.chiefdelphi.com/forums/showthread.php?t=110988)

mrklempae 16-01-2013 20:11

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!

DjScribbles 17-01-2013 10:33

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);


ekapalka 17-01-2013 13:26

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.

BradAMiller 18-01-2013 20:11

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

DjScribbles 21-01-2013 10:25

Re: Programming ultrasonic rangefinders
 
Quote:

Originally Posted by ekapalka (Post 1217258)
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.


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

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