-
Notifications
You must be signed in to change notification settings - Fork 4
Comm. Timing + PWM mode + examples #32
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
Merged
HanaeRateau
merged 5 commits into
SofaComplianceRobotics:main
from
akruszew:voltage_control
Apr 1, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
724d6ca
Communication Timing improvement + PWM mode + example + Changed pid e…
akruszew 8d808df
Update emioapi/_motorgroup.py
akruszew d94df75
Update emioapi/_motorgroup.py
akruszew 55338b7
Update emioapi/_motorgroup.py
akruszew cd19bf9
Adds matplotlib as dev dependency to run some of the examples and min…
HanaeRateau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| #!/usr/bin/env -S uv run --script | ||
|
|
||
| import time | ||
| import logging | ||
| import os | ||
| import sys | ||
| import matplotlib | ||
| matplotlib.use("TkAgg") | ||
| import matplotlib.pyplot as plt | ||
| import numpy as np | ||
| sys.path.append(os.path.dirname(os.path.realpath(__file__))+'/..') | ||
| from emioapi import EmioMotors | ||
| from emioapi._logging_config import logger | ||
|
|
||
| ''' | ||
| This example demonstrates how to use the EMIO API to control DYNAMIXEL motors in PWM mode. | ||
| ''' | ||
|
|
||
| def main(emio: EmioMotors, loops=500, motor_id=1): | ||
| ''' | ||
| Main function to run the PWM test. | ||
| Parameters: | ||
| - emio: An instance of the EmioMotors class. | ||
| - loops: Number of iterations to run the test. | ||
| - motor_id: ID of the motor to test (0-3). | ||
| ''' | ||
| # Enable PWM mode | ||
| emio.enablePWMMode() | ||
| time.sleep(1) | ||
| emio.printStatus() | ||
|
|
||
| # Intialize lists to store data for plotting | ||
| start = time.perf_counter() | ||
| last = start | ||
| dt = [] | ||
| speed = [] | ||
| pwm = [] | ||
| t = [] | ||
| pwm_value = [0]*4 | ||
|
|
||
| # Loop to set PWM values and read velocity | ||
| for i in range(loops): | ||
| # Record loop time and elapsed time | ||
| new = time.perf_counter() | ||
| dt.append(new-last) | ||
| t.append(new-start) | ||
| last = new | ||
|
|
||
| # Alternate PWM values for the first half and second half of the loops | ||
| if i<loops/2: | ||
| pwm_value[motor_id] = 200 | ||
| else: | ||
| pwm_value[motor_id] = 400 | ||
|
|
||
| emio.goal_pwm = pwm_value | ||
| pwm.append(pwm_value[motor_id]) | ||
|
|
||
| velocity = emio.velocity | ||
| speed.append(velocity[motor_id]) | ||
|
|
||
|
|
||
| emio.goal_pwm=[0]*4 | ||
| logger.info("PWM test completed. Plotting results...") | ||
| logger.info(f"Average loop time: {np.mean(dt):.4f} seconds") | ||
| logger.info("Plotting PWM and velocity over time...") | ||
|
|
||
| fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True) | ||
| ax1.plot(t, pwm, '-+') | ||
| ax1.legend(["pwm"]) | ||
| ax2.plot(t, speed, '-+') | ||
| ax2.legend(["velocity"]) | ||
| plt.show() | ||
|
|
||
| if __name__ == "__main__": | ||
| emio_motors = None | ||
| try: | ||
| logger.info("Starting EMIO API test...") | ||
| logger.info("Opening and configuring EMIO API...") | ||
| emio_motors = EmioMotors() | ||
|
|
||
| if emio_motors.open(): | ||
|
|
||
| emio_motors.printStatus() | ||
|
|
||
| logger.info("Emio motors opened and configured.") | ||
| logger.info("Running main function...") | ||
| main(emio_motors, 500,1) | ||
|
|
||
| logger.info("Main function completed.") | ||
| logger.info("Closing Emio motor connection...") | ||
|
|
||
| emio_motors.close() | ||
| logger.info("Emio connection closed.") | ||
| except KeyboardInterrupt: | ||
| if emio_motors: | ||
| emio_motors.goal_pwm=[0]*4 | ||
| plt.close("all") | ||
| emio_motors.close() | ||
| except Exception as e: | ||
| logger.exception(f"An error occurred: {e}") | ||
| if emio_motors: | ||
| emio_motors.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,3 +72,8 @@ Class = 2 | |
| Method = 3 | ||
| Function = 3 | ||
| Data = 3 | ||
|
|
||
| [dependency-groups] | ||
| dev = [ | ||
| "matplotlib>=3.10.8", | ||
| ] | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.