Robotics-II-Circuit-Python

Circuit Python tutorials in Robotics II

View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python

Table of Contents

Reading a FlySky FS-i6x joystick

The RC signal is read from specific pins on a microcontroller, and the code then outputs the duty cycle value of the signal. We’ll first learn how to read our x2 joystick values.


Download the py file


"""Basic RC Analog input Control
Outputs an analog channel duty cycle rating in 0>100.
50 is the deadpoint. 0 is miniumum, 100 is maximum.

"""

import time
import board
from rc import RCReceiver

rc = RCReceiver(ch1=board.D10, ch2=board.D11, ch3=None, ch4=None, ch5=board.D12, ch6=board.D13)

# Main code
while True:
    # Read channels
    channel_1 = rc.read_channel(1)
    channel_2 = rc.read_channel(2)
    if channel_1 is not None and channel_2 is not None: # must not be None to do something with the output
        print("1: ", channel_1, "2: ", channel_2)
    
    rc.ensure_cycle()  # Maintains sync with our 20ms cycle every loop iteration


Code Breakdown

Libraries

"""
Basic RC Analog input Control
Outputs an analog channel duty cycle rating in 0 > 100.
50 is the deadpoint. 0 is miniumum, 100 is maximum.
"""
import time
import board
from rc import RCReceiver

Initializing the RCReceiver instance

The next section of the code initializes the RC receiver channels.


rc = RCReceiver(ch1=board.D10, ch2=board.D11, ch3=None, ch4=None, ch5=board.D12, ch6=board.D13)

Main Code Loop

This is the main part of our script where we continuously read and print the values from the RC receiver.


# Main code
while True:
    # Read channels
    channel_1 = rc.read_channel(1)
    channel_2 = rc.read_channel(2)
    if channel_1 is not None and channel_2 is not None: # must not be None to do something with the output
        print("1: ", channel_1, "2: ", channel_2)
    rc.ensure_cycle()  # Maintains sync with our 20ms cycle every loop iteration


Return to FlySky Tutorials