Skip to content

Commit

Permalink
docs: add commentaries and code description
Browse files Browse the repository at this point in the history
  • Loading branch information
PIERROOOTT committed Feb 5, 2024
1 parent 0abcbee commit 252c045
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions examples/back_switch.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,59 @@
"""
This example demonstrates how to move the motor after hitting a switch.
It also shows the use of Crappy Blocks outside a Crappy loop.
In this example, a Tkinter slider controlled by the user sends command to the
Actuator Block. The slider sends speed command to drive the motor here.
The user can only drive the motor in the opposite direction of the switch that
has been hit.
The test ends when the slider window is closed.
"""

import crappy
import tkinter as tk

if __name__ == '__main__':

open_switch = 0

# This Actuator drives a Phidget4AStepper in speed.
mot = crappy.actuator.Phidget4AStepper(
steps_per_mm=2500,
current_limit=3,
max_acceleration=20,
remote=True,
switch_ports=(5, 6))
steps_per_mm=2500, # Number of steps necessary to move by 1 mm.
current_limit=3, # Maximum current the driver is allowed to deliver
# to the motor, in A.
max_acceleration=20, # Maximum acceleration the motor is allowed to
# reach in mm/s².
remote=True, # True if connected to a wireless VINT Hub, False if
# connected to a USB VINT Hub.
switch_ports=(5, 6) # Port numbers of the VINT Hub where the
# switches are connected.
)

# Remove the automatic check of the state of the switches
mot._check_switch = False

# Open the Actuator
mot.open()

# Find the switch that has been hit
for switch in mot._switches:
if switch.getState() is False:
open_switch = switch.getHubPort()


def gen_speed(value):
def gen_speed(value) -> None:
"""Callback sending the speed command to the driver when the user update
the slider value. """

mot.set_speed(float(value))


# Creates the graphical interface
# Create the graphical interface
root = tk.Tk()
root.title("Motor speed")

# Create the slider associate to the switch hit
if open_switch == 5:
# Create the slider
slider = tk.Scale(root,
Expand Down

0 comments on commit 252c045

Please sign in to comment.