diff options
author | 2023-09-15 23:08:52 +0530 | |
---|---|---|
committer | 2023-09-18 15:57:18 +0000 | |
commit | 746451c1587a2830b47904b5abb9f0b6c1b10641 (patch) | |
tree | 4c2531b4740f4b7c61f2e245636c40df9d9c9844 /extras/FeedCAN.py | |
parent | c9e50f62b3a59133b64d98d15bfe18c52b2ba7f5 (diff) |
Update extra modules
V1:
- Add module to feed Steering Control CAN messages
- Add License headers to files
- Modify UI_Handler to accommodate new Navigation bar animations
- Minor code refactoring
V2:
- Add subscription handling in UI_Handler module
- Add documentation for code
- Remove redundant code
Bug-AGL: SPEC-4905
Signed-off-by: suchinton2001 <suchinton.2001@gmail.com>
Change-Id: I6f7c2c2fb00885064c7894329329f13e447648d3
Diffstat (limited to 'extras/FeedCAN.py')
-rw-r--r-- | extras/FeedCAN.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/extras/FeedCAN.py b/extras/FeedCAN.py new file mode 100644 index 0000000..92649f4 --- /dev/null +++ b/extras/FeedCAN.py @@ -0,0 +1,66 @@ +""" + Copyright 2023 Suchinton Chakravarty + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +""" + +import can + +def send_can_signal(frame): + """ + Sends a CAN signal to the CAN bus using the given frame. + Args: + frame (str): The frame to be sent to the CAN bus. + Returns: + None + """ + msg = separate_can_frame(frame) + bus = can.interface.Bus(channel='can0', bustype='socketcan') + #msg = can.Message(arbitration_id=can_id, data=data, is_extended_id=False) + try: + bus.send(msg) + print("CAN signal sent successfully:") + print("CAN ID:", hex(msg.arbitration_id)) + print("Data:", msg.data) + if frame!="021#FFFFFFFF00000000": + # Turn off signal + send_can_signal("021#FFFFFFFF00000000") + + except can.CanError: + print("Failed to send CAN signal") + finally: + bus.shutdown() + +def separate_can_frame(frame): + """ + Separates a CAN frame into its arbitration ID and data parts. + + Args: + frame (str): A string representing the CAN frame in the format "ARBID#DATA". + + Returns: + can.Message: A can.Message object with the arbitration ID and data extracted from the input frame. + """ + arb_id, data = frame.split("#") + arb_id = int(arb_id, 16) + data = bytes.fromhex(data) + message = can.Message(arbitration_id=arb_id, data=data) + return message + + +def main(): + frame = "021#FFFFFFFF10000000" + send_can_signal(frame) + +if __name__ == "__main__": + main() |