03 Pan-Tilt Control and LED Light Control

From Waveshare Wiki
Jump to: navigation, search

Pan‑Tilt Control and LED Light Control

Pan‑Tilt Control

Basic Introduction

Products equipped with a pan‑tilt include two servos: a pan servo and a tilt servo. The pan servo controls horizontal rotation, with a range of ±180° (total 360°). The tilt servo controls vertical rotation, with a range of -45° to 90° (total 135°).

For products without a pan‑tilt, you can also add a pan‑tilt to the RaspRover yourself.

# Import library for chassis control
from base_ctrl import BaseController

# Function to detect the Raspberry Pi model
def is_raspberry_pi5():
    with open('/proc/cpuinfo', 'r') as file:
        for line in file:
            if 'Model' in line:
                if 'Raspberry Pi 5' in line:
                    return True
                else:
                    return False

# Determine the GPIO serial device name based on the Raspberry Pi model
if is_raspberry_pi5():
    base = BaseController('/dev/ttyAMA0', 115200)
else:
    base = BaseController('/dev/serial0', 115200)

In the code block above, we import and instantiate the library for chassis control. Next, we control the pan‑tilt motion by changing the angles of the pan and tilt servos.

Modify the values in the code below. In the following code:

  • input_x is the rotation angle of the pan servo; the pan axis has a range of ±180° (total 360°).
  • input_y is the rotation angle of the tilt servo; the tilt axis has a range of -45° to 90° (total 135°).
  • input_speed is the pan‑tilt rotation speed. When set to 0, the speed is fastest.
  • input_acc is the acceleration at the start and end of pan‑tilt rotation. The smaller the value, the smoother the start/stop. When set to 0, the maximum acceleration is used.

Run the following code. The pan‑tilt will move 45° to the right and 45° upward, then stop.

input_x = 45
input_y = 45
input_speed = 0
input_acc = 0

base.gimbal_ctrl(input_x, input_y, input_speed, input_acc)

Besides controlling the pan‑tilt by changing the servo angles, you can also directly command continuous pan‑tilt motion.

Modify the values in the code below. In the following code:

  • input_x controls the pan servo's rotation mode. When set to -1, the pan rotates continuously to the left (clockwise). When set to 1, it rotates continuously to the right (counter‑clockwise). When set to 0, it stops.
  • input_y controls the tilt servo's rotation mode. When set to -1, the tilt servo rotates continuously downward (looking down). When set to 1, it rotates continuously upward (looking up). When set to 0, it stops.
  • input_speed is the pan‑tilt rotation speed.

When both input_x and input_y are set to 2, the pan‑tilt automatically returns to its center position.

Run the following code. The pan‑tilt will move left until it reaches 180°, then stop.

input_x = -1
input_y = 0
input_speed = 0

base.gimbal_base_ctrl(input_x, input_y, input_speed)

Run the following code. The pan‑tilt will move upward until it reaches 90°, then stop.

input_x = 0
input_y = 1
input_speed = 0

base.gimbal_base_ctrl(input_x, input_y, input_speed)

Finally, run the following code to return the pan‑tilt to the center position.

input_x = 2
input_y = 2
input_speed = 0

base.gimbal_base_ctrl(input_x, input_y, input_speed)

LED Light Control

Basic Introduction

For WAVE ROVER and UGV series products, the driver board integrates two 12V switch control interfaces (the actual maximum voltage varies with battery voltage). They are controlled by GPIO4 and GPIO5 of the ESP32 via MOSFETs. Each line has two connectors, giving a total of four 12V switch control interfaces. According to the default assembly, GPIO4 controls the chassis headlights (WAVE ROVER has no chassis headlights), and GPIO5 controls the front light. You can send corresponding commands to the lower computer to turn these two interfaces on/off and control the output voltage (however, due to the inherent delay of MOSFET control, the PWM output from the ESP32 is not linearly related to the actual output voltage).

For products without LED lights, you can expand LEDs with a withstand voltage of 12.6V on these two 12V switch control interfaces (generally, 12V is acceptable because, for safety and battery protection, the product's UPS will not charge the battery above 12V). You can also expand other peripherals on the remaining switch control interfaces – for example, a 12V water‑gel blaster gearbox connected to the interface controlled by IO5 to achieve automatic aiming and firing.

To run the code in a code block, select the code block you want to run and press Ctrl+Enter. If you are using JupyterLab on a mobile phone or tablet, you can press the triangular play button at the top to run the code block.

In the code block above, we import and instantiate the chassis control library. Next, we control the switch of the IO4 interface. The variable IO4_PWM is the PWM output from GPIO4 of the ESP32, ranging from 0 to 255. When this variable is 0, the IO4‑controlled interface is off; when it is 255, the output voltage of the IO4‑controlled interface is close to the UPS battery voltage (the voltage of three 18650 lithium batteries connected in series).

Run the following code block to turn on the IO4‑controlled interface (illuminating the chassis headlights). Note: WAVE ROVER does not have chassis headlights, so running the following code block will have no effect. You need to run the next code block (for the front light) to turn on the headlight, which is located on the camera pan‑tilt. If the product does not come with a camera pan‑tilt, there is no front light.

IO4_PWM = 255
IO5_PWM = 0

base.lights_ctrl(IO4_PWM, IO5_PWM)

Run the following code block to turn on the IO5‑controlled interface (illuminating the pan‑tilt headlight). Note: If the product does not come with a camera pan‑tilt, there is no headlight.

IO4_PWM = 255
IO5_PWM = 255

base.lights_ctrl(IO4_PWM, IO5_PWM)

If your product is equipped with LED lights, they should all be on by now. You can run the following code block to dim the LEDs.

IO4_PWM = 64
IO5_PWM = 64

base.lights_ctrl(IO4_PWM, IO5_PWM)

Finally, run the following code block to turn off the LEDs.

base.lights_ctrl(0, 0)

Here, run a code block that integrates pan‑tilt functionality.

import time
base.gimbal_ctrl(0, 0, 0, 0)
base.lights_ctrl(255, 255)
time.sleep(0.3)
base.gimbal_ctrl(45, 0, 0, 0)
base.lights_ctrl(0, 0)
time.sleep(0.3)
base.gimbal_ctrl(-45, 0, 0, 0)
base.lights_ctrl(255, 255)
time.sleep(0.3)
base.gimbal_ctrl(0, 90, 0, 0)
base.lights_ctrl(0, 0)
time.sleep(0.3)
base.gimbal_ctrl(0, 0, 0, 0)

How to Use Host Device