I am trying to collect data from the manual control using the gamepad that came with the jet racer kit from Waveshare. What I would like to do is display what the throttle and steering values are live when using the gamepad but also save this data with a timestamp so I can use this with the recorded images dataset. Then what I would have is the image, steering and throttle data for any given time.
My python is a little rusty and I don’t seem to find a way to do this! I can record and save the images just not the data from the gamepad. Any help or pointer would be great.
Which Ones ? I am using code from Waveshare plus my own. I am just needing a way of capturing the gamepad data for the throttle and steering probably a loop
One method would be to attach callback function to the joystick axes, or other attributes like the motor values.
These callbacks could use the python time library “time.monotonoc()” to get a monotonic timestamp. Inside these callbacks you could add these samples to say a running CSV or JSON file, depending on the format you desire.
This would save the samples asynchronously.
Please let me know if this helps or you have any questions.
Say you wanted to record the left and right motor values, asynchronously. You could do the following
import time
def timestamp():
return int(time.monotonic() * 1e9) # timestamp in nanoseconds
def write_sample(filename, t, value):
# open with 'a' to append to file
with open(filename, 'a') as f:
f.write("%d,%f\n" % (t, value)) # write timestamp,value CSV line
def handle_left_motor(change):
# called when left motor value changes (see .observe below)
value = change['new']
write_sample("left_motor.csv", timestamp(), value)
def handle_right_motor(change):
# called when right motor value changes (see .observe below)
value = change['new']
write_sample("right_motor.csv", timestamp(), value)
robot.left_motor.observe(handle_left_motor, names='value')
robot.right_motor.observe(handle_right_motor, names='value')
With this code, whenever the motor values change, the value will by dumped to a corresponding CSV file.
This may not be the ideal way to precisely log the values, but is simple and may work perfect depending on your purposes.
Please let me know if this helps or you run into issues. (as a disclaimer, I haven’t tested/executed the above code)