Home
Programming Guide - FIRST Robotics Resource Center
Contents
1. if FindColor IMAQ HSL greenHue amp greenSat amp greenLum amp par amp amp par particleToImagePercent lt MAX PARTICLE TO IMAGE PERCENT amp amp par particleToImagePercent gt MIN PARTICLE TO IMAGE PERCENT myRobot gt Drive 1 0 else myRobot gt Drive 0 0 Wait 0 05 myRobot gt Drive 0 0 0 0 Example 4 two color tracking float par center_mass_ x normalized 0 0 An example of tracking a two color target 1s in the demo project TwoColorTrackDemo The file Target cpp in this project provides an API for searching for this type of target The example below first creates tracking data Ctt sprintf tdl name td1 tal tdl tal tdl tal sprintf td2 name td2 td2 ta2 td2 td2 td2 PINK hue minValue 220 hue maxValue 255 saturation minValue 75 saturation maxValue 255 luminance minValue 85 luminance maxValue 255 GREEN hue minValue 55 hue maxValue 125 saturation minValue 58 saturation maxValue 255 luminance minValue 92 luminance maxValue 255 Call FindTwoColors with the two sets of tracking data and an orientation ABOVE BELOW RIGHT LEFT to obtain two ParticleAnalysisReports which have details of a two color target Note The FindTwoColors API code is in the demo project not in the WPILib project 39 Other Classes Ctt find a two color target if FindTwoColors td1 t
2. m_compressor Start In Java you would accomplish the same thing as follows Solenoid Pneumatics The Solenoid object controls the outputs of the NI 9472 Digital Output Module It is designed to apply an input voltage to any of the 8 outputs Each output can provide up to 1A of current The module is designed to operate 12v pneumatic solenoids used on FIRST robots This makes the use of relays unnecessary for pneumatic solenoids Note The NI 9472 Digital Output Module does not provide enough current to operate a motor or the compressor so relays connected to Digital Sidecar digital outputs will still be required for those applications The port numbers on the Solenoid class range from 1 8 as printed on the pneumatics breakout board Note The NI 9472 indicator lights are numbered 0 7 for the 8 ports which is different numbering than used by the 35 Vision Image Processing Solenoid Pneumatics class or the pneumatic bumper case silkscreening Example Setting the output values of the Solenoid objects to true or false will turn the outputs on and off re spectively The following code fragment will create 8 Solenoid objects initialize each to true on and then turn them off one per second Then it turns them each back on one per second and deletes the objects Cr Solenoid s 8 for int i Se Be EY ew Solenoid i 1 allocate the Solenoid objects 4 i lt Be Itt s i gt Set true turn them all on
3. Wait 1 0 or int 0 1 lt 05 14 s i gt Set false turn them each off in turn Wait 1 0 for int i 0 i lt 8 i s i gt Set true turn them back on in turn Wait 1 0 delete s i delete the objects You can observe the operation of the Solenoid class by looking at the indicator lights on the 9472 module Vision Image Processing Access to National Instrument s nivison library for machine vision enables automated image process ing for color identification tracking and analysis The VisionAPI cpp file provides open source C wrappers to a subset of the proprietary library The full specification for the simplified FRC Vision programming interface is in the FRC Vision API Specification document which is in the WindRwer docs extensions FRC directory of the Wind River installation with this document The FRC Vision in terface also includes high level calls for color tracking TrackingAPI cpp Programmers may also call directly into the low level library by including nivision h and using calls documented in the NI Vision for LabWindows CVI User Manual Naming conventions for the vision processing wrappers are slightly different from the rest of WPILib C routines prefixed with imaq belong to N s LabVIEW CVI vision library Routines prefixed with fre are simplified interfaces to the vision library provided by BAE Systems for FIRST Robot ics Competition use Sample programs provided in
4. Wait 2 0 Drive 0 0 0 0 y void OperatorControl void SetWatchdogEnabled true while IsOperatorControl WatchdogFeed ArcadeDrive JOYSTICK PORT START ROBOT CLASS SimpleCRobot Choosing between C and Java Language differences Choosing between C and Java C and Java are very similar languages in fact Java has its roots in C when it was designed If you looked at a C or Java program from a distance it would be hard to tell them apart You ll find that if you can write a WPILib C program for your robot then you can probably also write a Java program Language differences There is a good detailed list of the differences between the two languages on Wikipedia available org wiki Comparison_of Java_and C The following is a summary of those differences as they wil most likely effect robot programs created with WPILib e C memory is allocated and freed manually that is the programmer is required to allocate objects and delete them In Java objects are allocated the same way through the use of the new operator but it is freed automatically when there are no more references to them This greatly simplifies memory management for the programmer e Java does not have pointers only references to objects All objects must be allocated with the new operator and are always referenced using the dot operator for example gyro getAngle In C there are pointers references a
5. Counter objects are extremely flexible elements that can count input from either a digital input signal or an analog trigger They can also operate in a number of modes based on the type of input signal some of which are used to implement other sensors in the WPI Robotics Library e Gear tooth mode enables up down counting based on the width of an input pulse This is used to implement the GearTooth object with direction sensing e Semi period mode counts the period of a portion of the input signal This is used to measure the time of flight of the echo pulse in an ultrasonic sensor e Normal mode can count edges of a signal in either up counting or down counting directions based on the input selected Encoders Encoders are devices for measuring the rotation of a spinning shaft Encoders are typically used to measure the distance a wheel has turned that can be translated into robot distance across the floor Distance moved over a measured period of time represents the speed of the robot and is another common measurement for encoders There are several types of encoders supported in WPILi 16 Encoders Geartooth Sensor single output encoders that provide a state change as the whee Counter class is turned With a single output there is no way of detecting the direction of rotation The Innovation First VEX encoder and the index outputs of a quadrature encoder are examples of this type Ol Ce C Quadrature en Quadrature
6. ers was implemented in complex real time software and caused a number of problems as system complexity increased On the cRIO the FPGA implements all the high speed measurements through dedicated hardware ensuring accurate measurements no matter how many sensors and motors are added to the robot In addition there are many features in the WPI Robotics Library that make it easy to implement many other types of sensors not directly supported with classes For example general purpose coun ters can measure period and count from any device generating output pulses Another example is a generalized interrupt facility to catch high speed events without polling and potentially missing them Digital I O Subsystem Diy Sid Car The NI 9401 digital I O module has 32 GPIO lines Through the circuits in the digital breakout board these map into 10 PWM outputs 8 Relay outputs for driving Spike relays the signal light an 12C port and 14 bidirectional GPIO lines The basic update rate of the PWM lines is a multiple of approximately 5 ms Jaguar speed con trollers update at slightly over 5ms Victors update 2X slightly over 10ms and servos update at 4X slightly over 20ms Digital Inputs Digital inputs are typically used for switches The DigitalInput object is typically used to get the cur rent state of the corresponding hardware line 0 or 1 More complex uses of digital inputs such as en coders or counters are handled by using the appropriat
7. and GetRawAxis methods GetAxis takes an Axis Type kX Axis kYAxis kZAxis kTwistAxis or kThrottleAxis and returns that axis s value GetRawAxis takes an a number 1 6 and returns the value of the axis associated with that number these numbers are reconfigurable and generally used with custom control systems since the other two methods reliably return the same data for a given axis There are three ways to access the top button defaulted to button 2 and trigger button 1 The first is to use their respective accessor methods GetTop and Get Trigger which return a true or false value based on whether the button 1s currently being pressed A second method is to call Get Button which takes a Button Type which can be either kTopButton or kTriggerButton The last method is one that allows access to the state of every button on the joystick GetRawButton This method takes a number corresponding to a button on the joystick see diagram below and return the state of that button In addition to the standard method of accessing the Cartesian coordinates x and y axes of the joystick s position WPILib also has the ability to return the position of the joystick as a magnitude 32 Controlling Pneumatics Other Driver Station features and direction To access the magnitude the GetMagnitude method can be called and to access the direction either GetDirectionDegrees or GetDirectionRadians can be call
8. Using the Gyro class The Gyro object is typically created in the constructor of the RobotBase derived object When the Gyro object is instantiated it will go through a calibration period to measure the offset of the rate output while the robot is at rest This requires that the robot be stationary while this is happening and that the gyro is unusable until after the calibration has completed Once initialized the GetAngle getAngle in Java method of the Gyro object will return the number of degrees of rotation heading as a positive or negative number relative to the robot s position during the calibration period The zero heading can be reset at any time by calling the Reset reset in Java method on the Gyro object Setting the gyro sensitivity The Gyro class defaults to the settings required for the 80 sec gyro that was delivered by FIRST in the 2008 kit of parts To change gyro types call the SetSensitivity float sensitivity setSensitivity double sensitivity in Java method and pass it the sensitivity in volts sec Just be careful since the units are typically specified in mV volts 1000 in the spec sheets A sensitivity of 12 5 mV sec would require a SetSensitivity setSensitivity in Java parameter value of 0 0125 11 Gyro Setting the gyro sensitivity Cr Example This program causes the robot to drive in a straight line using the gyro sensor in combination with the RobotDrive class The RobotDriv
9. each channel The WPI Robotics Library will allow the sample rate to be changed once for a module Changing it to a different value will result in a runtime error being generated The use of some sensors currently just the Gyro will set the sample rate to a specific value for the module it is connected to 21 Analog Inputs Analog Triggers Summary e There is one sample rate per module e The number of oversampled and averaged values is expressed as a power of 2 e The delivered sample rate is reduced by the oversample and average values e There are 2 accumulators connected to analog channels and 2 of the first Analog Module This means that only two devices such as gyros that use the accumulators can be connected to the cRIO and they must be connected to channel 1 or 2 of Analog Module 1 e The returned analog value is 2n times larger than the actual value where n is the number of oversampled bits Averaging doesn t change the returned values except to average them Analog Triggers Anebo In x ae y pa 3 32 A 1 4 th Window a 36 2 rr 3 Rising Pale 1 Felling Pale AO 3 Point Average Reject Filter 22 Camera Camera task management Avg Reject Filter 3PY simile o med ran Filter outpnt pom on bits n 9 Camera Note the camera and image processing classes will be changing for 2010 The version documented here corresponds to the older 2009 library The Java library is upg
10. how the encoder is mounted relative to the shaft being measured The k4X makes sure that an encoder module from the FPGA is used and 4X accuracy is obtained To get the 4X value you should use the GetRaw method on the encoder The Get method will always return the normalized value by dividing the actual count obtained by the 1X 2X or 4X multiplier Analog Inputs The NI 9201 Analog to Digital module has a number of features not available on simpler controllers It will automatically sample the analog channels in a round robin fashion providing an aggregate sample rate of 500 ks s 500 000 samples second These channels can be optionally oversampled and averaged to provide the value that is used by the program There are raw integer and floating point voltage outputs available in addition to the averaged values The averaged value is computed by summing a specified number of samples and performing a simple average that is dividing by the number of samples that are in the average When the system averages a number of samples the division results in a fractional part of the answer that is lost in producing the integer valued result That fraction represents how close the average values were to the next higher integer Oversampling is a technique where extra samples are summed but not di vided down to produce the average Suppose the system were oversampling by 16 times that would mean that the values returned were actually 16 times the av
11. the Spike relay that is controlling the power to the compres sor A digital output or Solenoid module port alone doesn t supply enough current to operator the compressor e The Digital input port connected to the pressure switch that is monitoring the accumulator pres sure The Compressor class will automatically create a task that runs in the background twice a second and turns the compressor on or off based on the pressure switch value If the system pressure is above the high set point the compressor turns off If the pressure is below the low set point the com pressor turns on To use the Compressor class create an instance of the Compressor object and Start it This is typically done in the constructor for your Robot Program Once started 1t will continue to run on its own with no further programming necessary If you do have an application where the compres sor should be turned off possibly during some particular phase of the game play you can stop and restart the compressor using the Stop and Start methods The compressor class will create instances of the DigitalInput and Relay objects internally to read the pressure switch and operate the Spike relay For example suppose you had a compressor and a Spike relay connected to Relay port 2 and the pressure switch connected to digital input port 4 Both of these ports are connected to the primary digital input module You could create and start the compressor running
12. to set the position of a turret to a posi tion related to the x axis on a joystick using a single motor on a Jaguar and a potentiometer for angle feedback As the joystick X value changes the motor should drive to a position related to that new value The PIDController class will ensure that the motion is smooth and stops at the right point A potentiometer that turns with the turret will provide feedback of the turret angle The potentiom eter 1s connected to an analog input and will return values ranging from 0 5V from full clockwise to full counterclockwise motion of the turret The joystick X axis returns values from 1 0 to 1 0 for full left to full right We need to scale the joystick values to match the 0 5V values from the potentiom eter This can be done with the following expression turretStick GetX 1 0 2 5 The scaled value can then be used to change the setpoint of the control loop as the joystick is moved The 0 1 0 001 and 0 0 values are the Proportional Integral and Differential coefficients respec tively The AnalogChannel object is already a subclass of PIDSource and returns the voltage as the control value and the Jaguar object is a subclass of PIDO utput Ctt Joystick turretStick 1 Jaguar turretMotor 1 AnalogChannel turretPot 1 PIDController turretControl 0 1 0 001 0 0 amp turretPot amp turretMotor turretControl Enable start calculating PIDOutput val while IsOperator turr
13. use the Ultrasonic class Instead use the appropriate class for the sensor for example an AnalogChannel object for an ultrasonic sen sor that returns the range as a voltage The following two examples read the range on an ultrasonic sensor connected to the output port ULTRASONIC_PING and the input port ULTRASONIC_ECHO Ctt Ultrasonic ultra ULTRASONIC_PING ULTRASONIC_ECHO ultra SetAutomaticMode true int range ultra GetRangelnches C Example 4 Getting the range from an Ultrasonic rangefinder instance Ultrasonic ultra new Ultrasonic ULTRASONIC_ PING SONIC ECHO ultra setAutomaticMode true int range ultra getRangeInches Java Example 3 Getting the range from an Ultrasonic rangefinder instance Counter Subsystem The counters subsystem represent a very complete set of digital signal measurement tools for in terfacing with many sensors that you can put on the robot There are several parts to the counter subsystem 15 Encoders Counter Objects Py ae Pride X S A gt Bes Figure 3 Schematic of the possible sources and counters in the Counter Subsystem in the cRIO Counters can be triggered by either Analog Triggers or Digital Inputs The trigger source can either control up down counters Counter objects quadrature encoders Encoder objects or interrupt generation Analog triggers count each time an analog signal goes outside or inside of a set range of voltages Counter Objects
14. 2 channel bool Write a digital output value on port channel value bool GetDigitalOut UINT32 channel Read the currently set digital output value on port channel Note The driver station does not have pull up or pull down resistors on any of the digital inputs This means that un connected inputs will have a random value You must use external pull up or pull down resistors on digital inputs to get repeatable results Other Driver Station features The Driver Station is constantly communicating with the Field Management System FMS and pro vides additional status information through that connection bool IsAutonomous Field state autonomous vs teleop UINT32 GetPacketNum Sequence number of the current driver station received data ber packet Alliance GetAlliance Alliance red blue for the match UINT32 GetLocation Starting field position of the robot 1 2 or 3 float GetBattery Voltage Battery voltage on the robot Joysticks 31 Joysticks Other Driver Station features The standard input device supported by the WPI Robotics Library is a USB joystick The 2009 kit joystick comes equipped with eleven digital input buttons and three analog axes and interfaces with the robot through the Joystick class The Joystick class itself supports five analog and twelve digital inputs which allows for joysticks with more axis control or buttons The joystick must be connected to one of the four available USB ports
15. C C and Java WPILib Programming Guide for the FIRST Robotics Competition er Polytechnic Institute Robotic FIRST Brad Miller Ken Streeter Beth Finn Jerry Morrison Dan Jones Ryan O Meara Derek White Stephanie Hoag Rev 1 0 November 2009 Contents Contents What is the WPI Robotics Libra y iscuscacsletivredaranrarieretnesnnnsivets ice vnestenrsienetesetns 4 Choosing between C and Java ssesessseseessessessssseseeseeseeseeseeseeseeseeseese 7 Ao ELO 6 APOCO E RPP E E EE E a E S E E NE 9 Digital I O A II 10 Digital INPUTS sess dinn unio ii 11 Digital OUTPUTS aia 12 ACC CLEFOMGUET PREU ccnp vnedeiedvewulwabadunisxnus A 13 GVO ocx te ceuoustaea ease E ener rannexeunanewnereiea O E 14 Aa A nEn EERENS ERE KEE EEE 17 Ultrasonic NANSEHNGE iodo ale 18 Counter SUDSYSTEM cui ia beens 20 2 o oe eae ee PT ae 21 A O A A 22 Geartooth SENSON ate rai 23 Quadrature Encoders iii aa 24 Analog INPUES ria a AA AA A da 26 A a A EL O A 28 CIMA RR 29 C ntrolling MOTOTS cerro coa 33 PWM OPPOCO CO A 34 VICLOM ita ico 35 A A A 36 PP O o maMeee emonarse Red REReeeeNeseen al RobotDrivVe comicidad riadas 38 Getting Feedback from the Drivers Station sssssssessssssscsssessscsssecssseseeees 40 JOSE ici nudwenevunheteunecelaceeieaveiuud tedkehes 42 Controlling PREUMATICS 1 0vccecnnsieudanenaxearar netsiaanewen seetatadeadanetencersunanpenenauensa 44 COMPRES a aa 45 Solenold PHEUMALICS steed cicintscsaedideadbhverwedletu
16. atively GetImageBlocking will wait until a new image is available if the current one has already been served To prevent excessive blocking time the call will return unsuccessfully if a new image is not available in 0 5 second Cr Image cameralmage frcCreateImage IMAQ IMAGE HSL double timestamp timestamp of image returned double lastImageTimestamp timestamp of last image to ensure image is new int success GetImageBlocking cameraImage amp timestamp lastImageTimestamp Camera Metrics Various camera instrumentation counters used internally may be accessed that may be useful for camera performance analysis and error detection Here is a list of the metrics CAM_STARTS CAM_STOPS CAM_NUM_IMAGE CAM_BUFFERS_ WRITTEN CAM_ BLOCKING_COUNT CAM_SOCKET_OPEN CAM_SOCKET_INIT_ATTEMPTS CAM_ BLOCKING_TIMEOUT CAM_GETIMAGE_SUCCESS CAM_GETIMAGE_FAILURE CAM_STALE_IMAGE CAM_GETIMAGE_BEFORE_INIT CAM_GETIMAGE_BEFORE_ AVAILABLE CAM_READ_JPEG_FAILURE CAM_PC_SOCKET_OPEN CAM_PC_SEND IMGAGE_SUCCESS CAM_PC_SENDIMAGE_FATLURE CAM_PID_SIGNAL_ERR CAM_ BAD_IMAGE_SIZE CAM_HEADER_ERROR The following example call gets the number of images served by the camera Ctt int result GetCameraMetric CAM_NUM_IMAGE Images to PC The class PCVideoServer when instantiated creates a separate task that sends images to the PC for display on a dashboard application The sample program DashboardDemo shows an example of use 26 Con
17. ble Kp 0 03 public GyroSample 1 getWatchdog setExpiration 0 1 protected void Autonomous Gyro reset while isAutonomous getWatchdog feed double angle gyro getAngle get heading myRobot drive 1 0 angle Kp turn correct heading Timer delay 0 004 myRobot drive 0 0 0 0 stop robot y Java Example 1 Driving in a straight line using a gyro The angle is multiplied by Kp to scale it for the speed of the robot drive This factor is called the pro portional constant or loop gain Increasing Kp will cause the robot to correct more quickly but too high and it will oscillate Decreasing the value will cause the robot correct more slowly maybe never getting back to the desired heading This is proportional control HiTechnicCompass Use of the compass is somewhat tricky since 1t uses the earth s magnetic field to determine the head ing The field is relatively weak and the compass can easily develop errors from other stronger mag netic fields from the motors and electronics on your robot If you do decide to use a compass be sure to locate it far away from interfering electronics and verify the readings on different headings Each digital I O module has only one 2C port to connect a sensor to For example let s create a compass on the 12C port of the digital module plugged into slot 4 13 Ultrasonic rangefinder Setting the gyro sensitivity Cr HiTechnicCompass com
18. channel pulses once for each complete revolution of the encoder shaft If counting the index channel is required for the application it can be done by connecting that channel to a simple Counter object which has no direction information Quadrature encoders are handled by the Encoder class Using a quadrature encoder is done by sim ply connecting the A and B channels to two digital I O ports and assigning them in the constructor for Encoder There are four QuadratureEncoder modules in the FPGA and 8 Counter modules that can operate as quadrature encoders One of the differences between the encoder and counter hardware is that encoders can give an oversampled 4X count using all 4 edges of the input signal Counters can either return a 1X or 2X result based on one of the input signals If 1X or 2X is chosen in the Encoder constructor a Counter module is used with lower oversampling and if 4X default is chosen then one of the four encoders is used Ctt Encoder encoder 1 2 true k4X C Example 5 Creating an encoder on ports 1 and 2 with reverse sensing and 4X encoding Encoder encoder encoder new Encoder 1 2 true EncodingType k4X Java Example 4 Creating an encoder on ports 1 and 2 with reverse sensing and 4X encoding 19 Analog Inputs Quadrature Encoders Where 1 and 2 are the port numbers for the two digital inputs and true tells the encoder to not invert the counting direction The sensed direction could depend on
19. clude SimpleTracker which in autonomous mode tracks a color and drives toward it VisionServoDemo which also tracks a color with a two servo gimbal VisionDemo demonstrates other capabilities including storing a JPEG image to the cRIO and DashboardDemo sends images to the PC Dashboard application 36 Vision Image Processing Color Tracking Image files may be read and written to the cRIO non volatile memory File types supported are PNG JPEG JPEG2000 TIFF AIDB and BMP Images may also be obtained from the Axis 206 camera Using the FRC Vision API images may be copied cropped or scaled larger smaller In tensity measurements functions available include calculating a histogram for color or intensity and obtaining values by pixel Contrast may be improved by equalizing the image Specific color planes may be extracted Thresholding and filtering based on color and intensity characteristics are used to separate particles that meet specified criteria These particles may then be analyzed to find the characteristics Color Tracking High level calls provide color tracking capability without having to call directly into the image pro cessing routines You can either specify a hue range and light setting or pick specific ranges for hue saturation and luminance for target detection Example 1 using defaults Call GetTrackingData with a color and type of lighting to obtain default ranges that can be used in the call to FindColor Th
20. d2 ABOVE amp parl amp par Average the two particle centers to get center X amp y of combined target horizontalDestination parl center_mass_x normalized par2 center mass x_ normalized 2 verticalDestination parl center mass y normalized par2 center mass y normal ized 2 To obtain the center of the combined target average the x and y values As before use the normal ized values to work within a range of 1 0 to 1 0 Use the center_mass_x and center_mass_y values if the exact pixel position is desired Several parameters for adjusting the search criteria are provided in the Target h header file again provided in the TwoColorTrackDemo project not WPILib The initial settings for all of these paramters are very open to maximize target recognition Depending on your test results you may want to adjust these but remember that the lighting conditions at the event may give different results These parameters include FRC_MINIMUM_PIXELS_FOR_TARGET default 5 Make this larger to prevent extra processing of very small targets FRC_ALIGNMENT_SCALE default 3 0 scaling factor to determine alignment To ensure one target is exactly above the other use a smaller number However light shining directly on the target causes significant variation so this parameter is best left fairly high FRC_MAX_IMAGE_SEPARATION default 20 Number of pixels that can exist separating the two colors Best number varies with i
21. ds that can help with driving the robot either from the Driver Station controls or through programmed operatio Drive speed turn Designed to take speed and turn values ranging from 1 0 to 1 0 The speed values set the robot overall drive speed positive values forward and negative values backwards The turn value tries to specify constant radius turns for any drive speed The negative values represent left turns and the positive values represent right turns TankDrive leftStick right Takes two joysticks and controls the robot with tank steer Stick ing using the y axis of each joystick There are also meth ods that allow you to specify which axis is used from each stick ArcadeDrive stick Takes a joystick and controls the robot with arcade single stick steering using the y axis of the joystick for forward backward speed and the x axis of the joystick for turns There are also other methods that allow you to specify dif ferent joystick axes HolonomicDrive magnitude Takes floating point values the first two are a direction direction rotation vector the robot should drive in The third parameter rotation is the independent rate of rotation while the robot is driving This is intended for robots with 4 Mecanum wheels independently controlled SetLeftRightMotorSpeeds left Takes two values for the left and right motor speeds As Speed rightSpeed with all the other methods this will control the motors as defined by the co
22. e Drive method takes the speed and the turn rate as arguments where both vary from 1 0 to 1 0 The gyro returns a value that varies either positive or negative degrees as the robot deviates from its initial heading As long as the robot continues to go straight the heading will be zero If the robot were to turn from the O degree heading the gyro would indicate this with either a positive or negative value This example uses the gyro to turn the robot back on course using the turn parameter of the Drive method class GyroSample public SimpleRobot RobotDrive myRobot robot drive system Gyro gyro static const float Kp 0 03 public GyroSample myRobot 1 2 sensors in initialization list gyro 1 GetWatchdog SetExpiration 0 1 void Autonomous gyro Reset while IsAutonomous GetWatchdog Feed float angle gyro GetAngle get heading myRobot Drive 1 0 angle Kp turn to correct heading Wait 0 004 myRobot Drive 0 0 0 0 stop robot y C Example 1 Driving in a straight line using a gyro package edu wpi first wpilibj templates 12 HiTechnicCompass Setting the gyro sensitivity Java import edu wpi first wpilibj Gyro import edu wpi first wpilibj RobotDrive import edu wpi first wpilibj SimpleRobot import edu wpi first wpilibj Timer public class GyroSample extends SimpleRobot private RobotDrive myRobot robot drive system private Gyro gyro dou
23. e ParticleAnalysisReport returned by FindColor specifies details of the largest particle of the targeted color Ctt TrackingThreshold tdata GetTrackingData BLUE FLUORESCENT ParticleAnalysisReport par if FindColor IMAQ HSL amp tdata hue amp tdata saturation amp tdata luminance amp par printf color found at x i y 1 par center_mass_x_normalized par center mass y normal printf color as percent of image d par particleToImagePercent The normalized center of mass of the target color is a range from 1 0 to 1 0 regardless of image size This value may be used to drive the robot toward a target Example 2 using specified ranges To manage your own values for the color and light ranges you simply create Range objects 37 Vision Image Processing Example 3 using return values Ctt Range hue sat lum hue minValue 140 Hue hue maxValue 155 sat minValue 100 Saturation sat maxValue 255 lum minValue 40 Luminance lum maxValue 255 FindColor IMAQ HSL amp hue amp sat amp lum amp par Tracking also works using the Red Green Blue RGB color space however HSL gives more consis tent results for a given target Example 3 using return values Here is an example program that enables the robot to drive towards a green target When it is too close or too far the robot stops driving Steering like this is quite simple as shown in the example The foll
24. e classes Using these other supported device types encoder ultrasonic rangefinder gear tooth sensor etc doesn t require a digital input object to be created The digital input lines are shared from the 14 GPIO lines on each Digital Breakout Board Creating an instance of a DigitalInput object will automatically set the direction of the line to input Digital Outputs Types of supported sensors Digital input lines have pull up resistors so an unconnected input will naturally be high If a switch is connected to the digital input it should connect to ground when closed The open state of the switch will be 1 and the closed state will be 0 In Java digital input values are true and false So an open switch is true and a closed switch is false Digital Outputs Digital outputs are typically used to run indicators or interface with other electronics The digital outputs share the 14 GPIO lines on each Digital Breakout Board Creating an instance of a Digital Output object will automatically set the direction of the GPIO line to output In C digital output values are O and 1 representing high 5V and low Ov signals In Java the digital output values are true 5V and false Ov Accelerometer The accelerometer typically provided in the kit of parts is a two axis accelerometer It can provide acceleration data in the X and Y axes relative to the circuit board In the WPI Robotics Library you treat it as two devices one for the X axi
25. e routed to functions in your code They are dispatched at task level and not as kernel interrupt handlers This is to help reduce many of the real time bugs that have been at the root of so many issues in our programs in the past We believe this works because of the extensive FPGA hardware support We have chosen to not use the C exception handling mechanism although it is available to teams for their programs Our reasoning has been that uncaught exceptions will unwind the entire call stack and cause the whole robot program to quit That didn t seem like a good idea in a finals match in the Championship when some bad value causes the entire robot to stop The objects that represent each of the sensors are dynamically allocated We have no way of know ing how many encoders motors or other things a team might put on a robot For the hardware an internal reservation system is used so that people don t accidentally reuse the same ports for different purposes although there is a way around it if that was what you meant to do There are also classes to assist in use of the camera and with image processing that is often used in FRC games The classes wrap lower level functions that are part of the NI Vision library that is also used in the LabVIEW implementation of WPILib We can t say that our library represents the only right way to implement FRC robot programs There are a lot of smart people on teams with lots of experience doing rob
26. ear or sprocket teeth and detect whenever a tooth passes The gear tooth sensor is a Hall effect device that uses a magnet and solid state device that can measure changes in the field caused by the passing teeth 17 Encoders Quadrature Encoders Figure 5 A gear tooth sensor mounted on a VEX robot chassis measuring a metal gear rotation Notice that there is a metal gear attached to the plastic gear in this picture The gear tooth sensor needs a ferrous material passing by it to detect rotation Example Quadrature Encoders Background Information Encoders are devices for measuring the rotation of a spinning shaft Encoders are typically used to measure the distance a wheel has turned that can be translated into robot distance across the floor Distance moved over a measured period of time represents the speed of the robot and is another common measurement for encoders Encoders typically have a rotating disk with slots that spins in front of a photodetector As the slots pass the detector pulses are generated on the output The rate at which the slots pass the detector indicates the rotational speed of the shaft and the number of slots that have passed the detector indi cates the number of rotations or distance 18 Encoders Quadrature Encoders Figure 6 A Grayhill quadrature optical encoder Note the two connectors one for the A channel and one for the B chan nel Some quadrature encoders have an extra index channel This
27. ed Joystick driveJoy 1 Jaguar leftControl 1 Jaguar rightControl 2 if driveJoy GetTrigger 1f the trigger is pressed Have the left motor get input from Y axis and the right motor get input from X axis leftControl Set driveJoy GetY rightControl Set driveJoy GetAxis kXAxis else if driveJoy GetRawButton 2 1f button number 2 pressed top Have both right and left motors get input from the throttle axis leftControl Set driveJoy GetThrottle rightControl Set driveJoy GetAxis kThrottleAxis I button number 4 is pressed else if driveJoy GetRawButton 4 1f button number 4 is pressed Have the left motor get input from the magnitude of the joystick s position leftControl Set driveJoy GetMagnitude Throttle 9 Controlling Pneumatics 33 Controlling Pneumatics Compressor These classes make it easier to use pneumatics in your robot Solenoid Can control pneumatic actuators directly without the need for an additional relay In the past a Spike relay was required along with a digital output port to control a pneumatics component Compressor Keeps the pneumatics system charged by using a pressure switch and software to turn the compressor on and off as needed Compressor The Compressor class is designed to operate the FRC supplied compressor on the robot A Compres sor object is constructed with 2 input output ports e The Digital output port connected to
28. eld EE AEREE EREEREER eves 47 Vision Image PROCESSING corri il ici 48 Color Tracking siii di ia 49 What is the WPI Robotics Library o A o uo A O 53 PIERO AMM rra 54 ROLAYS PROP Co0 Mian talninavidiied uaa tab EEE EEEE 56 Using the serial POF scciisctarsisuvectctddecemnsassinntsiaaseapenededaaesieseeeetiertaadendndietes 58 USING 2G AP PP tingi rittt t tennt s EAT EAEE AT EEEE EEDE ETE EEE TEREE E eee 59 System Architecture ic sreernereere ran REEE EA EEE E EE EE 60 Digital SOU CSSanssnaces cin cacamanes EE EEEE O 61 Contributing to the WPI Robotics Library orina oe 62 GIOSSARY it A A EER EEE EEE a EEEE 63 What is the WPI Robotics Library What is the WPI Robotics Library The WPI Robotics library is a set of classes that interfaces to the hardware in the FRC control sys tem and your robot There are classes to handle sensors motors the driver station and a number of other utility functions like timing and field management The library is designed to e Deal with all the low level interfacing to these components so you can concentrate on solving this year s robot problem This is a philosophical decision to let you focus on the higher level design of your robot rather than deal with the details of the processor and the operating system e Understand everything at all levels by making the full source code of the library available You can study and modify the algorithms used by the gyro class for oversampling and integrati
29. encoders coders A channel and the B channel The B channel is out of phase from Encoder class the A channel By measuring the relationship between the two inputs the software can determine the direction of rotation The system looks for Rising Edge signals ones where the input is transitioning from 0 to 1 and falling edge signals When a rising edge is detected on the A channel the B channel determines the direction If the encoder was turning clockwise the B channel would be a low value and if the encoder was turning counter clockwise then the B channel would be a high value The direc tion of rotation determines which rising edge of the A channel is detected the left edge or the right edge The Quadrature encoder class can look at all edges and give an oversampled output with 4x This is a device supplied by FIRST as part of the FRC robot stan GearTooth class dard kit of parts The gear tooth sensor is designed to monitor the rotation of a sprocket or gear that is part of a drive system It uses a Hall effect device to sense the teeth of the sprocket as they move he se Table 1 Encoder types that are supported by WPILib These types of encoders are described in the following sections Channel A Channel B Code trackon disk Channel A l Channel B 90 Figure 4 Quadrature encoder phase relationships between the two channels Example Geartooth Sensor Gear tooth sensors are designed to be mounted adjacent to spinning ferrous g
30. erage Using the oversampled value gives additional precision in the returned value 20 Analog Inputs Quadrature Encoders Over Sample z Average Engine ca ie a 2 0 AvgiL Mm 2 Ss AQ RAY N over Sampl bits M average bits To set the number of oversampled and averaged values use the methods Ctt void SetAverageBits UINT32 bits UINT32 GetAverageBits void SetOversampleBits UINT32 bits UINT32 GetOversampleBits C Example 6 Methods used for setting up oversampling and averaging void setAverageBits int bits UINT32 getAverageBits void setOversampleBits UINT32 bits UINT32 getOversampleBits Java Example 5 Methods used for setting up oversampling and averaging The number of averaged and oversampled values are always powers of 2 number of bits of overs ampling averaging Therefore the number of oversampled or averaged values is 2bits where bits is passed to the methods SetOversampleBits bits and SetAverageBits bits The actual rate that values are produced from the analog input channel is reduced by the number of averaged and oversampled values For example setting the number of oversampled bits to 4 and the average bits to 2 would reduce the number of delivered samples by 24 2 or 64 The sample rate is fixed per analog I O module so all the channels on a given module must sample at the same rate However the averaging and oversampling rates can be changed for
31. es to read On subsequent calls each lineNumber is requested to get one camera parameter There should be one property value entry on each line i e exposure auto A sample camera Config txt file is included with the CameraDemo project This file must be placed on the root direc tory of the cRIO Below is an example file Cr HEEE HE HE HEEE TEHE HE H EEE H EH lines starting with or or comments this a sample configuration file only sensor properties may be set using this file set appearance properties when StartCameraTask is called 005 EHH exposure auto colorlevel 99 24 Camera Simple Camera initialization Simple Camera initialization StartCamera Task initializes the camera to serve MJPEG images using the following camera appear ance defaults e Frame Rate 10 frames sec e Compression 0 e Resolution 160x120 e Rotation 0 Cr if StartCameraTask 1 dprintf LOG ERROR Failed to spawn camera task Error code s GetErrorText GetLastError Configurable Camera initialization Image processing places a load on the cRIO which may or may not interfere with your other robot code Depending on needed speed and accuracy of image processing it is useful to configure the camera for performance The highest frame rates may acquire images faster than your processing code can process them especially with higher resolution images If your camera mount is upside down
32. etControl SetSetpoint turretStick GetX 1 0 EZ Wait 02 wait for new joystick values The PIDController object will automatically in the background e Read the PIDSource object in this case the turretPot analog input e Compute the new result value e Set the PIDO utput object in this case the turretMotor This will be repeated periodically in the background by the PIDController The default repeat rate is 50ms although this can be changed by adding a parameter with the time to the end of the PIDCon troller argument list See the reference document for details Relays The cRIO provides the connections necessary to wire IFI spikes via the relay outputs on the digi tal sidecar The sidecar provides a total of sixteen outputs eight forward and eight reverse The 41 Other Classes Using the serial port forward output signal is sent over the pin farthest from the edge of the sidecar which is labeled as output A while the reverse signal output is sent over the center pin which is labeled output B The final pin is a ground connection When a Relay object is created in WPILib its constructor takes a channel and direction or a slot channel and direction The slot is the slot number that the digital module is plugged into the digital module being what the digital sidecar is connected to on the cRIO this parameter is not needed if only the first digital module is being used The channel is the number of the connecti
33. in the constructor of your RobotBase derived object using the following 2 lines of code Ctt Compressor c new Compressor 4 2 c gt Start Note The variable c is a pointer to a compressor object and the object is allocated using the new operator If it were allocated as a local variable in the constructor at the end of the constructor function its local variables would be deallocated and the compressor would stop operating That s all that is required to enable the compressor to operate for the duration of the robot program C Object Life Span You need the Compressor object to last the entire game If you allocate 1t with new the best practice is to store the pointer in a member variable then delete it in the Robot s destructor 34 Controlling Pneumatics Solenoid Pneumatics Ctt class RobotDemo public SimpleRobot Compressor m compressor public RobotDemo m compressor new Compressor 4 2 m_compressor gt Start RobotDemo delete m_compressor Alternatively declare it as a member object then initialize and Start it in the Robot s constructor In this case you need to use the constructor s initialization list to initialize the Compressor object The C compiler will quietly give RobotDemo a destructor that deletes the Compressor object Crt class RobotDemo public SimpleRobot Compressor m_compressor public RobotDemo m_compressor 4 2
34. ject If you need to be sure code runs when a class if no longer referenced you must explicitly call a method that cleans up e Java Native Interface JNI is not supported This is a method of calling C programs from Java using a standard technique The JVM does support a similar way of calling to C code called JNA There is more information about JNA in a later section of this document e Serialization and Remote Method Invocation RMI are not supported e The user interface APIs Swing and AWT are not implemented e Threads are supported by thread groups are not e Furthermore since Java ME is based on an earlier version of Java SE 1 3 it doesn t include newer features such as Gernerics Annotations and Autoboxing Sensors The WPI Robotics Library includes built in support for all the sensors that are supplied in the FRC kit of parts as well as many other commonly used sensors available to FIRST teams through indus trial and hobby robotics outlets Types of supported sensors The library natively supports sensors of a number of categories shown below Wheel motor posi Gear tooth sensors encoders analog encoders and potentiometers tion measurement Robot orientation Compass gyro accelerometer ultrasonic rangefinder Generic pulse output Counters Our version of Java Digital I O Subsystem Types of supported sensors In the past high speed counting of pulses from encoders or accurate timing of ultrasonic rangefind
35. mage resolution It would normally be very low but is set to a higher number to allow for glare or incomplete recognition of the color FRC_SIZE_FACTOR default 3 Size difference between the two particles With this setting one particle can be three times the size of the other FRC_MAX_HITS default 10 Number of particles of each color to analyze Normally the tar get would be found in the first largest particles Reduce this to increase performance Increase it to maximize the chance of detecting a target on the other side of the field FRC_COLOR_TO_IMAGE_PERCENT default 0 001 One color particle must be at least this percent of the image Other Classes PID Programming PID controllers are a powerful and widely used implementation of closed loop control The PID Controller class allows for a PID control loop to be created easily and runs the control loop in a sepa rate thread at consistent intervals The PIDController automatically checks a PIDSource for feed back and writes to a PIDOutput every loop Sensors suitable for use with PIDController in WPILib are already subclasses of PIDSource Additional sensors and custom feedback methods are supported PID Programming 40 Other Classes Relays through creating new subclasses of PIDSource Jaguars and Victors are already configured as sub classes of PIDO utput and custom outputs may also be created by sub classing PIDO utput The following example shows how to create a PIDController
36. n of kForwardOnly In the following line m_relay is set to the direction of kReverseOnly and is then turned on which results in the reverse output being turned on m_relay2 is then set to forward since it is a forward only relay this has the same effect as setting it to on After that m_relay is turned off a command that turns off any active pins on the channel regardless of direction Using the serial port 42 System Architecture Using RC Using 12C System Architecture This section describes how the system is put together and how the libraries interact with the base hardware It should give you better insight as to how the whole system works and its capabilities Note This is a work in progress the pictures will be cleaned up and explanations will be soon added We wanted to make this available to you in its raw form rather than leaving it out all together Digital Sources I TE A comulator Digital Filter Digital Fitters Be ae GPLO Fife Fitter x3 CE DE od 2 Exomple pen 1 Contributing to the WPI Robotics Library 43 Glossary Glossary Concurrency cRIO deadlock particle quadrature encoder race condition semaphore task VxWorks Digital Sources 44 45 46 47 48
37. nd local instances of objects e C uses header files and a preprocessor for including declarations in parts of the program where they are needed In Java this happens automatically when the program is built by looking at the modules containing the references e C implements multiple inheritance where a class can be derived from several other classes combining the behavior of all of the base classes In Java only single inheritance is supported but interfaces are added to Java to get most of the benefits that multiple inheritance would provide without the complications e Java programs will check for array subscripts out of bounds uninitialized references to objects and a number of other runtime errors that might occur a program under development e C programs will have the highest performance on the platform since it compiles to machine code for the power pc processor in the cRIO Java on the other hand compiles to byte code for a virtual machine and is interpreted WPILib differences We made every attempt to make the transition between C and Java as easy as possible in either direction All the classes and methods have the same names There are some differences that are brought on by the differences in the languages or the language conventions Method naming convention Methods are named with an Methods are named with a upper case first letter and then lower case first letter then camel case after that for ex camel case thereafte
38. nstructor The Drive method of the RobotDrive class is designed to support feedback based driving Suppose you want the robot to drive in a straight line despite physical variations in its parts and external forces There are a number of strategies but two examples are using Gear Tooth sensors or a gyro In either case an error value is generated that tells how far from straight the robot is currently tracking This error value positive for one direction and negative for the other can be scaled and used directly with the turn argument of the Drive method This causes the robot to turn back to straight with a correction that is proportional to the error the larger the error the greater the turn By default the RobotDrive class assumes that Jaguar speed controllers are used To use Victor speed controllers create the Victor objects then call the RobotDrive constructor passing it pointers or refer ences to the Victor objects rather than port numbers Example TODO Getting Feedback from the Drivers Station The driver station is constantly communicating with the robot controller You can read the driver sta tion values of the attached joysticks digital inputs and analog inputs and write to the digital outputs The DriverStation class has methods for reading and writing everything connected to it including 30 Joysticks Getting data from the digital and analog ports joysticks There is another object Joystick that provides a more co
39. nvenient set of methods for deal ing with joysticks and other HID controllers connected to the USB ports Getting data from the digital and analog ports Building a driver station with just joysticks is simple and easy to do especially with the range of HID USB devices supported by the driver station Custom interfaces can be constructed using the digital and analog I O on the driver station Switches can be connected to the digital inputs the digital out puts can drive indicators and the analog inputs can read various sensors like potentiometers Here are some examples of custom interfaces that are possible e Set of switches to set various autonomous modes and options e Potentiometers on a model of an arm to control the actual arm on the robot e Rotary switches with a different resistor at each position to generate unique voltage to add effec tively add more switch inputs e Three pushbutton switches to set an elevator to one of three heights automatically The range of possibilities is limited to your imagination These custom interfaces often give the robot faster control than is available from a standard joystick or controller You can read write the driver station analog and digital I O using the following DriverStation meth ods float GetAnalogIn UINT32 channel Read an analog input value connected to port channel bool GetDigitalln UINT32 channel Read a digital input value connected to port chan nel void SetDigitalOut UINT3
40. ol small hobby servos as typically supplied in the FIRST kit of parts RobotDrive General purpose class for controlling a robot drive train with either 2 or 4 drive motors It provides high level operations like turning It does this by controlling all the robot drive motors in a coordinated way It s useful for both autonomous and tele operated driving PWM The PWM class is the base class for devices that operate on PWM signals and is the connection to the PWM signal generation hardware in the cRIO It is not intended to be used directly on a speed controller or servo The PWM class has shared code for Victor Jaguar and Servo subclasses that set the update rate deadband elimination and profile shaping of the output signal Victor The Victor class represents the Victor speed controllers provided by Innovation First They have a minimum 10ms update period and only take a PWM control signal The minimum and maximum values that will drive the Victor speed control vary from one unit to the next You can fine tune the 21 Controlling Motors Jaguar values for a particular speed controller by using a simple program that steps the values up and down in single raw unit increments You need the following values Max The maximum value where the motors stop changing speed and the light on the Victor goes to full green DeadbandMax The value where the motor just stops operating The value that is in the center of the deadband that tu
41. on of the input signal or just ask the class for the current robot heading You can work at any level First something about our environment The robot controller for 2009 and later is a National Instru ments cRIO 9074 real time controller or cRIO for short It provides about 500x more memory Most of the high speed sensor interrupt logic that typically requires precise coding hand optimiza tion and lots of bugs has been replaced with dedicated hardware FPGA When the user requests the number of ticks on a 1000 pulse revolution optical encoder it just asks the FPGA for the value Another example is A D sampling that used to be done with tight loops waiting for the conversions to finish Now sampling across 16 channels is done in hardware There are two choices of text based languages available C and Java Those were chosen because we felt they represented a better level of abstraction for robot programs than C which was used in the past C and Java when used properly also encourage a level of software reuse that is not as easy or obvious in C At all levels in the library we have attempted to design it for maximum extensi bility There are classes that support all the sensors speed controllers driver station etc that will be in the kit of parts In addition most of the commonly used sensors that we could find that are not tradition ally in the kit are also supported like ultrasonic rangefinders Another example is several
42. on on being used on the digital sidecar The direction can be kBothDirections two direction solenoid kFor wardOnly uses only the forward pin or kReverseOnly which uses only the reverse pin If a value is not input for direction it defaults to kBothDirections This determines which methods in the Relay class can be used with a particular instance of the object Included in the Relay class Method gt A Void Set Value value This method sets the the state of the relay Valid inputs All Directions kOff turns off the Relay kForwardOnly or kReverseOnly kOn turns on forward or reverse of relay depending on direction kForwardOnly kForward set the relay to forward kReverseOnly kReverse set the relay to reverse Void SetDirection Direction direc Sets the direction of the relay Valid inputs tion kBothDirections Allows the relay to use both the forward and reverse pins on the channel kForwardOnly Allows relay to use only the forward signal pin kReverseOnly Allows relay to use only the reverse signal pin Relay m_relay 1 Relay m_relay2 2 Relay kForwardOnly m_relay SetDirection Relay kReverseOnly m_relay Set Relay kOn m_relay2 Set Relay kForward m_relay Set Relay kOf In this example m_relay is initialized to be on channel 1 Since no direction is specified the direc tion is set to the default value of kBothDirections m_relay2 is initialized to channel 2 with a direc tio
43. on the driver station When the station is turned on the joysticks must be at their center position as the startup routine will read whatever position they are in as center The constructor takes either the port number the joystick is plugged into followed by the number of axes and then the number of buttons or just the port num ber from the driver s station The former is primarily for use in sub classing For example to create a class or a non kit joystick and the latter for a standard kit joystick Ctt Joystick driveJoy 1 Joystick opJoy 2 5 8 The above example would create a default joystick called driveJoy on USB port 1 of the driver sta tion and something like a Microsoft Sidewinder which has five analog axises 1 e x y throttle twist and the hat and eight buttons which would be a good candidate for a subclass of Joystick There are two methods to access the axes of the joystick Each input axis is labeled as the X Y Z Throttle or Twist axis For the kit joystick the applicable axes are labeled correctly a non kit joy stick will require testing to determine which axes correspond to which degrees of freedom Each of these axes has an associated accessor the X axis from driveJoy in the above example could be read by calling driveJoy GetX the twist and throttle axes are accessed by driveJoy Get Twist and driveJoy GetThrottle respectively Alternatively the axes can be accessed via the the GetAxis
44. or sideways adjusting the Image Rotation in the start task command will compensate and im ages will look the same as if the camera was mounted right side up Once the camera is initialized it begins saving images to an area of memory accessible by other programs The images are saved both in the raw JPEG format and in a decoded format Image used by the NI image processing func tions Cr int frameRate 15 valid values 0 30 int compression 0 valid values 0 100 ImageSize resolution k160x120 k160x120 k320x240 k640x480 ImageRotation rot ROT 180 ROT_0 ROT _180 StartCameraTask frameRate compression resolution rot Image Acquisition Images of types IMAQO_ IMAGE_HSL IMAO_IMAGE_RGB and IMAQ_IMAGE_U8 gray scale may be acquired from the camera To obtain an image for processing first create the image struc ture and then call GetImage to get the image and the time that it was received from the camera 25 Camera Camera Metrics double timestamp timestamp of image returned Image cameraImage frcCreateImage IMAQ IMAGE HSL if cameraImage printf error s GetErrorText GetLastError if GetImage cameraImage amp timestamp printf error s GetErrorText GetLastError GetImage reads the most recent image regardless of whether it has been previously accessed Your code can check the timestamp to see if it s an image you already processed Altern
45. ot programming We welcome their input in fact we expect their input to help make this better as a community effort To this end all of the source code for the library will be published on a server We are in the process of setting up a mechanism where teams can contribute back to the library And we are hoping to set up a repository for teams to share their own work This is too big for a few people to have exclusive control we want this software to be developed as a true open source project like Linux or Apache We believe that the objet oriented programming paradigm best fits robot programming with WPILib but C programming is available for those with C experience Here is an example of using C with the WPI Robotics Library What is the WPI Robotics Library Ctt The following C program demonstrates driving the robot for 2 seconds forward dur ing the Autonomous period and driving with arcade style joystick steering during the Operator Control period Notice that constants define the port numbers used in the program This is a good practice and should be used for C and C programs include WPILib h include SimpleCRobot h static const UINT32 LEFT _MOTOR_PORT static const UINT32 RIGHT_MOTOR_PORT 2 static const UINT32 JOYSTICK PORT 1 void Initialize void CreateRobotDrive LEFT MOTOR PORT RIGHT MOTOR_PORT SetWatchdogExpiration 0 1 void Autonomous void SetWatchdogEnabled false Drive 0 5 0 0
46. owing declarations in the class are used for the example at Cr RobotDrive myRobot Range greenHue greenSat greenLum This is the initialization of the RobotDrive object the camera and the colors for tracking the target It would typically go in the RobotBase derived constructor Crr if StartCameraTask 1 printf Failed to spawn camera task Error code bs GetErrorText GetLastError myRobot new RobotDrive 1 2 values for tracking a target may need tweaking in your envi ronment greenHue minValue 65 greenHue maxValue 80 greenSat minValue 100 greenSat maxValue 255 greenLum minValue 100 greenLum maxValue 255 Here is the code that actually drives the robot in the autonomous period The code checks if the color was found in the scene and that it was not too big close and not too small far If it is in the limits then the robot is driven forward full speed 1 0 and with a turn rate determined by the cen 38 Vision Image Processing Example 4 two color tracking ter_mass_x_ normalized value of the particle analysis report The center_mass_x_normalized value is 0 0 if the object is in the center of the frame otherwise it varies between 1 0 and 1 0 depending on how far off to the sides it is That is the same range as the Drive method uses for the turn value If the robot is correcting in the wrong direction then simply negate the turn value Ge while IsAutonomous
47. pass 4 compVal compass GetAngle C Example 2 Creating an instance of an I2C compass in C Ultrasonic rangefinder The WPI Robotics library supports the common Daventech SRF04 or Vex ultrasonic sensor This sensor has two transducers a speaker that sends a burst of ultrasonic sound and a microphone that listens for the sound to be reflected off of a nearby object It uses two connections to the cRIO one that initiates the ping and the other that tells when the sound is received The Ultrasonic object mea sures the time between the transmission and the reception of the echo Figure 2 SRFO4 Ultrasonic Rangefinder connections Both the Echo Pulse Output and the Trigger Pulse Input have to be connected to digital I O ports on a digital sidecar When creating the Ultrasonic object specify which ports it is connect to in the 14 Counter Subsystem Setting the gyro sensitivity constructor Cr Ultrasonic ultra ULTRASONIC_ECHO PULSE OUTPUT ULTRASONIC TRIGGER PULSE INPUT C Example 3 Creating an Ultrasonic rangefinder instance Ultrasonic ultra new Ultrasonic ULTRASONIC_ECHO PULSE OUTPUT ULTRASONIC _ TRIGGER PULSE INPUT Java Example 2 Creating an instance of an Ultrasonic rangefinder instance In this case ULTRASONIC_ECHO_PULSE_OUTPUT and ULTRASONIC_TRIGGER_ PULSE_INPUT are two constants that are defined to be the digital I O port numbers For ultrasonic rangefinders that do not have these connections don t
48. queries the camera sensor properties The properties that may be updated are listed below along with their out of the box defaults e brightness 50 e whitebalance auto 23 Camera Sensor property configuration using a text file e exposure auto e exposurepriority auto e colorlevel 99 e sharpness 0 GetlmageSetting queries the camera image appearance properties see the Camera Initialization sec tion below Examples of property configuration and query calls are below Ctt set a property ConfigureCamera whitebalance fixedfluor1 query a sensorproperty char responseString 1024 create string bzero responseString 1024 initialize string if GetCameraSetting whitebalance responseString 1 printf no response from camera An else printf whitebalance s in responseString query an appearance property if GetImageSetting resolution responseString 1 printf no response from camera An else printf resolution s in responseString The example program CameraDemo cpp will configure properties in a variety of ways and take snapshots that may be FTP d from the cRIO for analysis Sensor property configuration using a text file The utility ProcessFile sets obtain camera sensor configuration specified from a file called cam eraConfig txt on the cRIO ProcessFile is called the first time with 0 lineNumber to get the num ber of lin
49. r for ex ample GetDistance ample getDistance Sensors Utility functions Simply call functions for each Java has no functions outside of these functions for example of classes so all library func Wait 1 0 will wait for one tions are implemented as second methods for example Timer delay 1 0 will wait for one second Our version of Java The Java virtual machine and implementation we are using is the Squawk platform based on the Java ME micro edition platform Java ME is simplified version of Java designed for the limitations of embedded devices like the cRIO As a result there are no user interface classes or other classes that aren t meaningful in this environment If you ve done any Java programming in the past it was prob ably with the Java Standard Edition SE Some of the differences between SE and ME are e Dynamic class loading is not support class loading or unloading Reflection a way of manipulat ing classes while the program is running is also not supported e The Java compiler generates a series of byte codes for the virtual machine When you create a program for the cRIO a step is automatically run after the program compiles called pre verifica tion This pre verification pass simplifies the program loading process and reduces the size of the Java virtual machine JVM e Finalization is not implemented which means the system will not automatically call the finalize method of an unreferenced ob
50. raded but does not correspond to this documentation The C library has not yet been upgraded The camera provided in the 2009 kit is the Axis 206 The C camera API provides initialization con trol and image acquisition functionality Image appearance properties are configured when the cam era 1s started Camera sensor properties can be configured with a separate call to the camera which should occur before camera startup The API also provides a way to update sensor properties using a text file on the cRIO PcVideoServer cpp provides a C API that serves images to the dashboard running on a PC There is a sample dashboard application as part of the LabVIEW distribution that can interface with C and C programs Camera task management A stand alone task called FRC_Camera is responsible for initializing the camera and acquiring imag es It continuously runs alongside your program acquiring images It needs to be started in the robot code if the camera is to be used Normally the task is left running but if desired it may be stopped The activity of image acquisition may also be controlled for example if you only want to use the camera in Autonomous mode you may either call StopCameraTask to end the task or call Stoplmage Acquisition to leave the task running but not reading images from the camera Camera sensor property configuration ConfigureCamera sends a string to the camera that updates sensor properties GetCameraSetting
51. rns off the motors DeadbandMin The value where the motor just starts running in the opposite direction The minimum value highest speed in opposite direction where the motors stop changing speed With these values call the SetBounds method on the created Victor object Cr void SetBounds INT32 max deadbandMax center deadbandMin min Example Jaguar The Jaguar class supports the Luminary Micro Jaguar speed controller It has an update period of slightly greater than 5ms and currently uses only PWM output signals In the future the more sophis ticated Jaguar speed controllers might have other methods for control of its many extended functions The input values for the Jaguar range from 1 0 to 1 0 for full speed in either direction with 0 repre senting stopped Use of limit switches TODO Example TODO Servo The Servo class supports the Hitechnic servos supplied by FIRST They have a 20ms update period and are controlled by PWM output signals The input values for the Servo range from 0 0 to 1 0 for full rotation in one direction to full rotation in the opposite direction There is also a method to set the servo angle based on the currently fixed minimum and maximum angle values For example the following code fragment rotates a servo through its full range in 10 steps 28 Controlling Motors RobotDrive Ctt Servo servo 3 create a servo on PWM port 3 on the first module float servoRange se
52. robot classes that provide starting points for teams to implement their own robot code These classes have methods that are called as the program transitions through the various phases of the match One class looks like the old easyC WPILib model with Autonomous and OperatorControl functions that get filled in and called at the right time Another is closer to the old IFI default where user supplied methods are called continuously but with much finer control And the base class for all of these is available for teams wanting to implement their own versions Even with the class library we anticipate that teams will have custom hardware or other devices that we haven t considered For them we have implemented a generalized set of hardware and software to make this easy For example there are general purpose counters than count any input either in the up direction down direction or both with two inputs They can measure the number of pulses the width of the pulses and number of other parameters The counters can also count the number of times an analog signal reaches inside or goes outside of a set of voltage limits And all of this without requiring any of that high speed interrupt processing that s been so troublesome in the past And this is just the counters There are many more generalized features implemented in the hardware and What is the WPI Robotics Library software We also have interrupt processing available where interrupts ar
53. rvo GetMaxAngle servo GetMinAngle for float angle servo GetMinAngle step through range of angles angle lt servo GetMaxAngle angle servoRange 10 0 servo SetAngle angle set servo to angle Wait 1 0 wait 1 second RobotDrive The RobotDrive class is designed to simplify the operation of the drive motors based on a model of the drive train configuration The idea is to describe the layout of the motors Then the class can generate all the speed values to operate the motors for different situations For cases that fit the model it provides a significant simplification to standard driving code For more complex cases that aren t directly supported by the RobotDrive class it may be subclassed to add additional features or not used at all To use it create a RobotDrive object specifying the left and right Jaguar motor controllers on the robot RobotDrive drive 1 2 left right motors on ports 1 2 Or RobotDrive drive 1 2 3 4 four motor drive configuration This sets up the class for a 2 motor configuration or a 4 motor configuration There are additional methods that can be called to modify the behavior of the setup Ctt SetInvertedMotor kFrontLeftMotor true This sets the operation of the front left motor to be inverted This might be necessary depending on the gearing of your drive train 29 Getting Feedback from the Drivers Station RobotDrive Once set up there are metho
54. s and the other for the Y axis This is to get better perfor mance if your application only needs to use one axis The accelerometer can be used as a tilt sensor actually measuring the acceleration of gravity In this case turning the device on the side would indicate 1000 milliGs or one G Figure 1 FRC supplied 2 axis accelerometer board connected to a robot Gyro Gyros typically supplied by FIRST in the kit of parts are provided by Analog Devices and are actu ally angular rate sensors The output voltage is proportional to the rate of rotation of the axis normal to the gyro chip top package surface The value is expressed in mV second degrees second or 10 Gyro Using the Gyro class rotation expressed as a voltage By integrating summing the rate output over time the system can derive the relative heading of the robot Another important specification for the gyro is its full scale range Gyros with high full scale ranges can measure fast rotation without pinning the output The scale is much larger so faster rotation rates can be read but there is less resolution since a much larger range of values is spread over the same number of bits of digital to analog input In selecting a gyro you would ideally pick the one that had a full scale range that exactly matched the fastest rate of rotation your robot would ever experience That would yield the highest accuracy possible provided the robot never exceeded that range
55. trolling Motors PWM Cr StartCameraTask Initialize the camera PCVideoServer pc The constructor starts the image server task pc Stop Stop image server task PESStart Restart task and serve images again To use this code with the LabVIEW dashboard the PC must be configured as IP address 10 x x 6 to correspond with the cRIO address 10 x x 2 Controlling Motors The WPI Robotics library has extensive support for motor control There are a number of classes that represent different types of speed controls and servos The library is designed to support non PWM motor controllers that will be available in the future The WPI Robotics Library currently sup ports two classes of speed controllers PWM based motor controllers Jaguars or Victors and servos Motor speed controller speed values floating point numbers that range from 1 0 to 1 0 where 1 0 is full speed in one direction and 1 0 is full speed in the other direction 0 0 represents stopped Mo tors can also be set to disabled where the signal is no longer sent to the speed controller There are a number of motor controlling classes as part of th Base class for all the pwm based speed controllers and servos Victor Speed controller supplied by Innovation First commonly used in robotics compe titions with a 10ms update rate Jaguar Advanced speed controller used for 2009 and future FRC competitions with a 5ms update rate Servo Class designed to contr
Download Pdf Manuals
Related Search
Related Contents
Broan-NuTone N-79V Speed Control Kitchen Hood Finisar FWLF1523P1C51 network transceiver module HQ W7-54913- N Samsung VC-9636 راهنمای محصول PDFファイル ManageEngine OS Deployer User's Guide Manual de Servicio - IHMC Public Cmaps Copyright © All rights reserved.
Failed to retrieve file