-
Notifications
You must be signed in to change notification settings - Fork 0
Position_Controlled_Motors and friends #35
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
Open
xxAtrain223
wants to merge
5
commits into
dev
Choose a base branch
from
anthony/dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5e33b2b
Changes to make VelocityControlledMotor work
a1e0ba3
Removed vpid from pid.py, refactored pid
7b11471
Fixes for i2c encoders
73e3a34
Added position_controlled_motor and vpid
6073c57
Merge branch 'dev' into anthony/dev
xxAtrain223 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| from .component import Component | ||
| from ...units import * | ||
|
|
||
|
|
||
| class PositionControlledMotor(Component): | ||
| SET_VOLTAGE = "kSetPCMVoltage" | ||
| STOP = "kStopPCM" | ||
| POSITION = "kGetPCMPosition" | ||
| POSITION_RESULT = "kGetPCMPositionResult" | ||
| SET = "kSetPCMPosition" | ||
|
|
||
| def __init__(self, spine, devname, config, commands, sim): | ||
| self.spine = spine | ||
| self.devname = devname | ||
| self.label = config['label'] | ||
| self.index = config['index'] | ||
| self.sim = sim | ||
| self.motor = config['motor'] | ||
| self.encoder = config['encoder'] | ||
| self.pid = config['pid'] | ||
|
|
||
| if self.sim: | ||
| self.sim_position = Constant(0) | ||
| if not self.sim: | ||
| self.setVoltageIndex = commands[self.SET_VOLTAGE] | ||
| self.stopIndex = commands[self.STOP] | ||
| self.positionIndex = commands[self.POSITION] | ||
| self.positionResultIndex = commands[self.POSITION_RESULT] | ||
| self.setIndex = commands[self.SET] | ||
|
|
||
| def get_command_parameters(self): | ||
| yield self.setVoltageIndex, [self.SET_VOLTAGE, "ii"] | ||
| yield self.stopIndex, [self.STOP, "i"] | ||
| yield self.positionIndex, [self.POSITION, "i"] | ||
| yield self.positionResultIndex, [self.POSITION_RESULT, "d"] | ||
| yield self.setIndex, [self.SET, "id"] | ||
|
|
||
| def set_voltage(self, value): | ||
| if self.sim: | ||
| # TODO: update sim_velocity based on sim_motor | ||
| return | ||
|
|
||
| self.spine.send(self.devname, False, self.SET_VOLTAGE, self.index, value) | ||
|
|
||
| def stop(self): | ||
| if self.sim: | ||
| self.sim_velocity = Constant(0) | ||
| return | ||
|
|
||
| self.spine.send(self.devname, False, self.STOP, self.index) | ||
|
|
||
| def get_position(self): | ||
| if self.sim: | ||
| return self.sim_position | ||
|
|
||
| response = self.spine.send(self.devname, True, self.POSITION, self.index) | ||
| response = Angle(response[0], Angle.rev) | ||
| return response | ||
|
|
||
| def set_position(self, position): | ||
| if self.sim: | ||
| self.sim_position = position | ||
| return | ||
|
|
||
| self.spine.send(self.devname, False, self.SET, self.index, position.to(Angle.degree)) | ||
|
|
||
| def get_dependency_update(self): | ||
| dependencies = {} | ||
| dependencies[self.motor]['sim_velocity'] = self.sim_velocity | ||
| dependencies[self.encoder]['sim_velocity'] = self.sim_velocity | ||
| return dependencies | ||
|
|
||
| def sim_update(self, tm_diff): | ||
| self.sim_position += self.sim_velocity * tm_diff | ||
|
|
||
| def get_hal_data(self): | ||
| hal_data = {} | ||
| hal_data['velocity'] = self.sim_velocity | ||
| hal_data['position'] = self.sim_position | ||
| return hal_data |
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,108 @@ | ||
| from .component import Component | ||
|
|
||
|
|
||
| class Pid(Component): | ||
| PID_CONSTANTS = "kPidConstants" | ||
| PID_CONSTANTS_RESULT = "kPidConstantsResult" | ||
| PID_MODIFY_CONSTANTS = "kModifyPidConstants" | ||
| PID_SET = "kSetPidSetpoint" | ||
| PID_OFF = "kPidOff" | ||
| PID_DISPLAY = "kPidDisplay" | ||
| PID_DISPLAY_RESULT = "kPidDisplayResult" | ||
|
|
||
| VPID_CONSTANTS = "kVPidConstants" | ||
| VPID_CONSTANTS_RESULT = "kVPidConstantsResult" | ||
| VPID_MODIFY_CONSTANTS = "kModifyVpidConstants" | ||
| VPID_SET = "kSetVpidSetpoint" | ||
| VPID_OFF = "kVpidOff" | ||
| VPID_DISPLAY = "kVpidDisplay" | ||
| VPID_DISPLAY_RESULT = "kVpidDisplayResult" | ||
|
|
||
| def __init__(self, spine, devname, config, commands, sim): | ||
| self.spine = spine | ||
| self.devname = devname | ||
| self.label = config['label'] | ||
| self.index = config['index'] | ||
| self.vpid = config['vpid'] | ||
| self.sim = sim | ||
|
|
||
| if not self.sim: | ||
| if not self.vpid: | ||
| self.constantsIndex = commands[self.PID_CONSTANTS] | ||
| self.constantsResultIndex = commands[self.PID_CONSTANTS_RESULT] | ||
| self.modifyConstantsIndex = commands[self.PID_MODIFY_CONSTANTS] | ||
| self.setIndex = commands[self.PID_SET] | ||
| self.offIndex = commands[self.PID_OFF] | ||
| self.displayIndex = commands[self.PID_DISPLAY] | ||
| self.displayResultIndex = commands[self.PID_DISPLAY_RESULT] | ||
|
|
||
| self.CONSTANTS = self.PID_CONSTANTS | ||
| self.CONSTANTS_RESULT = self.PID_CONSTANTS_RESULT | ||
| self.MODIFY_CONSTANTS = self.PID_MODIFY_CONSTANTS | ||
| self.SET = self.PID_SET | ||
| self.OFF = self.PID_OFF | ||
| self.DISPLAY = self.PID_DISPLAY | ||
| self.DISPLAY_RESULT = self.PID_DISPLAY_RESULT | ||
| else: | ||
| self.constantsIndex = commands[self.VPID_CONSTANTS] | ||
| self.constantsResultIndex = commands[self.VPID_CONSTANTS_RESULT] | ||
| self.modifyConstantsIndex = commands[self.VPID_MODIFY_CONSTANTS] | ||
| self.setIndex = commands[self.VPID_SET] | ||
| self.offIndex = commands[self.VPID_OFF] | ||
| self.displayIndex = commands[self.VPID_DISPLAY] | ||
| self.displayResultIndex = commands[self.VPID_DISPLAY_RESULT] | ||
|
|
||
| self.CONSTANTS = self.VPID_CONSTANTS | ||
| self.CONSTANTS_RESULT = self.VPID_CONSTANTS_RESULT | ||
| self.MODIFY_CONSTANTS = self.VPID_MODIFY_CONSTANTS | ||
| self.SET = self.VPID_SET | ||
| self.OFF = self.VPID_OFF | ||
| self.DISPLAY = self.VPID_DISPLAY | ||
| self.DISPLAY_RESULT = self.VPID_DISPLAY_RESULT | ||
|
|
||
| def get_command_parameters(self): | ||
| yield self.constantsIndex, [self.CONSTANTS, "i"] | ||
| yield self.constantsResultIndex, [self.CONSTANTS, "iddd"] | ||
| yield self.modifyConstantsIndex, [self.MODIFY_CONSTANTS, "iddd"] | ||
| yield self.setIndex, [self.SET, "id"] | ||
| yield self.offIndex, [self.OFF, "i"] | ||
| yield self.displayIndex, [self.DISPLAY, "i"] | ||
| yield self.displayResultIndex, [self.DISPLAY_RESULT, "iddd"] | ||
|
|
||
| def constants(self): | ||
| if self.sim: | ||
| return | ||
|
|
||
| response = self.spine.send(self.devname, True, self.CONSTANTS, self.index) | ||
| return response | ||
|
|
||
| def modify_constants(self, kp, ki, kd): | ||
| if self.sim: | ||
| return | ||
|
|
||
| self.spine.send(self.devname, False, self.MODIFY_CONSTANTS, self.index, kp, ki, kd) | ||
|
|
||
| def set(self, setpoint): | ||
| if self.sim: | ||
| return | ||
|
|
||
| self.spine.send(self.devname, False, self.SET, self.index, setpoint) | ||
|
|
||
| def off(self): | ||
| if self.sim: | ||
| return | ||
|
|
||
| self.spine.send(self.devname, False, self.OFF, self.index) | ||
|
|
||
| def display(self): | ||
| if self.sim: | ||
| return 0 | ||
|
|
||
| response = self.spine.send(self.devname, True, self.DISPLAY, self.index) | ||
| return response | ||
|
|
||
| def sim_update(self, tm_diff): | ||
| pass | ||
|
|
||
| def get_hal_data(self): | ||
| return {} | ||
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
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.
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.
Shouldn't this class be Vpid and all the functions only be for vpid?
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.
It should, that class also still needs to be cut down to just vpid. I'm doing it now.