Beaglebone Green Canbus

Hello,

I am trying to setup canbus on a fleet of vehicles using the beaglebone green. I have working can bus on a beaglebone green without docker or balena. My trouble is trying to turn on can0 and can1. I have edited the /etc/network/interfaces document on my beaglebone but don’t know how to do that. Additionally I am used to setting up the pin configuration using a pin config shell script. This requires a service to be established in systemd. I found this complex with balena and docker so I instead tried to simply RUN “config-pin p9.19” unfortunately this does not work. What should I do to get this working. I have tried installing the bb-cape-overlays but this does not work.

I am using FROM balenalib/beaglebone-green-golang:latest-build AS build

Hello.

The following steps might help.

Enable the CAN Bus with capemgr. It is supported by balenaOS, see the documentation here.
You can do something like echo BB-CAN1 > /sys/devices/platform/bone_capemgr/slots to enable CAN Bus 1 for instance.

Bring up the interface by running sudo ip link set can0 up type can bitrate 250000.
You could also enable the dtoverlay in /boot/uEnv.txt so it is loaded at system boot.

Note that it needs to be done at runtime, so there has to be a script that sets up those interfaces, pins, etc.

Hope that helps to get you going! Please let us know how it goes.

Awesome, thank you for the help, I will give that a try!

So, I have tried what you proposed. I however don’t believe I am doing everything correctly. Here are a few questions that have popped up along the way.

Can I see an example dockerfile that shows how to modify /mnt/boot/uEnv.txt. Everytime I try it says something like. " COPY failed: stat /var/lib/balena32/tmp/docker-builder187984163/mgbApp/uEnv.txt: no such file or directory"

Also wrapping my brain around multi-stage building of containers is limited as well and it appears is a requirement when working with go, is this correct?

Hi Drey,

Let’s focus on getting the CAN buses to work first. The instructions above indicated that you should run the following as part of your application container entry script with the root user:

 echo BB-CAN0 > /sys/devices/platform/bone_capemgr/slots
 echo BB-CAN1 > /sys/devices/platform/bone_capemgr/slots
ip link set can0 up type can bitrate 250000
ip link set can1 up type can bitrate 250000

The above lines should be enough to get the CAN interfaces working.
Could you please try those instructions and let us know the outcome?
Thanks

So am I supposed to include this in my dockerfile?

Entering this command into the Host OS appears to be the wrong option. I must be seriously lacking the necessary knowledge to get this going haha!

Indeed, the application container entry script refers to Dockerfile.
More specifically, depending on how your application is set up, you’re likely looking to append the commands using CMD or ENTRYPOINT. See https://docs.docker.com/develop/develop-images/dockerfile_best-practices for context.

As always, let us know how it goes!

I know its a lot to ask but can you help me with what the dockerfile would look like? I have tried many different ways now and it just keeps failing.

Hi Drey,

The lines that Alex added above need to be run on your application container, not the host machine. In a nutshell, your device runs on balenaOS, but your dockerfile(s) wire up containers which have their own OS and run their own programs and scripts. You need to create a script as part of your application, in the same directory as the dockerfile, and add a COPY line to bring it into the container. You need need to use the CMD or ENTRYPOINT directives to make it run in the container. That way, every time the container is started, those commands are run and the CanBus is enabled.

I think you would really benefit from completing our masterclasses on docker and services:
https://www.balena.io/docs/learn/more/masterclasses/docker-masterclass/#4-introduction-to-dockerfiles
https://www.balena.io/docs/learn/more/masterclasses/services-masterclass/

They will help you better understand what to do going forward!
Phil

Phil,

Thank you for that explanation. I believe what you are asking me to do is something I have done. I wrote a script which includes the 4 lines of code that @alexgg provided above. I then made it executable with chmod +x. Then I call the script with CMD ./somescript.sh. The container builds successfully and the script is executed. When I look at the output of the container however it shows the same error as above “write error: No such file or directory”.

Is there a way to test these commands after the containers are built to ensure that the commands will indeed function when built using docker?

Hi Drey,

In your Dockerfile, did you add a line such as:

RUN chmod +x somescript```

That will move your script to the current working directory on your device's HostOS. Then as a last line in your Dockerfile:

```CMD ["/bin/bash",  "./somescript.sh"]```

John

Sorry, something got messed up when I sent that message. The Dockerfile should have:

COPY somescript.sh .
RUN chmod +x somescript.sh

Then the last line should be:

CMD ["/bin/bash", "./somescript.sh"]

John

@jtonello Thank you for the quick response. Below is the output after running the script

11.09.20 12:05:57 (-0700)  main  ./somescript.sh: line 2: echo: write error: No such file or directory
11.09.20 12:05:57 (-0700)  main  Cannot find device "can0"
11.09.20 12:05:57 (-0700)  main  Cannot find device "can1"```

Here is my Dockerfile.template contents 

```FROM balenalib/%%BALENA_MACHINE_NAME%%-debian:stretch
COPY somescript.sh .
RUN chmod +x somescript.sh
CMD ["/bin/bash", "./somescript.sh"]```

Here are my script contents 

```echo BB-CAN0 > /sys/devices/platform/bone_capemgr/slots
echo BB-CAN1 > /sys/devices/platform/bone_capemgr/slots
ip link set can0 up type can bitrate 250000
ip link set can1 up type can bitrate 250000```

Also how do I post multiple code snippets in the forum and have it show in separate outputs?

Hi,

First, is your somescript.sh located in the same directory as your Dockerfile.template. That’s a must in this example because it’s trying to copy from the working directory on your workstation. Second, is the first line of your bash script #!/bin/bash ?

John

As for code snippets, you should be able to wrap each bit with ` (3 ticks before and after multi-line snippets, not single quotes)

John

Yes it is in the same directory and the shebang is in the first line of the script.

Do I need to enable the bone_capemgr/slots? I believe I read somewhere that slots doesn’t allow modification until you enable something.