B-g431-esc1 CAN interface

Hi All,
I’ve done quite a bit of reading about using CAN with MCP2515 CAN Bus SPI Module Arduino and that makes sense, but its not clear to me how the integrated CAN is used in the b-g431-esc1.

I’ve also read these two threads but TBH I’m a little lost, and unsure if the libraries mentioned are applicable to the B-G and an Arduino dev environment. I’m not a software pro more a persistent hacker lol !

All I’m looking to do (at least initially) is to send individual position demands to multiple motors over a single CAN bus instead of individual hobby servo PWMs I’m doing Dev work with at the moment. I’ll be using them in a fairly harsh EMC environment and am expecting PWM not to cut it for that situation. Eventually I’ll want to return motor / driver status and other information to a central co-ordinating main board but small steps first.

Has anyone got a simple can bus exchange working on the b-g431-esc1, if so any advice on how to work with that interface within Arduino dev environment would be much appreciated.

Many thanks
Barry M

Here we go: GitHub - Igitigit2/SimpleCanLib

I am the author, but the code is a lot copied together from other sources.

Have fun,
Chris

2 Likes

@CNCModeller, I just saw that you were looking for Arduino explicitly. My lib works in the Arduino environment on Platformio, but not using the native Arduino tools. I would clearly recommend using platformio anyway. Arduino is great for very simple projects and to get started, but as soon as your projects get more complex, the Arduino environment will reach its limits.

Best regards,
Chris

Thanks @Grizzly, I guess I need to take the training wheel off and have a play with the Platformio IDE.

I was hoping not to have to go that far this early in the project but hey ho…

Wish me luck! :slight_smile:

Many Thanks
Barry M

1 Like

Good luck! It’s worth the efforts.

  • Chris
2 Likes

Hi @Grizzly ,
I’ve got your library ping pong example code working on a B-G but I also wanted to communicate with another Arduino using an MCP2515 CAN Bus spi module which allows definition of the message ID and payload. Will I be able to send and receive data between the two with your library? Can I use it for the SPI can module too? I’m guessing probably not?

As a fall back I assume I can use the spi setup to sniff out the message IDs etc sent via your library?

I’m also aware that lower value IDs are prioritised over CAN, is that something I can manage via your library too?

Looking at the message setup .h file I’m still only vaguely understanding how the approach of definition of message instances work. But I think I’m getting there.

Any thoughts would be much appreciated.

Many thanks
Barry M

Dear @CNCModeller,

That is great news, with that you have a really solid basis to develop your own application layer!

Currently, there is no support for the MCP2515 in my library and I do not own any such module. Since I am peeking at @Valentine 's Hackjammer boards however, I might add suppot for it in the future, but that won’t be very soon certainly. Supporting the chip should not be too difficult, since there are libraries which implement the low level routines in pretty much the way I need it in my library too. But, as usual, the devil will be hidden somewhere in the details probably. All that would be required will be writing a chip specific version of the class SimpleCan, which is defined in SimpleCan.h.

As a side note here: One of the great advantages of my lib is that it decouples all interrupt service code from the actual application using (on ESP32: thread safe) queues. This means, when you are handling incomming messages in your application, you are not in an interrupt service routine and you can use serial, printing and whatever you need. I had serious problems before I implemented it this way.

That should be no problem. The CAN bus doesn’t care which library is sending and receiving. In principle you will have to implement everything which my library defines in the application specific xxxProfile.h file. In that file you find the CAN IDs used as welll as the definition of functions which handle the incoming messages for those IDs (the broker class…). Note that the xxxProfile.h file defines the interfaces only. The actual implementation should be somewhere else in your code. This allows you to use one single header file to define the entire CAN bus application layer (profile) without forcing all involved modules to include header files for e.g. SimpleFOC or other code that might be required to react on received CAN IDs.

This is implicit. You can define the CAN IDs as you need them and everything else is controlled by the very low level CAN bus protocol and the way access to the bus is controlled. It happens inside the controllers.

If you need more help on this, please let me know.

Best regards,
Chris

There is a well-developed and supported CAN library used in automotive for that chip, since it’s industry standard. I also developed a sample board for development testing. Please check out this post

Implements MCP2515 / SN65HVD230 3.3v input and logic combination.

Using library:

Cheers,
Valentine

Thanks @Valentine & @Grizzly

Yes I have used an SPI based module on my normal Arduinos. One of these:

https://amzn.eu/d/3JcBe2x

I’m hoping to use that as a central master device that sends position demands to multiple b-g driven motors.

Will have a look at the library.:+1:

Many thanks!

Barry M

1 Like

Dear Valentine,

I am aware of that library and it certainly is very good, but implements the very low level only. It is the code which I would use to extend my library to support MCP2515. My goal is to have a common interface for all supported CAN bus controllers, to keep the application free from code called from ISRs and to have a clearly separated application layer which defines CAN IDs and all the associated functionality. The application layer should be ignorant of the low level implementation and actual hardware. A good example is the ping-pong demo which comes with my lib. Both, ESP32 and G431 share the same application layer code, CAN IDs, handler for received messages, header file etc. for exchanging data.

Best regards,
Chris

2 Likes

Is there any update here? I don’t even know the ESC’s J1 pins labels - which one is 5V etc. It does not seem to be written anywhere. If someone can show me the wiring pins, I would be very appreciative of it. Thank you in advance :))

@CNCModeller @Grizzly

It’s in the datasheet for the board, well they call it a manual https://www.st.com/resource/en/user_manual/um2516-electronic-speed-controller-discovery-kit-for-drones-with-stm32g431cb-stmicroelectronics.pdf

page 11

1 Like

There are 4 pins there. I guess, there should be VCC, CAN_H, CAN_L, and GND. What is not written in that manual or on the board is the order of these pins. So, which on is which. Any idea here? I attached an image for reference

oh yeah, sorry I was thinking the other set of pads, there is a pic here B-G431B-ESC1: Beginner guide + I2C guide - hardware support - SimpleFOC Community

Which shows some more, but not those ones. If you look at the schematics of the board you can figure it out though, at least see which are v+ and ground and then the other two would be harder to figure out but you can.

there is a pdf document with the schematics of the board somewhere. If you google there is a page from ST electronics with a collection of documentation, that’s where I got the manual.

The correct pin sequence here is: Gnd, CAN L, CAN H, 5V

1 Like

@Anthony_Douglas I think that image refers to the encoder interface, but still thanks Anthony, the image still comes in handy for me handling with the position sensor. Thanks @Grizzly , that is exactly what I looked for :slight_smile:

I will work on interfacing the st board with nvidia orin jetson via CAN bus. If there is already existing work, feel free to let me know. Otherwise, I guess I will be the first one here :wink:

1 Like

What are you planning to do here - there is existing work/support for fdcan. Both Grizzly and I have repos where esc1 can bus is working - if you get stuck then I’m sure one of us can help you. (I’m currently refactoring my library so it works with stm32f4 and h7 too)

2 Likes

@Owen_Williams, maybe it would be good if we join forces here? My lib started out with code from you, @erwin74 and @JorgeMaker. It supports G431 and ESP32 already (the only ones I have available and currently use). If we can add more MCUs it would be nice of course! We can discuss privatly if you prefer (or not at all, no problem with that).

2 Likes

@Grizzly - sure. It would be great to collaborate. I’m in the middle of a messy refactor at the moment on my v0.91 branch. I’ve been focusing on stmg4 and stmh7, stmf4 and esp. Will probably look at stm32L0 and stm32g0 after that (which I’m hoping are similar to the f4). Then maybe the new uno r4 boards.

Maybe we could discuss on discord (I’m not a regular discord user, but perhaps we could create a channel or something)

A thread on discord would be great… I have also been tinkering with CAN on stm32g4 but you two are both leagues ahead of me in programming ability :slight_smile: