From 0abcbee2212c5848ea6e08aae4d55765b6664f0e Mon Sep 17 00:00:00 2001 From: PIERROOOTT Date: Mon, 5 Feb 2024 13:15:04 +0100 Subject: [PATCH] feat: add python code example to move the motor after hitting a switch --- examples/back_switch.py | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 examples/back_switch.py diff --git a/examples/back_switch.py b/examples/back_switch.py new file mode 100644 index 0000000..7cf7f97 --- /dev/null +++ b/examples/back_switch.py @@ -0,0 +1,52 @@ +import crappy +import tkinter as tk + +if __name__ == '__main__': + + open_switch = 0 + + mot = crappy.actuator.Phidget4AStepper( + steps_per_mm=2500, + current_limit=3, + max_acceleration=20, + remote=True, + switch_ports=(5, 6)) + + mot._check_switch = False + mot.open() + for switch in mot._switches: + if switch.getState() is False: + open_switch = switch.getHubPort() + + + def gen_speed(value): + mot.set_speed(float(value)) + + + # Creates the graphical interface + root = tk.Tk() + root.title("Motor speed") + + if open_switch == 5: + # Create the slider + slider = tk.Scale(root, + from_=0, + to=2, + orient=tk.HORIZONTAL, + length=200, + resolution=0.1, + command=gen_speed) + elif open_switch == 6: + slider = tk.Scale(root, + from_=-2, + to=0, + orient=tk.HORIZONTAL, + length=200, + resolution=0.1, + command=gen_speed) + else: + raise ValueError(f"No switch has been hit or disconnected") + + slider.pack(pady=20) + + root.mainloop()