Programming CYOA Training#

Overview#

  • This is a self-paced training course designed to provide you with an environment where you can quickly gain experience working with the key technologies of our robots: with ESP32, the Arduino Framework for ESP32, Bluetooth pairing, and motor controls.

  • This course can be as easy or hard as you make it. There are four key objectives, and three optional challenges, along with a set of recommended steps.

  • Ultimately, you do not have to do anything except achieve the four objectives. You can stop reading this page after this section and still achieve all of them (but it will be exceedingly difficult to do so, and you could break a robot).

  • Thus, there are several “hints” and/or “solutions” placed in this document, which you can choose to use if you find yourself stuck on a particular area.

    • The only information that is revealed by default is the minimum amount of information you need in order to work with the aforementioned technologies, particularly things that are specific to “Polar Robotics” more so than ESP32 etc. in general.

    • You are expected to use the internet (Google etc.) to help you complete the programming objectives.

      • Note that the entire Polar Robotics codebase is available, and if you are very stuck you may consult it, but that somewhat defeats the purpose of this activity, and it will probably be easier to figure out how to do the task itself rather than figure out how we did it (which is likely more complex than the way you need to do it here).

    • Once you have determined a solution, it is recommended to view any hints you did not use to supplement your understanding.

Tip

Walk before you run. Build up to achieving the objectives outlined below. Try to get a proof-of-concept working first. This will make the process less frustrating and more rewarding.

Hard Rules#

  • There are a few rules required for your safety, the functionality of the robots, and the effectiveness of this training.

  1. You may work either alone or with one other person.

    • You are allowed (and encouraged) to talk to other trainees, but this rule is intended to ensure all trainees gain roughly the same amount of experience, i.e., that no one person does all the coding.

  2. You may not set the motors to over 50% power.

    • The motors used by most robots are extremely powerful. You can harm yourself, the robot, and the environment if you set them to full power.

  3. You must initially set the motors to 0% power upon startup.

  4. You may not disassemble the robot.

    • This also means you should not remove the ESP32 from its breakout board without permission from the training manager.

  5. Do not plug the ESP32 into your computer and its battery at the same time.

  6. Turn OFF the main power switch, then power the ESP (via a battery or your computer) before inserting the 24V drill battery and flipping the power switch to ON.

    • If you turn on the main power (which drives the motors) before the ESP is ready, they can spin unexpectedly.

  7. It is strongly recommended that until you are ready to drive the robot, you sit the robot up against something so the wheels don’t touch the ground while testing, especially if your computer is directly connected to it.

    • Similarly to (6), you do not want the robot to go out of control.

  8. Do not do burnouts on the floor.

    • If you do, you will be responsible for cleaning it up.

Key Objectives#

  1. Make the LED on the ESP32 blink with a period of 1 second.

  2. Make a function to spin both drive motors at 25% power for three seconds.

  3. View live output from the PS5 controller on the serial monitor.

  4. Be able to drive a lineman from a controller.

Optional Challenges#

  1. Figure out how to control the motors by yourself instead of using the provided code.

  2. Implement pairing by yourself instead of using the provided code.

  3. Implement an acceleration curve to your drive controls.

Creating a PlatformIO Project#

Tip

PlatformIO has several example projects available from the Project Examples button in the Quick Access menu on the PlatformIO homepage. You may find these helpful.

Note

If you already have the ESP32PRCodebase repository open in VSCode, you will want to close it before creating a new PlatformIO project.

  1. Navigate to the PlatformIO homepage (alien sidebar icon, Quick Access pane on the left, PIO Home dropdown, Open).

  2. On the right of the PlatformIO homepage, under the Quick Access button menu, click New Project.
    Creating a new PIO project|600

  3. Give the project an appropriate name. Then select the NodeMCU-32S board and the Arduino framework. You may optionally change the location.
    Creating a new PIO project 2|400

  4. You may need to wait a short time, but eventually you should be greeted with a new project opened to the platformio.ini file as shown below:
    Creating a new PIO project 3|600

  5. Add the following 3 lines to the bottom of platformio.ini, under the current last line:

board_build.mcu = esp32
upload_protocol = esptool
monitor_speed = 115200
  1. Now, on the project explorer sidebar, open the src folder, and then main.cpp. You are ready to begin programming!

Blinking the LED#

  • If you successfully completed the uploading code training training, you would have seen the blue LED begin blinking as the program begins to run. In our production code, this indicates our pairing routine. For now, your goal is to make the LED blink with a period of one second (you may choose to do longer or shorter, as long as it blinks consistently while your program is running).

    • Note that the ESP also has a red LED. This is the power LED, which indicates that the ESP has power. You cannot make it flash via code. Please do not make it flash manually by rapidly plugging and unplugging the ESP. This is not the objective.

    • The LED you are trying to program is typically referred to as the “built-in LED” - this may help you find information about it.

Moving the Motors#

  • Your second objective involves controlling the motors.

    • Again, you should start by trying to control one, and do not set any motor to over 50% power.

    • Also remember that you should encapsulate this in a function outside of setup() or loop().

  • There are two key pieces of information that you need in order to achieve this objective:

    1. The pins used for the lineman motors are 32 for the left motor and 33 for the right motor. They are already wired up correctly, so you only need to write your code to fit this.

    2. Controlling the motors is not as simple as the LED, nor can you simply write “0.5” to the pin to drive the motor at half power.

      • The technology that allows us to control the motors at variable speeds is detailed in the first hint below.

      • For Challenge 1, you can attempt to figure out the control method by yourself. For your reference, the motors are AmpFlow E30-150-24 (short) or E30-400-24 (long) 3” high-performance economy motors. Our motor controllers are the Sabertooth 2x25s.

Serial Monitor and Bluetooth Pairing#

  • Your third objective is to achieve a live view of the PS5 controller output on the serial monitor.

    • To do this, you must be able to: 1. send messages over the serial connection, and 2. connect to a PS5 controller.

  • In order to achieve this objective, you will at minimum require the following information:

    • The MAC addresses of our PS5 controllers begin with either bc:c7:46:03 or bc:c7:46:04.

  • Again, walk before you run. Start with sending a simple string message over the serial connection.

  • Challenge 2 is to implement Bluetooth pairing from scratch.

    • Warning: This is essentially impossible at the level of experience the average participant in this tutorial is expected to have. The challenge is designed to get you to think about what you would have to do in order to get this to work, and to give you experience working through a complex issue. You are not expected to be able to solve this challenge, but you are welcome to attempt it before moving on.

  • If you choose not to pursue Challenge 2, in order to control the PS5 controller, you will need to use the ps5-esp32 library.

    • The easiest way to use this is to copy the ps5-esp32-main folder from our codebase’s lib folder to your project’s lib folder:
      PS5 Library|250

      • Note that the image above is for illustration purposes, as this is possible only if you have ESP32PRCodebase and your new project in the same VSCode Workspace (not recommended). You will need to do this in your OS’s file manager.

  • While the other challenges are feasible for this training, attempting to build a communication interface for the PS5 controller from scratch is far outside the scope of a beginner project, and frankly is something that we realistically wouldn’t do either (hence why we used this library). So don’t try to do it.

    • However, you should be able to analyze the library’s source code and determine which functions to use. If you aren’t able to, there are some hints below.

Controlling the Robot#

  • Your final objective is to implement a control scheme to drive the robot.

  • You are encouraged to be creative with your control scheme, although controlling the robot will be easiest if you use the joysticks in an intuitive manner.

  • Challenge 3 can be done after you have a basic drive control code working. Adding some non-linearity to the power of the motors will help prevent loss of traction.