FIRST Robotics Competition Documents

cancel
Showing results for 
Search instead for 
Did you mean: 

LabVIEW Tutorial 10: Robot Simulation

Simulate Your Robot in LabVIEW for FRC!

 

With LabVIEW, you can get a early start learning LabVIEW Robotics for the FIRST Robotics Competition by using Robot Simulation to program a predefined robot without having an RT (Real-Time) roboRIO target. This allows multiple developers to concurrently create and test LabVIEW code without requiring each developer to have access to the hardware. Programming is the same, except only the predefined Actuators and Sensors on the simulated robot are supported. By exploring the pre-built LabVIEW Robotics Simulation projects, you can discover aspects of LabVIEW Robotics applications and learn how to start your own LabVIEW Robotics Simulation project. The intent of the Simulations are to start learning LabVIEW for FIRST Robotics Competition, but you do have the ability to move simulation code to an RT roboRIO target and run on it on a real robot.

 

 

Opening the Learn LabVIEW: Mecanum Maze Simulation Project

 

  1. In order to interact with real, or simulated robots, we must use the FRC Driver Station to enable the robot. Start the FRC Driver Station.

     

    Note  If two windows open, one may be a generic Dashboard window. Close it. You should only have a window open that is similar to the one shown below. 

     2016DriverStation.gif

  2.  

     

  3. Create a New FRC roboRIO Robot Project either from the Getting Started Window or by going to File>>New…

     

    2015ProjectWizard.gif

  4.  


  5. Enter your Team number, and select the Learn LabVIEW: Mecanum Maze Simulation project.  Entering your team number will automatically determine the IP address for you. For example, if your Team number is 1959, the IP address of the RT roboRIO Target will roboRIO-1959-FRC.local.

    CreateRobotProj_DialogMaze.png
  6.  

     

  7. When the LabVIEW Project opens, select and open Robot Main.vi. Notice the Robot Main.vi as well as the folder containing the team VIs and the support VIs are all under My Computer. This is because the simulation is controlled and processed on your computer.
  8.  

  9. Before you run Robot Main.vi, you will open the corresponding FRC dashboard project. The dashboard project will allow you to view the custom data the Maze robot is sending. The custom tab in the dashboard has already been set up for you. You can create the dashboard project from the Getting Started window or by going to File>>New...>>Project>>Project From Wizard>>FRC Dashboard Project.
  10.  

  11. In the Create New FRC Dashboard Project dialog box, select the Dashboard for Maze Robot project.

    2017CreateDashProj_DialogMaze.png
  12.  

     

  13. Notice the Dashboard Main.vi is under My Computer. Open the Dashboard Main.vi. Select the Custom tab. In the FRC Robot Project we have already set up the roboRIO code to send data to this tab. Later on in this tutorial, you will learn how to add more information to this tab.  
  14.  

    2017Dash.png

     

     

  15. Return to the Robot Main.vi. Run the VI and the FRC Simulation Viewer should open. Notice that the screenshot has the Simulation Viewer option to Follow turned On.
  16.  

    Note  By default, the robot's simulated camera is turned off, as this takes more of the computer's processing power. To turn on vision acquisition, see the section in this tutorial titled Enabling Vision in the Simulation.

     
    2017RunSimViewerMaze.png


  17. The FRC Driver Station should now show that you have a Simulated Robot. Click Enable to place the robot in Teleoperated Enabled mode. If it does not show that you have a Simulated Robot, click the Setup tab (gear icon) and ensure your team number is entered correctly.
  18.  

    DriverStationSimulated.gif


  19. Return to the Dashboard Main.vi and run the VI. Arrange your screen so that you can see Dashboard Main.vi and the FRC Simulation Viewer.
  20.  

    Note  For help navigating the FRC Simulation Viewer, see the section titled Using the Simulation Viewer

     

  21. You should now be able to use a joystick to drive the robot in the FRC Simulation Viewer and view the data being sent by the robot in Dashboard Main.vi.
  22.  

  23. The goal in the Mecanum Maze Simulation is to navigate the robot through the maze in the quickest amount of time. In the Custom tab in the dashboard, you'll notice that you can see the walls around the robot change as you drive and the time continually updates. Notice the movement of the robot. You can drive forwards, backwards, sideways, and spin. This is due to the mecanum wheels on our simulated robot.

 

Using the Simulation Viewer

 

2017SimViewer.png

 

Aside from Teleoperation of the simulated robot using the joystick, there are several options in the FRC Simulation Viewer to help customize your simulation environment:

  • SimulateRun and Pause buttons are available.
  • Time Factor—allows you to run the dynamic model of the system slower than real time (minimum 0.25) and faster than real time (maximum 20.0).  A Time Factor of 1.0 will be representative of the system in real time.
  • View—Side or Driver enables you to default back to one of the two traditional points of view.  If Follow is Off, left-clicking and panning in the Viewer window will allow you to choose a custom viewing angle.
  • Follow—If you turn Follow On, the Viewer will keep the robot centered in the screen from whichever View you have selected.  If you turn Follow Off, left-clicking and panning in the Viewer window will allow you to choose a custom viewing angle, but zooming is no longer possible.
  • Color—selects the background color.
  • Driver Station IP—Auto should be set to connect with the correct IP Address based on the RT roboRIO Target properties in the LabVIEW Project Explorer, but there is also an option to Set the IP address manually.
  • Close—Close the FRC Simulation Viewer and stop Robot Main.vi.

 

 

Programming a simulated robot


The simulated robots are predefined - they cannot be changed.  But they are the same as real robots in that they have sensors and actuators attached. By default, each project includes code to drive and control the robot.  Beyond that, you may add your own code to make use of additional sensors and actuators included on each robot.  This section includes the following topics:

Adding Sensors & Actuators to the Simulation
Enabling Vision in the Simulation
Building the Simulation for the roboRIO


Adding Sensors & Actuators to the Simulation


In the Learn LabVIEW: Mecanum Maze Simulation project, you will notice a file titled Robot Simulation Readme.html under My Computer. This file contains information on the sensors and actuators that are connected to the simulated robot. In order to take advantage of these on the simulated robot, you need to know which channel the sensors and actuators are on and how each is wired to the simulated robot. 

The Robot Simulation Readme.html is unique to each project. When modifying a simulation project, check this file to ensure that you have selected the correct channel to communicate with a sensor or actuator. Below is a list for the Mecanum Maze Simulated Robot.



Supported Actuators and Sensors on the Mecanum Maze Simulated Robot


        Actuators on the simulated robot
  1. Front Left Motor 
    • PWM Channel = PWM 0
  2. Front Right Motor
    • PWM Channel = PWM 1
  3. Rear Left Motor
    • PWM Channel = PWM 2
  4. Rear Right Motor
    • PWM Channel = PWM 3
  5. Camera Servo 
    • PWM Channel = PWM 5
    • Angular Range = 170
        Sensors on the simulated robot
  1. Gyro 
    • Analog Channel = AI 1
  2. SRF04 Ultrasonic Forward Facing
    • Ping DIO Channel = DIO 0
    • Echo DIO Channel = DIO 1
  3. SRF04 Ultrasonic Right Facing
    • Ping DIO Channel = DIO 2
    • Echo DIO Channel = DIO 3
  4. SRF04 Ultrasonic Left Facing
    • Ping DIO Channel = DIO 4
    • Echo DIO Channel = DIO 5
  5. AXIS M1011 Camera


You will notice that the simulated robot includes a gyro. To add a gyro to the Maze Simulation, you can take advantage of some existing sample code and the Custom tab on the simulation dashboard. Once you get code working in simulated mode, the same code can be built for a real robot.


  1. Open the Dashboard Main.vi. For the simulation, you will take advantage of the Custom tab on the simulation dashboard. FRC Dashboard projects already include code that makes it easier to write and read data from our robot's network table in our dashboard. 

    2017DashGyro.png

     


  2. On the Dashboard Main.vi Front Panel, navigate to the Custom tab. You will already see an Orientation gauge here. This indicator has been created for you. Right-click it and select Find Terminal. This will take you to where the indicator is on the Block Diagram of Dashboard Main.vi. Take note of the name of the indicator, including any capital letters. Since the indicator is on the Custom tab, you can pass data to it using the NT Write Number VI in your robot code, and do not need to add any code to Dashboard Main.vi. The code for this is shown in step 8 below. 

  3. You will now update the robot simulation code to add the gyroscope. Open the Robot Main.vi

  4. Go to the Block Diagram of Robot Main.vi. When incorporating a sensor you typically initialize it once, read its values in a loop, and then close communication when the program ends. On the Block Diagram of Robot Main.vi, find Begin.vi and double-click it to open it. 

  5. In Begin.vi we will add code to intialize the gyro.  Place the code near the joystick configuration. You can find the gyro VIs in your Function Palette under WPI Robotics Library>> Sensors>> Gyro. Your code should look similar to the following picture.

    2017OpenGyroBlockDiagram.png

     


  6. Return to Robot Main.vi. Next, you will add code to read from the gyroscope sensor periodically. Open Periodic Tasks.vi (Timed Tasks icon) and create some space in between the sonar code and the bottom of the While Loop. An easy way to create space in LabVIEW is to place your cursor where you want to start, hold down the Ctrl key, click your left mouse button, and drag.

  7. Add the code to read the gyro. The VI that reads the gyro is WPI_GyroGetAngle.vi. Note that we are reading the gyro angle in a loop to continuously get the current value.  But the gyro reference is read outside the loop, since we only need to read the reference once. Your code should be similar to the following, where the grey line represents the left side of the While Loop.

    2017ReadGyroBlockDiagram.png

    Note  The indicator on our dashboard has a range of 0 to 360, but the VI to read the gyro data returns a value between -180 and 180. To account for this, you can add some logic to shift the values.

  8. Just reading the gyro sensor does not automatically update the dashboard. To do this, you will add code to write to the Orientation variable in the Network Table. From the WPI Robotics Library>>Dashboard palette drop the NT Write Value.vi.  Click on its drop-down selector and choose NT Write Number.  This will make its datatype match that of the Orientation terminal in the dashboard code.  Right-click on the Name terminal of NT Write Number and choose Create>>Constant.  Enter the constant value exactly as the indicator was named on the Custom tab on the dashboard project. This will now automatically update the dashbaord project indicator without adding extra code to the dashboard. The final Periodic Tasks.vi section of code should look similar to the following: 


    2017WriteGyroNT.png

     


  9. Lastly, you will close the reference to the sensor. From the Robot Main.vi, find the Finish.vi. In this VI, add code to close the gyro. You can create code to close the gyro that is similar to the existing code closing the Joystick. 

    Note  In a similar way, you can add code to control the camera servo in Teleop.vi. If you are able to control the camera servo, as well as get information from the other sensors, you can start to modify your program to try to navigate the maze autonomously. 


Notes:

  1. If you run a robot in simulation mode, but you have programmed it for I/O not supported in simulation mode, then errors will be generated that slow the simulation performance. You can check for errors in the Messages box of the FRC Driver Station.
  2. The E-Stop (space bar) works in Simulation Mode.  To reset it, simply wait 5 seconds and then stop and rerun Robot Main.vi.


Enabling Vision in the Simulation


By default, image acquisition is turned off in the Simulation, as it takes more of the CPU processing power to enable Vision. This section will cover how to enable Vision in the Mecanum Maze Project.


  1. Open Robot Main.vi and go to the Block Diagram. 

  2. Near the bottom, you can see two Diagram Disable Structures. In the first, you can see the Vision Processing.vi. This VI is responsible for acquiring and processing the data on the simulated robot. Right-click the structure and select Enable This Subdiagram


    2017EnableVision.png

     


  3. Double-click to open the Vision Processing.vi and navigate to the Block Diagram. Notice the Case Structure in the While Loop. In order to acquire images, the Enable Vision global variable must be true.

  4. The second Disable Structure contains the WPI_CameraSend Images To PC Loop.vi. This VI continually sends data to your dashboard. Right-click and enable the subdiagram containing WPI_CameraSend Images To PC Loop.vi.

  5. On the Front Panel, ensure we turn on the Enable Vision contol. 

  6. When we run the project, select Camera in the bottom left corner of the Dashboard. 

    2017MazeDashboardVision.png

     

Building the Simulation for the roboRIO


The FRC Simulation projects are intended to get a start with LabVIEW Robotics. You can utilize the Simulation projects to learn the basics behind programming and controlling a robot. Though not the main use of the Simulation projects, you do have the ability to develop code in simulation mode and then move the code to the roboRIO.  This would only make sense if your physical robot had the same I/O as the simulated robot. This section will cover how to move the simulation projects to the roboRIO.


  1. Close any open VIs. In the Mecanum Maze Simulation Project, move the Robot Main.vi, Support Code folder, and Team Code folder to the target in the project. 

    2017BuildMazeTarget.png

  2. Open Robot Main.vi. At the bottom, we can see a Timing Sensor VI and the Simple Error Handler. Delete these VIs from the Block Diagram. The Timing Sensors.vi is used by our simulation to determine when our robot crosses the start and finish in the maze. If you open the VI, you will see that we create two sonar sensors to track when you cross the start and finish line. If you aren't simulating, you do not need these.


  3. Under the Target, expand Build Specifications. Right click the FRC Robot Boot-up Deployment and select Properties

  4. On the left side of the build window, select the Source Files category. 

  5. Select Robot Main.vi and select the arrow to move the VI to the Startup VIs area.
     
    2017BuildMazeSpec.png

     


  6. Select Build


  7. Switch to the Dashboard project. To build the dashboard executable, you can follow the instructions in Tutorial 6 - Creating a Custom Dashboard in the Rebuilding the FRC Dashboard Project. Tutorial 6 can be found on the LabVIEW splashscreen under Tutorials once you have installed the FRC Update Suite.

 


Exploring the Learn LabVIEW: Ball Shooter Simulation Project


The Ball Shooter Simulation Project is another simulation project that has been created to help you get started with LabVIEW Robotics. In this section, you will open and run the Ball Shooter Simulation Project. After testing out the project, you will open and explore some of the code.


  1. Start the FRC Driver Station. Again, if a generic Dashboard window opens, close this. You will be using the custom Ball Shooter Dashboard project. 

  2. Similar to the Maze Simulation, you will create both the robot and the dashboard projects.  Create a new FRC roboRIO Robot Project either from the Getting Started Window or by going to File>>New…

  3. Enter a Project name, your Team number, and select the Learn LabVIEW: Ball Shooter Simulation project.

    CreateRobotProjDialogBallShooter.png

     

  4. Similar to the Maze simulation, you will also need to create the dashboard project from the Getting Started window or by going to File>>New...>>Project>>Project From Wizard>>FRC Dashboard Project. Select the Dashboard for Ball Shooter Robot project.

    2017CreateDashProjDialogBallShooter.png

     

     

  5. Run Robot Main.vi and enable TeleOperated mode in the FRC Driver Station. The Simulation Viewer should now be open and you should be able to control the robot with your joystick. 

    2017RunSimViewerBallShooter.png



  6. Run Dashboard Main.vi. You should be able to view data being sent by the robot in the dashboard. Notice the different settings as well as the score section.

    2017ShooterDashboard.png

     

     

  7. In this simulation, the goal is to score as many points as possible. On the wall of the simulation, you can see three different holes. The top, small hole is worth the most points, but is harder to hit. On the dashboard, this corresponds to the "High Hits". The middle, medium-sized hole is worth 25 points, and is denoted by "Middle Hits". The "Low Hits" refers to the lowest, largest hole and is worth the least amount of points. You receive bonus points for getting a ball in each hole. Try to score a few points by driving with a joystick and shooting using the joystick trigger (Button 1).

    2017BallShooterWall.png

     

     

  8. If there is not enough power behind your robot's shots, try adjusting Shooter Speed. Shooter Speed refers to the speed of the motors that launch the balls from our robot. Experiment to discover what control on the joystick adjusts Shooter Speed.


  9. Notice the other controls on the dashboard. You do not have an infinite number of balls in your robot. When you get low, you must drive around and scoop up the balls using the chute in the front of your robot. Scoop Speed refers to the speed of the motors scooping the balls into your robot hopper.


  10. Look more closely at the balls stored in your robot hopper. They should be moving slightly. This is because there is an agitator servo inside of the bin that continuously moves the balls. This ensures that balls do not get stuck, and that our robot can keep shooting. Try adjusting the Agitator Speed to see how the robot reacts.


  11. When you are done controlling the robot, you can close the dashboard and the Simulation Viewer. Press the Finish button on the Robot Main.vi.


  12. Open the Robot Main.vi Block Diagram. Earlier in the Maze robot code, you explored adding and periodically reading a gyroscope sensor in Periodic Tasks.vi. In this program Periodics Tasks.vi is used to control the scoop motors, read sonar and gyro sensors, and control the agitator servo every 100ms.


  13. By contrast, the loop containing Teleop.vi runs every 20ms. Teleop.vi is good for robot control, such as writing values to move the robot.  Double-click to open Teleop.vi. Notice how the code reads from Network Tables, and then passes the values to a VI or writes to a variable. In the dashboard project, the code writes the values to Network Tables so that the robot project can read the user values.


  14. Return to Robot Main.vi. Similarly to the Maze Simulation, another robot and sensor have been incorporated into the project to keep track of scoring. Double-click to open scoring sensor.vi and inspect the code to keep track of scoring.


  15. Though a bit more logic is involved in this code, notice that, based on distance from the sensor, the code uses a Case Structure to write updates to the target counters, and then adds the score. The code shown below also uses NT Write Number to pass values to the dashboard.  The names low target and Score exactly match the terminal names on the diagram of Dashboard Main.vi.

    2017ScoringSensor.png

     

  16. Close scoring sensor.vi when done inspecting the code.  Note that scoring sensor.vi is only used for simulation scoring.  It would be removed if this code were to be built for a real robot.

  17. Similarly to the Maze Simulation, vision is disabled by default. If you would like to run the code again with vision enabled, you can follow the steps outlined in Enabling Vision in the Simulation.
Comments
pero23
Member
Member
on

hello, urgent help, how can i obtain x  y coordinate colour detection(output) into an angle for ev3 robot.

basically i have a x y coordinate value that i obtained from a picture and i am trying to use a vision camera to control robot to move the the object. 

as of now i am stuck, can anyone help me please

MarkBalla
Active Participant Active Participant
Active Participant
on

There is an Issue with the default ShootServo Values in the FRC 2020 simulator.

The default project sets the ShootServo value to 0 when the the control button is pressed and 85 when the control button is released.

After the simulator is running and the driver station is enabled the ShootServo is moved to the fully back position for both a value of 0 and 85.

servo 0.pngservo 85.png

Out testing showed that the values 7 and 17 should be used instead so the ShootServo works correctly.

servo 7.pngservo 17.png

The ShootServo value is set in Teleop and the following values should be replaced

0 to 7

85 to 17

code wrong.pngcode fixed.png

If someone from ni sees this comment can you please fix this issue for future versions. It was difficult to figure out the problem and the ball shooter simulator is not very useful if this function does not work.

 

Mark

 

Mark

 

Champion CLA

Making senseless computers do
intelligent real world things
is NOT easy. SO MAKE IT FUN!
MarkBalla
Active Participant Active Participant
Active Participant
on

Further investigation of the simulator shows the agitator and camera servos are also not correct.

 

The agitator does a full rotation in 55 counts instead of the expected 360

Agitator 0.pngAgitator 90.pngAgitator 180.pngAgitator 270.pngAgitator 360.png

 

The camera servo does a 90 deg rotation in 12 counts.

camera 90.png

Champion CLA

Making senseless computers do
intelligent real world things
is NOT easy. SO MAKE IT FUN!
Contributors