The following code was used to move a rover in Barcelona using a glove located at the Edgerton Center during the EDW 2025. This code converts the values sent to Arduino Cloud into a json that the rover control system understands, sending it using a websocket.
The rover onboard software is a combination of an API + Website, built in Flask and the Operating system built in ROS 2 Humble.
By sending a json file using a websocket to the API, the rover operating system will receive this data and act accordingly.
Due to a NDA, I cannot provide access the code. But if interested in more details,
please contact me at joel.garcia1202@hotmail.com and I'll see what I can do.
"""
import time
import logging
import sys
import socketio
from arduino_iot_cloud import ArduinoCloudClient
sio = socketio.SimpleClient()
# The IP below is the local IP for the rover onboard computer.
# This could be changed to any IP that is running the onboard server.
HOST = "192.168.3.11"
PORT = 80
sio.connect(f"http://{HOST}:{PORT}")
print('my sid is', sio.sid)
sys.path.append("lib")
# These values below are how the code knows where the data is in Arduino Cloud. You can find them by creating a "Thing"
# in arduino cloud and selecting Manual Device.
DEVICE_ID = b"SECRET"
SECRET_KEY = b"SECRET"
# This Dictionary is what needs to be sent to the rover for it to act.
controller_data = {
"mode": 0, # The mode represents one of several channels. 0 Means you want to move the wheels, 1 is for the robotic arm and 2 is for testing purposes.
"buttons": { # These are the buttons in the controller we usually use to control the rover. Most of them do something.
"0": 0,
"1": 0,
"2": 0,
"3": 0,
"4": 0,
"5": 0,
"6": 0,
"7": 0,
"8": 0,
"9": 0,
"10": 0,
"11": 0,
"12": 0,
"13": 0,
"14": 0,
"15": 0,
"16": 0,
"17": 0
},
"axes": [ # These are the values we need to update to move the rover, as these represent the Joysticks
0.0, # This one only does some stuff in some specific modes of operation. Like Crab or turtle mode.
0.0, # Forward and backward motion (We will Use)
0.0, # Left and right Motion (We will Use)
0.0 # This one only does some stuff in some specific modes of operation. Like Crab or turtle mode.
]
}
# For debugging purposes. You can ignore this.
def logging_func():
logging.basicConfig(
datefmt="%H:%M:%S",
format="%(asctime)s.%(msecs)03d %(message)s",
level=logging.INFO,
)
# Here now come the functions that will be executed every time a value received from the glove changes.
# If the Index finger moves:
def IndexVal_callback(client, value):
# we set the received value (we made these either -1, 0 or 1) as if it were the Joystick axis
controller_data["axes"][2] = value
print("IndexVal is: ", value) # Printing it for debugging purposes
send_data()
def ThreeFingVal_Callback(client, value):
# we set the received value (we made these either -1, 0 or 1) as if it were the Joystick axis
controller_data["axes"][1] = value
print("ThreeFingVal is: ", value) # Printing it for debugging purposes
send_data()
def send_data():
# Sending the data is just sending the lastest version of the dictionary through the websocket.
sio.emit("controller", data=controller_data)
print(controller_data) # Printing it for debugging purposes
# When we run the script, we execute this:
if __name__ == "__main__":
logging_func() # debugging stuff
# Try to connect to the arduino cloud, set the functions to be executed when the fingers move and if it fails, just
# restart the connection.
while True:
try:
client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY)
client.register("IndexVal", value=None, on_write=IndexVal_callback)
client.register("ThreeFingVal", value=None, on_write=ThreeFingVal_Callback)
client.start()
except Exception as e:
logging.error(f"An error occurred: {e}")
logging.info("Retrying connection in 10 seconds...")
time.sleep(10)
No comments:
Post a Comment