mirror of
https://github.com/odriverobotics/ODrive.git
synced 2026-09-20 22:55:00 +08:00
fix odrive_demo.py
This commit is contained in:
@@ -103,7 +103,8 @@ def find_all(path, serial_number,
|
||||
the_rest = ':'.join(search_spec.split(':')[1:])
|
||||
if prefix in channel_types:
|
||||
threading.Thread(target=channel_types[prefix],
|
||||
args=(the_rest, serial_number, did_discover_channel, search_cancellation_token, channel_termination_token, logger)).start()
|
||||
args=(the_rest, serial_number, did_discover_channel, search_cancellation_token, channel_termination_token, logger),
|
||||
daemon=True).start()
|
||||
else:
|
||||
raise Exception("Invalid path spec \"{}\"".format(search_spec))
|
||||
|
||||
|
||||
@@ -256,7 +256,7 @@ class Channel(PacketSink):
|
||||
self._logger.debug("receiver thread is exiting: " + traceback.format_exc())
|
||||
finally:
|
||||
self._channel_broken.set()
|
||||
threading.Thread(target=receiver_thread).start()
|
||||
threading.Thread(target=receiver_thread, daemon=True).start()
|
||||
|
||||
def remote_endpoint_operation(self, endpoint_id, input, expect_ack, output_length):
|
||||
if input is None:
|
||||
|
||||
+2
-2
@@ -206,7 +206,7 @@ def show_deferred_message(message, cancellation_token):
|
||||
time.sleep(1)
|
||||
if not cancellation_token.is_set():
|
||||
print(message)
|
||||
t = threading.Thread(target=show_message_thread, args=(message, cancellation_token))
|
||||
t = threading.Thread(target=show_message_thread, args=(message, cancellation_token), daemon=True)
|
||||
t.daemon = True
|
||||
t.start()
|
||||
|
||||
@@ -436,7 +436,7 @@ def launch_dfu(args, logger, cancellation_token):
|
||||
def find_device_in_dfu_mode_thread():
|
||||
devices[0] = find_device_in_dfu_mode(serial_number, find_odrive_cancellation_token)
|
||||
find_odrive_cancellation_token.set()
|
||||
threading.Thread(target=find_device_in_dfu_mode_thread).start()
|
||||
threading.Thread(target=find_device_in_dfu_mode_thread, daemon=True).start()
|
||||
|
||||
# Scan for ODrives not in DFU mode
|
||||
# We only scan on USB because DFU is only implemented over USB
|
||||
|
||||
@@ -72,8 +72,10 @@ def start_liveplotter(get_var_callback):
|
||||
fig.canvas.draw()
|
||||
fig.canvas.start_event_loop(1/plot_rate)
|
||||
|
||||
threading.Thread(target=fetch_data).start()
|
||||
threading.Thread(target=plot_data).start()
|
||||
threading.Thread(target=fetch_data, daemon=True).start()
|
||||
threading.Thread(target=plot_data, daemon=True).start()
|
||||
|
||||
return cancellation_token;
|
||||
#plot_data()
|
||||
|
||||
def print_drv_regs(name, motor):
|
||||
@@ -142,7 +144,7 @@ def usb_burn_in_test(get_var_callback, cancellation_token):
|
||||
continue
|
||||
if i % 1000 == 0:
|
||||
print("read {} values".format(i))
|
||||
threading.Thread(target=fetch_data).start()
|
||||
threading.Thread(target=fetch_data, daemon=True).start()
|
||||
|
||||
def setup_udev_rules(logger):
|
||||
if platform.system() != 'Linux':
|
||||
|
||||
+13
-3
@@ -6,15 +6,25 @@ Example usage of the ODrive python library to monitor and control ODrive devices
|
||||
from __future__ import print_function
|
||||
|
||||
import odrive
|
||||
from odrive.enums import *
|
||||
import time
|
||||
import math
|
||||
|
||||
# Find a connected ODrive (this will block until you connect one)
|
||||
print("finding an odrive...")
|
||||
my_drive = odrive.find_any()
|
||||
|
||||
# Find an ODrive that is connected on the serial port /dev/ttyUSB0
|
||||
#my_drive = odrive.find_any("serial:/dev/ttyUSB0")
|
||||
|
||||
# Calibrate motor and wait for it to finish
|
||||
print("starting calibration...")
|
||||
my_drive.axis0.requested_state = AXIS_STATE_FULL_CALIBRATION_SEQUENCE
|
||||
while my_drive.axis0.current_state != AXIS_STATE_IDLE:
|
||||
time.sleep(0.1)
|
||||
|
||||
my_drive.axis0.requested_state = AXIS_STATE_CLOSED_LOOP_CONTROL
|
||||
|
||||
# To read a value, simply read the property
|
||||
print("Bus voltage is " + str(my_drive.vbus_voltage) + "V")
|
||||
|
||||
@@ -23,17 +33,17 @@ my_drive.axis0.controller.pos_setpoint = 3.14
|
||||
print("Position setpoint is " + str(my_drive.axis0.controller.pos_setpoint))
|
||||
|
||||
# And this is how function calls are done:
|
||||
my_drive.axis0.controller.set_pos_setpoint(0.0, 0.0, 0.0)
|
||||
for i in [1,2,3,4]:
|
||||
print('voltage on GPIO{} is {} Volt'.format(i, my_drive.get_adc_voltage(i)))
|
||||
|
||||
# A sine wave to test
|
||||
t0 = time.monotonic()
|
||||
while True:
|
||||
setpoint = 10000.0 * math.sin((time.monotonic() - t0)*2)
|
||||
print("goto " + str(int(setpoint)))
|
||||
my_drive.axis0.controller.set_pos_setpoint(setpoint, 0.0, 0.0)
|
||||
my_drive.axis0.controller.pos_setpoint = setpoint
|
||||
time.sleep(0.01)
|
||||
|
||||
|
||||
# Some more things you can try:
|
||||
|
||||
# Write to a read-only property:
|
||||
|
||||
+10
-3
@@ -7,6 +7,7 @@ from __future__ import print_function
|
||||
import sys
|
||||
import os
|
||||
import argparse
|
||||
import time
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(
|
||||
os.path.realpath(__file__))),
|
||||
@@ -14,7 +15,7 @@ sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(
|
||||
import fibre.discovery
|
||||
from fibre import Logger, Event
|
||||
import odrive
|
||||
from odrive.utils import OperationAbortedException
|
||||
from odrive.utils import OperationAbortedException, decode_temp
|
||||
from odrive.configuration import *
|
||||
|
||||
# Flush stdout by default
|
||||
@@ -149,8 +150,14 @@ try:
|
||||
|
||||
# If you want to plot different values, change them here.
|
||||
# You can plot any number of values concurrently.
|
||||
start_liveplotter(lambda: [my_odrive.motor0.encoder.pos_estimate,
|
||||
my_odrive.motor1.encoder.pos_estimate])
|
||||
cancellation_token = start_liveplotter(lambda: [
|
||||
my_odrive.axis0.encoder.pos_estimate,
|
||||
my_odrive.axis1.encoder.pos_estimate,
|
||||
])
|
||||
|
||||
print("Showing plot. Press Ctrl+C to exit.")
|
||||
while not cancellation_token.is_set():
|
||||
time.sleep(1)
|
||||
|
||||
elif args.command == 'drv-status':
|
||||
from odrive.utils import print_drv_regs
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ def for_all_parallel(objects, get_name, callback):
|
||||
# Start a thread for each element in the list
|
||||
all_threads = []
|
||||
for element in objects:
|
||||
thread = threading.Thread(target=run_callback, args=(element,))
|
||||
thread = threading.Thread(target=run_callback, args=(element,), daemon=True)
|
||||
thread.start()
|
||||
all_threads.append(thread)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user