Skip to content
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

Automatically start can interface on boot #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ or read all the parameters that are stored or existing in the command set of SOL
More information about SoloPy on `the SOLO website <https://www.solomotorcontrollers.com/>`_.
For List of All the Available Methods Read the `DataSheet <https://www.solomotorcontrollers.com/resources/specs-datasheets/>`__

SoloPy allows SOLO devices to be used in this conditions:
SoloPy allows SOLO devices to be used in this conditions:

- USB Communication with Windows OS or Linux

Expand All @@ -35,8 +35,8 @@ If you have installed the library and you want to update it on Linux, RASPBERRY

.. code-block::

$ pip install --upgrade SoloPy
$ pip install --upgrade SoloPy



USB Communication with Windows OS or Linux Note
Expand All @@ -54,7 +54,7 @@ USB Communication with Windows OS or Linux Note

UART protocol with RASPBERRY PI Note
=============
In order to enable UART Protocol on Raspberry Pi you need to follow this one time process.
In order to enable UART Protocol on Raspberry Pi you need to follow this one time process.

1. In Raspberry Pi, enter following command in Terminal to enable UART

Expand All @@ -79,15 +79,15 @@ CanOpen protocol with RASPBERRY PI Note
=============
In order to enable CanOpen Protocol on Raspberry Pi you need to follow this process one time

1. turn SPI on in raspberry pi:
1. turn SPI on in raspberry pi:

.. code-block::

$ sudo raspi-config
$ sudo raspi-config

then go to interfaces
then go to interfaces

then go to SPI and turn on
then go to SPI and turn on

then go to reboot

Expand All @@ -114,17 +114,22 @@ then go to reboot

**Every time** you reboot RASPBERRY PI you need to follow this process

1- type this command
1- type this command

.. code-block::

$ sudo ip link set can0 up type can bitrate 1000000

**Notes**

.. note::
To automatically start the CAN interface on boot, you can install a systemd service.
This can be done with the script ``scripts/install_can_service.sh``

**Notes**

- The bit-rate has to be the same as the one used in the code

- The following CAN transceiver module `"PiCAN2" <https://copperhilltech.com/pican-2-can-bus-interface-for-raspberry-pi/>`__ has been used to test the library
- The following CAN transceiver module `"PiCAN2" <https://copperhilltech.com/pican-2-can-bus-interface-for-raspberry-pi/>`__ has been used to test the library


Dependencies
Expand Down
52 changes: 52 additions & 0 deletions scripts/install_can_service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash

# Default bitrate
BITRATE=1000000


# Check if the service already exists
if systemctl --quiet is-active can_setup.service; then
echo "The CAN setup service is already active."
read -p "Do you want to stop, update, and restart the service? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
sudo systemctl stop can_setup.service
else
echo "Exiting without making changes."
exit 0
fi
fi

# Create the script for setting up CAN interfaces
cat <<EOL | sudo tee /etc/init.d/can_setup.sh
#!/bin/bash
sudo ip link set can0 up type can bitrate $BITRATE
sudo ifconfig can0 txqueuelen 65536
EOL

# Make the script executable
sudo chmod +x /etc/init.d/can_setup.sh

# Create the systemd service file
cat <<EOL | sudo tee /etc/systemd/system/can_setup.service
[Unit]
Description=Setup CAN interfaces
After=network.target

[Service]
ExecStart=/etc/init.d/can_setup.sh

[Install]
WantedBy=multi-user.target
EOL

# Reload the systemd manager configuration
sudo systemctl daemon-reload

# Enable the service
sudo systemctl enable can_setup.service

# Start the service
sudo systemctl start can_setup.service

echo "CAN interfaces setup complete with a bitrate of $BITRATE."