From eb07260c3ea57e74c59432fd036b275b608d85d0 Mon Sep 17 00:00:00 2001 From: Oskar Weigl Date: Sun, 1 Jul 2018 00:22:03 -0700 Subject: [PATCH] fix odrive_demo.py --- Firmware/fibre/python/fibre/discovery.py | 3 ++- Firmware/fibre/python/fibre/protocol.py | 2 +- tools/odrive/dfu.py | 4 ++-- tools/odrive/utils.py | 8 +++++--- tools/odrive_demo.py | 16 +++++++++++++--- tools/odrivetool | 13 ++++++++++--- tools/run_tests.py | 2 +- 7 files changed, 34 insertions(+), 14 deletions(-) diff --git a/Firmware/fibre/python/fibre/discovery.py b/Firmware/fibre/python/fibre/discovery.py index 4f1eda77..f1e62499 100644 --- a/Firmware/fibre/python/fibre/discovery.py +++ b/Firmware/fibre/python/fibre/discovery.py @@ -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)) diff --git a/Firmware/fibre/python/fibre/protocol.py b/Firmware/fibre/python/fibre/protocol.py index 327dcca0..50053a11 100644 --- a/Firmware/fibre/python/fibre/protocol.py +++ b/Firmware/fibre/python/fibre/protocol.py @@ -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: diff --git a/tools/odrive/dfu.py b/tools/odrive/dfu.py index 40f44065..cf81fba6 100755 --- a/tools/odrive/dfu.py +++ b/tools/odrive/dfu.py @@ -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 diff --git a/tools/odrive/utils.py b/tools/odrive/utils.py index 0f5c58b8..57effc4f 100755 --- a/tools/odrive/utils.py +++ b/tools/odrive/utils.py @@ -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': diff --git a/tools/odrive_demo.py b/tools/odrive_demo.py index 0e418495..6a710c19 100755 --- a/tools/odrive_demo.py +++ b/tools/odrive_demo.py @@ -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: diff --git a/tools/odrivetool b/tools/odrivetool index 604856fc..c3b2f415 100755 --- a/tools/odrivetool +++ b/tools/odrivetool @@ -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 diff --git a/tools/run_tests.py b/tools/run_tests.py index 3ba2e5ab..2e1359d6 100755 --- a/tools/run_tests.py +++ b/tools/run_tests.py @@ -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)