Repository navigation
Adding the possibility to drive a stepper motor #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 5.0.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| from typing import Union, List, Dict, Optional | ||
| from pymodaq.control_modules.move_utility_classes import ( | ||
| DAQ_Move_base, comon_parameters_fun, main, DataActuatorType, DataActuator | ||
| ) | ||
| from pymodaq_gui.parameter import Parameter | ||
| from pymodaq_plugins_arduino.hardware.arduino_telemetrix import Arduino | ||
| from pymodaq_plugins_arduino.utils import Config | ||
|
|
||
| config = Config() | ||
|
|
||
|
|
||
| class DAQ_Move_StepperMotor(DAQ_Move_base): | ||
| """Plugin to control stepper motor using Arduino controller and PyMoDAQ. | ||
| This object inherits all functionalities to communicate with PyMoDAQ’s DAQ_Move module through inheritance via | ||
| DAQ_Move_base. It makes a bridge between the DAQ_Move module and the Python wrapper of a particular instrument. | ||
| Use the arduino_telemetrix wrapper to communicate with the Arduino Board. | ||
| It may work with up to 4 axes depending on the configuration. | ||
| It does not consider the daisy chain option: only one controller. | ||
| Tested with Arduino Uno and one motor NEMA17 (1 axis). | ||
| PyMoDAQ version during the test was PyMoDAQ==5.0.5. | ||
| The operating system used was Windows 11. | ||
| Telemetrix4arduino has to be uploaded on the Arduino board. | ||
| Attributes | ||
| ---------- | ||
| controller: object | ||
| The particular object that allows communication with the hardware, in general, a Python wrapper around the | ||
| hardware library. | ||
| """ | ||
| _axis_names: Union[List[str], Dict[str, int]] = {'Stepper': config('stepper', 'name')} | ||
| _controller_units: Union[str, Dict[str, str]] = {'Stepper': 'count'} | ||
| _epsilons: Union[float, Dict[str, float]] = {'Stepper': 1} | ||
| data_actuator_type = DataActuatorType.DataActuator | ||
|
|
||
| params = [ | ||
| { | ||
| 'title': 'Ports:', | ||
| 'name': 'com_port', | ||
| 'type': 'list', | ||
| 'value': config('com_port'), | ||
| 'limits': Arduino.COM_PORTS, | ||
| } | ||
| ] + comon_parameters_fun(axis_names=_axis_names) | ||
|
|
||
| def ini_attributes(self): | ||
| self.controller: Optional[Arduino] = None | ||
|
|
||
| def get_actuator_value(self) -> DataActuator: | ||
| """Get the current position of the motor, in steps, with scaling conversion.""" | ||
| pos = DataActuator(data=self.controller.get_stepper_position(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this is some kind of internally stored number of counts? |
||
| units=self.axis_unit) | ||
| pos = self.get_position_with_scaling(pos) | ||
| return pos | ||
|
|
||
| def close(self): | ||
| """Terminate the communication with the Arduino.""" | ||
| if self.controller is not None: | ||
| self.controller.shutdown() | ||
|
|
||
| def commit_settings(self, param: Parameter): | ||
| """Apply the consequences of a change of value in the detector settings. | ||
| Parameters | ||
| ---------- | ||
| param: Parameter | ||
| A given parameter (within detector_settings) whose value has been changed by the user. | ||
| """ | ||
| pass | ||
|
|
||
| def ini_stage(self, controller=None): | ||
| """Actuator communication initialization. | ||
| Parameters | ||
| ---------- | ||
| controller: object | ||
| Custom object of a PyMoDAQ plugin (Slave case). None if only one actuator by controller (Master case). | ||
| Returns | ||
| ------- | ||
| info: str | ||
| initialized: bool | ||
| False if initialization failed otherwise True. | ||
| """ | ||
| self.ini_stage_init(slave_controller=controller) # Useful when controller is slave | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove that bit and use what you wrote below : |
||
|
|
||
| if self.is_master: # Needed when controller is master | ||
| self.controller = Arduino( | ||
| com_port=self.settings['com_port'] | ||
| ) | ||
| self.controller.initialize_stepper_motor( | ||
| config('stepper', 'pins', 'pul_pin'), | ||
| config('stepper', 'pins', 'dir_pin'), | ||
| config('stepper', 'pins', 'ena_pin'), | ||
| ) # Pulse, direction, enable pins | ||
|
|
||
| info = "Stepper motor connected with config file" | ||
| initialized = self.controller.stepper_motor is not None | ||
| return info, initialized | ||
|
|
||
| def move_abs(self, value: DataActuator): | ||
| """Move the motor to the absolute target defined by value.""" | ||
| value = self.check_bound(value) | ||
| self.target_value = value | ||
| value = self.set_position_with_scaling(value) | ||
| self.controller.move_stepper_to_position(value.units_as(self.axis_unit).value()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. faster to use directly value.value(self.axis_unit) |
||
|
|
||
| def move_rel(self, value: DataActuator): | ||
| """Move the motor by the relative amount defined by value.""" | ||
| self.move_abs(self.current_value + value) | ||
|
|
||
| def move_home(self): | ||
| """Move the motor back to its reference position (step 0).""" | ||
| home = DataActuator(data=0, units=self.axis_unit) | ||
| self.target_value = self.get_position_with_scaling(home) | ||
| self.controller.move_stepper_to_position(0) | ||
|
|
||
| def stop_motion(self): | ||
| """Stopping is not supported: moves are blocking until completion.""" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there no event mechanism in the telemetrix code? |
||
| pass | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main(__file__) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import numbers | ||
| from threading import Lock | ||
| from threading import Lock, Event | ||
|
|
||
| from pyvisa import ResourceManager | ||
| from telemetrix import telemetrix | ||
|
|
@@ -27,6 +27,8 @@ def __init__(self, *args, **kwargs): | |
| 3: 0, | ||
| 4: 0, | ||
| 5: 0} # Initialized dictionary for 6 analog channels | ||
| self.stepper_motor = None | ||
| self.position = 0 | ||
|
|
||
| @staticmethod | ||
| def round_value(value): | ||
|
|
@@ -96,6 +98,48 @@ def servo_move_degree(self, pin: int, value: float): | |
| self.pin_values_output[pin] = value | ||
| lock.release() | ||
|
|
||
| # Stepper motor methods | ||
| def initialize_stepper_motor(self, pulse_pin, direction_pin, enable_pin=7): | ||
| """ Initialize the stepper motor with the given pins """ | ||
| self.stepper_motor = self.set_pin_mode_stepper(interface=1, pin1=pulse_pin, pin2=direction_pin) | ||
| self.enable = enable_pin | ||
| self.set_pin_mode_digital_output(self.enable) # Set the enable pin as digital output | ||
| self.digital_write(self.enable, 1) # Disable the motor driver to avoid electrical consumption | ||
| self.stepper_set_current_position(self.stepper_motor, 0) # Set the current position to 0 | ||
|
|
||
| def move_stepper_to_position(self, position: float, max_speed=200, acceleration=400): | ||
| """ Move the stepper motor to the specified position """ | ||
| if self.stepper_motor is None: | ||
| raise ValueError("Stepper motor not initialized. Call initialize_stepper_motor first.") | ||
| self.stepper_set_max_speed(self.stepper_motor, max_speed) | ||
| self.stepper_set_acceleration(self.stepper_motor, acceleration) | ||
| self.stepper_move_to(self.stepper_motor, int(position)) | ||
| completion_event = Event() | ||
|
|
||
| def completion_callback(data): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could use that for the async move!! |
||
| """ Signal that the stepper motor has completed its movement """ | ||
| completion_event.set() | ||
|
|
||
| self.digital_write(self.enable, 0) | ||
| self.stepper_run(self.stepper_motor, completion_callback=completion_callback) | ||
| completion_event.wait() | ||
| self.digital_write(self.enable, 1) | ||
| return True | ||
|
|
||
| def get_stepper_position(self): | ||
| """ Retrieve the current position of the stepper motor """ | ||
| position_event = Event() | ||
|
|
||
| def position_callback(data): | ||
| """ Store the current position of the stepper motor """ | ||
| self.position = data[2] | ||
| position_event.set() | ||
|
|
||
| self.stepper_get_current_position(self.stepper_motor, | ||
| current_position_callback=position_callback) | ||
| position_event.wait() | ||
| return self.position | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| import time | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is count a valid pint unit?