1 of 48

RC Robot Project

2 of 48

Assembling the Kit

Notice the red & black wires

Left Side Back = Red on top

Left Side Front = Black on top

Right Side Back = Black on top

Right Side Front = Red on top

2

3 of 48

Something like this ...

3

Front

Back

4 of 48

The Raspberry Pi

4

5 of 48

The L298N H-bridge (motor controller)

5

Left

Right

6 of 48

Connect the Raspberry Pi to the L298N

6

Use the wires like this

  1. Connect GPIO17 on the rPi to IN1 on the L298N
  2. Connect GPIO27 on the rPi to IN2 on the L298N
  3. Connect GPIO23 on the rPi to IN3 on the L298N
  4. Connect GPIO24 on the rPi to IN4 on the L298N

7 of 48

Ground the Raspberry Pi to the L298N

7

Use the wires like this

  1. Connect any Ground on the rPi to the GND on the L298N

8 of 48

Something like this ...

8

9 of 48

Mount the battery packs

9

10 of 48

A look inside

10

11 of 48

Python Tutorial

11

  • https://www.programiz.com/python-programming
    • At a minimum, complete the following sections
      • Introduction
      • Flow Control

12 of 48

What’s next?

12

  • Gamepad code - On Laptop
  • Motor Controller (L298N) code - On Raspberry Pi

13 of 48

Gamepad Code (on your laptop)

13

  • Install python v3:
  • After python has been installed …
      • install pygame: pip3 install pygame
    • Install pynetworktables: pip3 install pynetworktables
  • Attach the gamepad to a usb port
  • Download the gamepad controller app
    • xbox360_windows.py
  • Run the app
    • python xbox360_windows.py

14 of 48

Gamepad Code

14

15 of 48

Gamepad Code

15

  • In the following slides we will
    • Update the xbox360_windows.py to output to the networktable
    • Show the expected output in the OutlineViewer

16 of 48

Outputting to the NetworkTable

  • Import the NetworkTableInstance in the code.

  • Initialize the instance to get global default instance

16

17 of 48

Outputting to the NetworkTable

  • Start the client which should be the laptop’s ip address

17

18 of 48

Outputting to the NetworkTable

  • If the IP address is not known it can be found with “ipconfig” in the command prompt. The IP address is the IPv4 address.

18

19 of 48

Outputting to the NetworkTable

  • Start a table named “drive” and create 3 entries (right, left, stop)

19

20 of 48

Outputting to the NetworkTable

  • Inside the axis logic, make a condition that if the desired axis is used than set the entry to output joystick values.

Here, the axis 1 is the left joystick up and down, while the axis 3 is the right joystick up and down.

20

21 of 48

Outputting to the NetworkTable

  • Inside the button logic, make a condition if the desired button is pressed than set the third entry to be true.

Variable “i” should be the button number while variable “button” will always be 1 when pressed.

21

22 of 48

Networktable in OutlineViewer

22

23 of 48

Motor Controller (L298N) code

23

  • Copy the template at the following link to your laptop and open it in VSCode
  • Follow the comments in the template to create the code for the project

24 of 48

Setup the Raspberry Pi

24

25 of 48

Motor Controller (L298N) code

25

  • Copy the tankDrive.py to your raspberry pi
  • Run the app
    • python3 tankDrive.py

26 of 48

Try Different Drive Mode

26

  • Now that you have tank drive working, try to update the code for one of the following
    • Arcade
    • Split Arcade

27 of 48

Add an Encoder

27

28 of 48

Correcting Bad Pulses

28

  • A capacitor was added to correct the “bad pulses”
    • 100nF (104) metallized polyester Capacitor was added
    • See the following for a detailed explanation:

https://androminarobot.blogspot.com/2016/07/en-este-tutorial-mostramos-como-usar-el.html

29 of 48

Encoder Wiring

29

You don’t have to use the wire colors in the photos - use what you have

30 of 48

Encoder Wiring

30

31 of 48

Encoder - Python

31

  • Copy the template at the following link to your laptop and open it in VSCode
  • This will be the beginning of our autonomous code
  • Follow the comments in the template to create the code for the project
  • Copy your auto.py to the raspberry pi

32 of 48

Encoder - Python

32

  • How did we get 2.55 ticks per inch?

TPI = ticks per inch

TPR = ticks per revolution = 20 (# slots on our encoder wheel)

C = circumference = 2𝝿r

𝝿 = 3.14159

r = wheel radius = 1.25 (wheel diameter is 2.5 inches)

TPI = TPR/C

TPI = 20/(2*3.14159*1.25)

TPI = 20/7.853975

TPI = 2.55

33 of 48

Encoder - Python

33

  • Run the app
    • python3 auto.py
  • Run it a few times measuring that it is driving the distance that you expect
    • How accurate is it?
    • Repeat for different distances
  • Extra credit - try to change the code to read in the distance to travel on the command line

34 of 48

Implementing PID

34

  • TBD

35 of 48

Add an Ultrasonic Range Finder

35

  • We will use the HC-SR04

36 of 48

HC-SR04

36

  • To be used to stop the robot once it gets too close to an object
  • Can be done with either Java or Python
    • Tutorials for both to follow
  • We will use the breadboard and will have to add resistors to step down the voltage received on the echo as it can be over the capacity of the GPIO pins

37 of 48

HC-SR04 Wiring

37

38 of 48

HC-SR04 Wiring

38

You don’t have to use the wire colors in the photos - use what you have

39 of 48

HC-SR04 - Java

39

  • <TBD>

40 of 48

HC-SR04 - Python

40

  • Copy the template at the following link to your laptop and open it in VSCode
  • Follow the comments in the template to create the code for the project
  • Copy your hc-sr04.py to the raspberry pi

41 of 48

HC-SR04 - Python

41

  • In order to use the python code you just created you will need to activate the piGPIO daemon
  • First, set it to start when the raspberry pi boots
    • Execute: sudo systemctl enable pigpiod
  • Start the daemon
    • Execute: sudo systemctl start pigpiod
  • Run the app in the background (“&” at the end)
    • python3 hc-sr04.py &
    • We run it in the background so that it runs all the time while also allowing us to run auto.py at the same time
    • Verify that you are seeing the distance value updating in the Outlineviewer

42 of 48

HC-SR04 - Python

42

  • Next, update auto.py to read the distance value from the networktables and do something with it
    • For example, stop if the robot is within a # of inches from an object

43 of 48

Vision

  • The next several slides will walk you through getting the …
    • Tools & software setup on the raspberry pi
    • Tools & software setup on your laptop
    • Python code to work with the camera
    • Object detection

43

44 of 48

Vision: Tools & software setup on rPi

  • TBD
  • USB vs PiCamera

44

45 of 48

Vision: Tools & software setup on Laptop

  • TBD

45

46 of 48

Vision: Python code to use camera

  • TBD

46

47 of 48

Vision: Object Detection

  • TBD

47

48 of 48

48

End