Tutorial to create simple drivable chassis in Unity

I’ve published a video presenting a tutorial on how to create a simple drivable chassis using the Unity gaming engine interfacing to Robot code using the WPILib WebSocket simulation. The tutorial will walk you through creating the basic (really basic!) chassis, adding wheels, motors, TalonSRX motor controllers, and gearboxes. The aim is to show just how easy it is to get started.

Here is a link to the Video: https://youtu.be/Qryc2ck-AgY
Here is a link to the starter code and instructions on GitHub: GitHub - kinahawi/FRC-2021-Starter-Project: Unity project to use when getting started with simulations for FIRST Robotics during the 2021 season.
Here is a link to a completed version of the tutorial in case you have trouble or just want to give it a try: GitHub - kinahawi/Chassis-Demo-Complete: Completed chassis demo example using Unity and WPILib

I hope to produce more content and instruction, but that will be somewhat dependent on how useful folks think it is, so please let me know if you like it or if I’m wasting time or duplicating something that already exists.

8 Likes

Really great work. Looking forward to see what you are able to do with it.

I was able to replicate your instructions in the video on my arch linux system up to the point where you simulate the robot in desktop mode using the winsock server. However, I do not have a logitech joystick. And could not follow the instructions on how to configure and troubleshoot setting up a different joystick.

Using the halsimgui I can drag a system joystick (or keyboard) over to joystick[1] and see it working. However, with unity over the ws server, it wasn’t clear to me from your video or google doc how to setup/configure/troubleshoot the complete process of wiring up joystick input.

Several of the coders on our team do not have game controllers at home. It’d be nice if the simulator setup did not have a dependency on a physical game controller.

I look forward to your next installment. I’m happy to help with testing. Would love to see this flushed out for general availability in 2022.

You can set it up to use the keyboard or other controller device. Here are some simple instructions for setting it up to use the arrow keys:

In Unity, go to Edit menu and select Project Settings…

Select the “Input Manager” item in the list on the left of the Project Settings dialog.

In the Input Manager options expand the “LeftY” item

  • Change the “Inverted” checkbox to be checked (to make up act like forward)
  • Change type to “Key or Mouse Button”

Expand the item called “RightX”

  • In the item called “Negative Button”, type the word “left” (no quotes) to tell Unity to use the left arrow key for negative values
  • In the item called “Positive Button”, type the word “right” to tell Unity to use the right arrow key for positive values
  • Change the Type to “Key or Mouse Button”

Close the settings box and you should be able to drive using the arrow keys.

You should be able to do something similar with the rest of the controls as well. If you look at the GameController in the hierarchy, you will see the names of the inputs polled for the Joysticks and buttons. The names listed there are the same as those defined in the input manager.

If this isn’t clear, let me know and I’ll try to find a more effective way to communicate.

Bill

Bill,

Thanks! Got it working. Looking forward to your next installment.

I’m also looking forward to digging further into what you’ve done and re-working it for our robot and robot code. I imagine this will spark more than a little interest at our next meeting.

Part of my confusion was my lack of familiarity with unity. I lost my updates to Project Settings, Input Manager several times without realizing it. Until I made the changes and saved.

A suggestion: in your documentation or a follow up video, it might be help if you were to walk through the connections from a hardware device in robot code -> simulator interface -> unity. Explain where (and where not) to connect the dots in the mental model we create in our heads. For example:

[robot.java]
Joystick _joystick = new Joystick(0)

to

[inspector for GameController]
“Joystick Number” 0

but not

[Edit, Project Settings…, Input Manager, LeftY]
“Joy Num” = Joystick 1

It won’t be immediately obvious to all newcomers what is significant for binding the robot.code to the simulator versus what is solely of concern to model the robot you are creating in unity.

Thanks for sharing all your work on this. Looking forward to whatever comes next.

Thanks Garrett, I’m glad you are finding it useful! That was my hope when I first started working on it.

Thanks also for the feedback and ideas on improving the instruction and documentation. I think you’re spot on and I’ll work on a follow-up video showing the complete thread from simulation to robot. I have another document which describes some other details, but it is a little out of date and not as well organized as I would like. Here is a link to it in case you would find it helpful: WPILib Simulation Integration with Unity - Google Docs

That document also contains links to other another GitHub project which implements most of our teams bot from last year. It illustrates use of PWMs, pneumatics, and an analog input.

The video might take me a couple of days. In the meantime, don’t be bashful with the questions and suggestions!

Bill

Ok, here is a video I published to try to make this more clear. Please let me know if it’s helpful or if I missed the mark. If you have questions in other aspects of the simulation, let me know that too because the goal is to produce something that the community finds helpful and easy to use.