-
Notifications
You must be signed in to change notification settings - Fork 674
SocketCAN (native) cyclic improvements #323
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
christiansandberg
merged 3 commits into
hardbyte:develop
from
christiansandberg:socketcan-cylic-fix
Jun 12, 2018
Merged
Changes from 1 commit
Commits
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
Next
Next commit
Keep periodic transmission on data update
Use BCM for setting duration
- Loading branch information
commit 7f20963fd3933a1e74be5143caa4b05196b4e475
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,6 @@ | |
| """ | ||
|
|
||
| import logging | ||
| import threading | ||
|
|
||
| import os | ||
| import select | ||
|
|
@@ -143,6 +142,10 @@ def split_time(value): | |
| return build_bcm_header(opcode, flags, count, ival1_seconds, ival1_usec, ival2_seconds, ival2_usec, can_id, nframes) | ||
|
|
||
|
|
||
| def build_bcm_update_header(can_id, msg_flags): | ||
| return build_bcm_header(CAN_BCM_TX_SETUP, msg_flags, 0, 0, 0, 0, 0, can_id, 1) | ||
|
|
||
|
|
||
| def dissect_can_frame(frame): | ||
| can_id, can_dlc, flags = CAN_FRAME_HEADER_STRUCT.unpack_from(frame) | ||
| if len(frame) != CANFD_MTU: | ||
|
|
@@ -224,22 +227,31 @@ class CyclicSendTask(SocketCanBCMBase, LimitedDurationCyclicSendTaskABC, | |
|
|
||
| """ | ||
|
|
||
| def __init__(self, channel, message, period): | ||
| def __init__(self, channel, message, period, duration=None): | ||
| """ | ||
| :param channel: The name of the CAN channel to connect to. | ||
| :param message: The message to be sent periodically. | ||
| :param period: The rate in seconds at which to send the message. | ||
| """ | ||
| super(CyclicSendTask, self).__init__(channel, message, period, duration=None) | ||
| super(CyclicSendTask, self).__init__(channel, message, period, duration) | ||
| self.duration = duration | ||
| self._tx_setup(message) | ||
| self.message = message | ||
|
|
||
| def _tx_setup(self, message): | ||
| # Create a low level packed frame to pass to the kernel | ||
| self.can_id_with_flags = _add_flags_to_can_id(message) | ||
| self.flags = CAN_FD_FRAME if message.is_fd else 0 | ||
| header = build_bcm_transmit_header(self.can_id_with_flags, 0, 0.0, | ||
| self.period, self.flags) | ||
| if self.duration: | ||
| count = int(self.duration / self.period) | ||
| ival1 = self.period | ||
| ival2 = 0 | ||
| else: | ||
| count = 0 | ||
| ival1 = 0 | ||
| ival2 = self.period | ||
| header = build_bcm_transmit_header(self.can_id_with_flags, count, ival1, | ||
| ival2, self.flags) | ||
| frame = build_can_frame(message) | ||
| log.debug("Sending BCM command") | ||
| send_bcm(self.bcm_socket, header + frame) | ||
|
|
@@ -262,7 +274,9 @@ def modify_data(self, message): | |
| Note the Message must have the same :attr:`~can.Message.arbitration_id`. | ||
|
Collaborator
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. ... "like the one of the message given in the constructor" |
||
| """ | ||
| assert message.arbitration_id == self.can_id, "You cannot modify the can identifier" | ||
| self._tx_setup(message) | ||
| header = build_bcm_update_header(self.can_id_with_flags, self.flags) | ||
| frame = build_can_frame(message) | ||
| send_bcm(self.bcm_socket, header + frame) | ||
|
|
||
| def start(self): | ||
| self._tx_setup(self.message) | ||
|
|
@@ -481,13 +495,7 @@ def send(self, msg, timeout=None): | |
| raise can.CanError("can.socketcan_native failed to transmit: {}".format(error_message)) | ||
|
|
||
| def send_periodic(self, msg, period, duration=None): | ||
| task = CyclicSendTask(self.channel, msg, period) | ||
|
|
||
| if duration is not None: | ||
| stop_timer = threading.Timer(duration, task.stop) | ||
| stop_timer.start() | ||
|
|
||
| return task | ||
| return CyclicSendTask(self.channel, msg, period, duration) | ||
|
|
||
| def _apply_filters(self, filters): | ||
| try: | ||
|
|
||
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.
maybe add the param to the docs here