dbus-serialbattery icon indicating copy to clipboard operation
dbus-serialbattery copied to clipboard

WIP: Support for Daly CAN Bus

Open SamuelBrucksch opened this issue 1 year ago • 8 comments

~~This is still highly experimental.~~

Install python-can on Venus OS: As pip3 is not available this needs to be done manually, you can check the download link here: https://pypi.org/project/python-can/#files

wget https://files.pythonhosted.org/packages/1f/f7/a643cba269d59e108fe4c1854a8e71d5cdadadd4de1b9c8862b190171122/python-can-4.0.0.tar.gz
tar xf python-can-4.0.0.tar.gz
cd python-can-4.0.0
python3 setup.py develop

In dbus-serialbattery.py comment all battery types, and uncomment DalyCAN, so it is the only BMS that is tried to connect to.

In Venus OS settings via Remote Console go to Settings->Services->VE.Canport->CAN-bus profile and ~~click disabled. Then on console via SSH enter ip link set can0 up type can bitrate 250000~~ check that a profile with 250k is selected.

As we do not need a serial port, this can be executed from command line directly, without searching for a serial device.

cd /data/etc/dbus-serialbattery
python3 dbus-serialbattery

If everything works well and cables were made correctly, you should see data on remote console.

To test that everything works you can also use Venus OS CLI tools:

candump can0

This will list all the communication on can0

cansend can0 18950140#

This will request the battery cell voltages. Response will look like this:

  candump can0
  cansend can0 18950140# [execute this on a second terminal]
  can0  18950140   [0] 
  can0  18954001   [8]  01 0D 7D 0D 7E 0D 7E 58
  can0  18954001   [8]  02 0D 7E 0D 7F 0D 7E 58
  can0  18954001   [8]  03 0D 7E 0D 7F 0D 7E 58
  can0  18954001   [8]  04 0D 7E 0D 7E 0D 7E 58
  can0  18954001   [8]  05 0D 7E 0D 7F 0D 7F 58
  can0  18954001   [8]  06 0D 7F 0D 7F 0D 7F 58

SamuelBrucksch avatar Aug 01 '22 20:08 SamuelBrucksch

@Louisvdw this sounds a bit complex but a few things can be made easier, like that you do not need to manually setup can0, if i add the right filters to the can bus library. However can does not need a serial, so we need to add a detection mechanism somewhere.

That detection mechanism could for example execute the cansend and candump command line tool and if we get data, we know we have a connected can interface. Is that something we could easily add to the detection mechanism? Because in that case we could also do the auto detection and add a small code snipped, to switch between CAN and serial based on the detection.

SamuelBrucksch avatar Aug 01 '22 20:08 SamuelBrucksch

Hi Samuel Yes that has always been the issue as the CANbus is not running through the serial starter mechanism that the driver normally use. There are a few other batteries that I am working on that also work a bit different. I'll play around with a few options on how to detect the CAN as well.

Louisvdw avatar Aug 02 '22 05:08 Louisvdw

I just added the can filters, so no need to manually set up can anymore as it is set to 250k by default, we can still mention to check if it is really set to 250k.

SamuelBrucksch avatar Aug 02 '22 12:08 SamuelBrucksch

Is it possible to configure the CAN bus at 500K? On multiplus II GX it is not possible to select the speed at 250k and therefore it does not work.

danygi79 avatar Aug 05 '22 23:08 danygi79

Daly has 250k. If the MP2 GX has VE.Can it can be configured to 250k. Only BMS Can is 500k.

SamuelBrucksch avatar Aug 06 '22 04:08 SamuelBrucksch

Is it possible to configure the CAN bus at 500K? On multiplus II GX it is not possible to select the speed at 250k and therefore it does not work.

You need to update the Multiplus II GX Firmware. Than you have the Menue to set the 250k for CAN CAN

I will wait till Louis has implemented the CAN-Connection to the next stable release.

geromd avatar Aug 11 '22 10:08 geromd

How did you connect the MP2 GX to the Daly BMS? Simply an adapter from daly bms port to rj45 of MP2 GX?

LukasBenner avatar Sep 05 '22 18:09 LukasBenner

Daly has CAN bus so i just connected pins L and H from Daly to can port from Cerbo.

SamuelBrucksch avatar Sep 06 '22 11:09 SamuelBrucksch

Hi, no need to change utils.py file?

I'm trying to implement can communication with a BMS Fox. Fox BMS.

image

stanhausc avatar Dec 15 '22 19:12 stanhausc

Hello Maybe a stupid question but does can bus work with venus os 2.9 on raspberry pi ?

Mipamlius avatar Dec 22 '22 08:12 Mipamlius

Just tried venus os 2.93 on a Cerbo and it works. Not sure about the Raspi.

SamuelBrucksch avatar Feb 23 '23 16:02 SamuelBrucksch

Hello Samuel, I will use it on a raspberry py 3b+ with a can-hat from waveshare. I installed the python-can-4.0 package and it is possible to use the commands: candump can0 and cansend can0 18950140# (in extra terminal) It shows the Voltages from the battery-cells. (My can-hat uses can8 and can9 instead of can0) Now I want to install dbus-seriallbattery, but I cannot find the right branch witch supports the can-connection. And the code in this pullrequest does not work with the actual released code 0.14.3 Please can you show me, where I can get it. BR Claus

Newti04 avatar Mar 28 '23 13:03 Newti04

I'm still on the code of this branch, as it works fine for me. There is no newer version of this.

SamuelBrucksch avatar Mar 28 '23 15:03 SamuelBrucksch

Hello Samuel, i tried this branch [36f297a]: https://github.com/SamuelBrucksch/dbus-serialbattery/tree/samuel/add-daly-can-support I copied dbus-serialbattery-samuel-add-daly-can-support/etc/dbus-serialbattery to /data/etc/dbus-serialbattery Then i edit /data/etc/dbus-serialbattery/dbus-serialbattery.py

from dbushelper import DbusHelper            
from utils import DRIVER_VERSION, DRIVER_SUBVERSION, logger
import logging                                             
# from lltjbd import LltJbd                                
# from daly import Daly                                    
# from ant import Ant                                      
# from jkbms import Jkbms                                  
# from sinowealth import Sinowealth                        
# from renogy import Renogy        
# from revov import Revov          
from daly_can import DalyCAN       
# from mnb import MNB
...
    def get_battery_type(_port):                                    
        # all the different batteries the driver support and need to test for
        battery_types = [                                                                                 
            # LltJbd(port=_port, baud=9600),                        
            # Ant(port=_port, baud=19200),                                   
            # Daly(port=_port, baud=9600, address=b"\x40"),                                               
            # Daly(port=_port, baud=9600, address=b"\x80"),         
            # Jkbms(port=_port, baud=115200),                                
            # Sinowealth(port=_port, baud=9600),                                                          
            # Renogy(port=_port, baud=9600),                        
            # Revov (port=_port, baud=9600),                                 
            DalyCAN(port=_port, baud=9600),                                                               
            # MNB(port=_port, baud=9600),                           
        ] 

and I changed can0 to can 8 in /data/etc/dbus-serialbattery/daly_can.py

...
               {"can_id":self.response_cell_balance, "can_mask": 0xFFFFFFF},
               {"can_id":self.response_alarm, "can_mask": 0xFFFFFFF}]
        self.bus = can.Bus(interface='socketcan',
              channel='can8',       
              receive_own_messages=False,
              can_filters=can_filters)
...

because my can-device is named can8

cansend can8 18950140#

shows in an extra terminal candump can8

....
can8  307   [8]  12 34 56 78 56 49 43 00
  can8  305   [8]  00 00 00 00 00 00 00 00
  can8  307   [8]  12 34 56 78 56 49 43 00
  can8  18950140   [0] 
  can8  18954001   [8]  01 0D 1B 0D 1D 0D 1D 00
  can8  18954001   [8]  01 0D 1B 0D 1D 0D 1D C0
  can8  18954001   [8]  01 0D 19 0D 1B 0D 1C 88
  can8  18954001   [8]  02 0D 1E 0D 1F 0D 1E 00
  can8  18954001   [8]  02 0D 1D 0D 1D 0D 1D C0
  can8  18954001   [8]  02 0D 1D 0D 1D 0D 1D 88
  can8  18954001   [8]  03 0D 1F 0D 1E 0D 1F 00
  can8  18954001   [8]  03 0D 1D 0D 1D 0D 1E C0
  can8  18954001   [8]  03 0D 1D 0D 1D 0D 1B 88
  can8  18954001   [8]  04 0D 1E 0D 1D 0D 1E 00
  can8  18954001   [8]  04 0D 1D 0D 1E 0D 1D C0
  can8  18954001   [8]  04 0D 1A 0D 1B 0D 1B 88
  can8  18954001   [8]  05 0D 1E 0D 1E 0D 1E 00
  can8  18954001   [8]  05 0D 19 0D 1D 0D 1D 88
  can8  18954001   [8]  06 0D 1B 0D 1E 0D 1E 00
  can8  18954001   [8]  05 0D 1D 0D 1D 0D 1D C0
  can8  18954001   [8]  06 0D 18 0D 1D 0D 1D 88
  can8  18954001   [8]  06 0D 1B 0D 1D 0D 1D C0
  can8       305   [8]  00 00 00 00 00 00 00 00
  can8       307   [8]  12 34 56 78 56 49 43 00
  can8       305   [8]  00 00 00 00 00 00 00 00
....

It shows the cell-numbers 3 times, because I have 3 DalyBMS 16S connected to the can-bus.

When I start dbus-serialbattery with::

python3 /data/etc/dbus-serialbattery/dbus-serialbattery.py can8

I get this output:

INFO:SerialBattery:Starting dbus-serialbattery
INFO:SerialBattery:dbus-serialbattery v0.12b2
INFO:SerialBattery:Testing DalyCAN
INFO:SerialBattery:DalyBMS 16 cells
INFO:SerialBattery:Connection established to DalyCAN
INFO:SerialBattery:Battery connected to dbus from can8
INFO:SerialBattery:=== Settings ===
INFO:SerialBattery:> Connection voltage NoneV | current NoneA | SOC None%
INFO:SerialBattery:> Cell count 16 | cells populated 0
INFO:SerialBattery:> CCL Charge NoneA | DCL Discharge NoneA
INFO:SerialBattery:> MIN_CELL_VOLTAGE 2.9V | MAX_CELL_VOLTAGE 3.45V
INFO:SerialBattery:DeviceInstance = 2
INFO:SerialBattery:com.victronenergy.battery.can8
WARNING:SerialBattery:read_soc_data - triesValid 2
WARNING:SerialBattery:read_soc_data - triesValid 1
Traceback (most recent call last):
  File "/data/etc/dbus-serialbattery/dbushelper.py", line 176, in publish_battery
    self.publish_dbus()
  File "/data/etc/dbus-serialbattery/dbushelper.py", line 186, in publish_dbus
    self._dbusservice['/Soc'] = round(self.battery.soc, 2)
TypeError: type NoneType doesn't define __round__ method

Cell count 16 is correct, the other things are None

Do you have any hints for me? BR Claus

Newti04 avatar Mar 29 '23 16:03 Newti04

Hello Samuel, I found the problem, I disconnect 2 DalyBMS, so with one DalyBMS connected it works :-) So I have to check, what to do, to use 3 BMS at one can-bus. Thanks for your help. Claus

Newti04 avatar Mar 29 '23 16:03 Newti04

Hello Samuel, i tried this branch [36f297a]: https://github.com/SamuelBrucksch/dbus-serialbattery/tree/samuel/add-daly-can-support I copied dbus-serialbattery-samuel-add-daly-can-support/etc/dbus-serialbattery to /data/etc/dbus-serialbattery Then i edit /data/etc/dbus-serialbattery/dbus-serialbattery.py from dbushelper import DbusHelper from utils import DRIVER_VERSION, DRIVER_SUBVERSION, logger import logging

# from lltjbd import LltJbd
# from daly import Daly
# from ant import Ant
# from jkbms import Jkbms
# from sinowealth import Sinowealth
# from renogy import Renogy
# from revov import Revov
from daly_can import DalyCAN

# from mnb import MNB
... def get_battery_type(_port): # all the different batteries the driver support and need to test for battery_types = [ # LltJbd(port=_port, baud=9600), # Ant(port=_port, baud=19200), # Daly(port=_port, baud=9600, address=b"\x40"), # Daly(port=_port, baud=9600, address=b"\x80"), # Jkbms(port=_port, baud=115200), # Sinowealth(port=_port, baud=9600), # Renogy(port=_port, baud=9600), # Revov (port=_port, baud=9600), DalyCAN(port=_port, baud=9600), # MNB(port=_port, baud=9600), ] and I changed can0 to can 8 in /data/etc/dbus-serialbattery/daly_can.py ... {"can_id":self.response_cell_balance, "can_mask": 0xFFFFFFF}, {"can_id":self.response_alarm, "can_mask": 0xFFFFFFF}] self.bus = can.Bus(interface='socketcan', channel='can8', receive_own_messages=False, can_filters=can_filters) ... because my can-device is named can8 cansend can8 18950140# shows in an extra terminal candump can8 .... can8 307 [8] 12 34 56 78 56 49 43 00 can8 305 [8] 00 00 00 00 00 00 00 00 can8 307 [8] 12 34 56 78 56 49 43 00 can8 18950140 [0] can8 18954001 [8] 01 0D 1B 0D 1D 0D 1D 00 can8 18954001 [8] 01 0D 1B 0D 1D 0D 1D C0 can8 18954001 [8] 01 0D 19 0D 1B 0D 1C 88 can8 18954001 [8] 02 0D 1E 0D 1F 0D 1E 00 can8 18954001 [8] 02 0D 1D 0D 1D 0D 1D C0 can8 18954001 [8] 02 0D 1D 0D 1D 0D 1D 88 can8 18954001 [8] 03 0D 1F 0D 1E 0D 1F 00 can8 18954001 [8] 03 0D 1D 0D 1D 0D 1E C0 can8 18954001 [8] 03 0D 1D 0D 1D 0D 1B 88 can8 18954001 [8] 04 0D 1E 0D 1D 0D 1E 00 can8 18954001 [8] 04 0D 1D 0D 1E 0D 1D C0 can8 18954001 [8] 04 0D 1A 0D 1B 0D 1B 88 can8 18954001 [8] 05 0D 1E 0D 1E 0D 1E 00 can8 18954001 [8] 05 0D 19 0D 1D 0D 1D 88 can8 18954001 [8] 06 0D 1B 0D 1E 0D 1E 00 can8 18954001 [8] 05 0D 1D 0D 1D 0D 1D C0 can8 18954001 [8] 06 0D 18 0D 1D 0D 1D 88 can8 18954001 [8] 06 0D 1B 0D 1D 0D 1D C0 can8 305 [8] 00 00 00 00 00 00 00 00 can8 307 [8] 12 34 56 78 56 49 43 00 can8 305 [8] 00 00 00 00 00 00 00 00 ...

It shows the cell-numbers 3 times, because I have 3 DalyBMS 16S connected to the can-bus.

When I start dbus-serialbattery with:: python3 /data/etc/dbus-serialbattery/dbus-serialbattery.py can8 I get this output:

INFO:SerialBattery:Starting dbus-serialbattery INFO:SerialBattery:dbus-serialbattery v0.12b2 INFO:SerialBattery:Testing DalyCAN INFO:SerialBattery:DalyBMS 16 cells INFO:SerialBattery:Connection established to DalyCAN INFO:SerialBattery:Battery connected to dbus from can8 INFO:SerialBattery:=== Settings === INFO:SerialBattery:> Connection voltage NoneV | current NoneA | SOC None% INFO:SerialBattery:> Cell count 16 | cells populated 0 INFO:SerialBattery:> CCL Charge NoneA | DCL Discharge NoneA INFO:SerialBattery:> MIN_CELL_VOLTAGE 2.9V | MAX_CELL_VOLTAGE 3.45V INFO:SerialBattery:DeviceInstance = 2 INFO:SerialBattery:com.victronenergy.battery.can8 WARNING:SerialBattery:read_soc_data - triesValid 2 WARNING:SerialBattery:read_soc_data - triesValid 1 Traceback (most recent call last): File "/data/etc/dbus-serialbattery/dbushelper.py", line 176, in publish_battery self.publish_dbus() File "/data/etc/dbus-serialbattery/dbushelper.py", line 186, in publish_dbus self._dbusservice['/Soc'] = round(self.battery.soc, 2) TypeError: type NoneType doesn't define **round** method

Cell count 16 is correct, the other things are None

Do you have any hints for me? BR Claus

The error is in the frame you are receiving from the SOC, you receive 'NoneType', I believe it is not returning anything. Have a look at what you're getting, or you can try adding 'self.soc = 50' to your code, and see if it works, and then fix that error.

stanhausc avatar Mar 29 '23 16:03 stanhausc

If you have multiple Daly you try to connect to, you probably need one instance of this for each BMS (with its own physical connection). Else every BMS needs a separate ID. But querying multiple BMS with different IDs is also not supported. So if they share one CAN Bus you definitely need multiple IDs for multiple devices. Check if you can set an ID in the Daly BMS app.

SamuelBrucksch avatar Mar 29 '23 16:03 SamuelBrucksch

Hello Samuel, yes, I have set different IDs for each BMS with the PC-Software. You wrote -> But querying multiple BMS with different IDs is also not supported. You mean it is not supported by this Software-Release, but it could be possible with rewriting/changing it, if the BMSs have different IDs on one can-bus. Ok, my can-hat has 2 can-bus connections and I could use the second can9 for the second BMS, but the intention was to use one connection for all 3 BMS. I see, if I start it with different args, it gets different DeviceInstance numbers, so it is possible to use it with different ports.

Do you know, how to check, how the BMS marks the data with his BMS-ID? ore how to filter the data from each BMS? BR Claus

Newti04 avatar Mar 29 '23 17:03 Newti04

You can check with candump and cansend. One part of the identifier should be the ID, maybe the 01 part, don't remember. Just try to replace the ID part with the new ID, also for filtering the responses and it should work.

SamuelBrucksch avatar Mar 29 '23 17:03 SamuelBrucksch

In the script daly_can.py there is # command bytes [Priority=18][Command=94][BMS ID=01][Uplink ID=40] command_base = 0x18940140 command_soc = 0x18900140 so I tried cansend cansend 18950140# cansend 18950240# cansend 18950340# cansend 18950440# cansend 18950540# ... and so on, but only with result on: cansend 18950140#

I have to check tomorrow with the PC, if the IDs on the BMSs are correct set.

Newti04 avatar Mar 29 '23 19:03 Newti04

Hello Samuel, now I had a look on the settings from the 3 BMS with BMSMonitor-V2.1.9 on the TAB "Engineering model" in the section "BMS address manager", all have different BoardNo: (1-3) and the same SlaveNo: 1. The SlaveNo cannot be changed. If I connect only one BMS and try to read data with candump, I get always the same result, with each BMS 1-3 and they give only feedback on cansend can8 18950140# So, I think the BMS do not change the can-ID when the BoardNo in BMS address manager is changed. The firmware on my BMSs is 11_221015_AL1T, perhaps other firmware will do the trick.

Newti04 avatar Mar 30 '23 12:03 Newti04

Hi guys,

What's the Status here? Any new insights about multiple bms on one port?

I initially wanted this also. My findings were:

  • You have to order a daly bms with can support explicitely
  • You have to also mention that you want to use them in parallel
  • And you also have to buy the WNT Connection boards

On the wnt, you choose the address by dip switch.

I got it running partly, but some read values were garbage. So I ditched it and use normal serial connection. But I'm still interested if there is a working solution.

transistorgit avatar Jul 02 '23 10:07 transistorgit

Hi, i am a bit lost, is there a step by step how to set up can connection? Serial is working, but also want to try can =)

SirDingDong avatar Sep 04 '23 18:09 SirDingDong

I will merge this PR and make some changes to work with the latest version. Since the Bluetooth driver is also not needing the serial adapter I make some changes to the install script so you can specify the can port number in the config.ini.

mr-manuel avatar Sep 17 '23 09:09 mr-manuel

Is there a simple way for a user to display all CAN devices like can0, can8 and so on in case it's not the default one?

mr-manuel avatar Sep 17 '23 11:09 mr-manuel

Please try the latest nightly from the dev branch. To enable CAN add CAN_PORT = can0 to the config.ini.

mr-manuel avatar Sep 17 '23 14:09 mr-manuel

Hi @mr-manuel , sorry for the late reply, i just tried it and set the CAN_PORT to can0, however it does not seem to have any effect. this is the log:

root@einstein:/data/etc/dbus-serialbattery# python3 dbus-serialbattery.py 
INFO:SerialBattery:
INFO:SerialBattery:Starting dbus-serialbattery
INFO:SerialBattery:dbus-serialbattery v1.0.20231010dev2
INFO:SerialBattery:No Port needed
INFO:SerialBattery:-- Testing BMS: 1 of 3 rounds
INFO:SerialBattery:-- Testing BMS: 2 of 3 rounds
INFO:SerialBattery:-- Testing BMS: 3 of 3 rounds
ERROR:SerialBattery:ERROR >>> No battery connection at /dev/ttyUSB9
root@einstein:/data/etc/dbus-serialbattery# 

Also if i enforce can0 in code, it also doesn't work:

root@einstein:/data/etc/dbus-serialbattery# python3 dbus-serialbattery.py 
INFO:SerialBattery:
INFO:SerialBattery:Starting dbus-serialbattery
INFO:SerialBattery:dbus-serialbattery v1.0.20231010dev2
INFO:SerialBattery:-- Testing BMS: 1 of 3 rounds
INFO:SerialBattery:-- Testing BMS: 2 of 3 rounds
INFO:SerialBattery:-- Testing BMS: 3 of 3 rounds
ERROR:SerialBattery:ERROR >>> No battery connection at can0
root@einstein:/data/etc/dbus-serialbattery# 

SamuelBrucksch avatar Oct 28 '23 08:10 SamuelBrucksch

Did you troubleshoot the CAN connection with can tools in the first post?

mr-manuel avatar Oct 29 '23 07:10 mr-manuel

No, i just stopped my working daly can implementation, renamed it to *_old, installed the nightly from develop at the old location and setup everything and reinstalled locally, restarted and then i didnt see anything. so i tried to start it manually by setting can0 and daly programmatically, and it didn't work. As i have a working version, i didn't spend much time on debugging. So not sure, what exactly was wrong here. After i switched back to my old version, everything worked again, so it's probably nothing related to the hardware/can setup.

SamuelBrucksch avatar Oct 29 '23 07:10 SamuelBrucksch

Since I could not test the integrations it will be nice, if you can debug it.

mr-manuel avatar Nov 03 '23 06:11 mr-manuel