|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#16
|
|||
|
|||
|
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);
|
|
#17
|
|||
|
|||
|
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);
|
|
#18
|
||||
|
||||
|
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.
|
|
#19
|
|||
|
|||
|
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 |
|
#20
|
|||
|
|||
|
Re: Programming ultrasonic rangefinders
Quote:
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. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|