How do I make sense of constant's lookup numbers

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ilium007
Posts: 37
Joined: Tue Feb 16, 2021 10:29 am

How do I make sense of constant's lookup numbers

Post by ilium007 » Wed Apr 06, 2022 12:35 am

Not sure if I have worded this correctly but I am not sure how to look up constant's values. ie.

Code: Select all

>>>
>>> can1.state()
2
>>>
>>>
>>> can1.
any             send            BUS_OFF         ERROR_ACTIVE
ERROR_PASSIVE   ERROR_WARNING   LIST16          LIST32
LOOPBACK        MASK16          MASK32          NORMAL
SILENT          SILENT_LOOPBACK                 STOPPED
clearfilter     deinit          info            init
initfilterbanks                 recv            restart
rxcallback      setfilter       state
>>>
>>>
>>>
In the CAN class CAN.state() returns a value relating to the bus state. These state values are constants that relate to a one of 5 states

CAN.STOPPED
CAN.ERROR_ACTIVE
CAN.ERROR_PASSIVE
CAN.ERROR_WARNING
CAN.BUS_OFF

How do I know which state relates to the integer return from CAN.state()?

Shards
Posts: 39
Joined: Fri Jun 25, 2021 5:14 pm
Location: Milton Keynes, UK

Re: How do I make sense of constant's lookup numbers

Post by Shards » Wed Apr 06, 2022 11:31 am

You can simply print the constants to find their values. However, it's not something you would normally need to do. Presumably you are examining the state to take appropriate action, so you will do something like:

Code: Select all

If can1.state() == CAN.STOPPED :
    Do something
If can1.state() == CAN.ERROR_PASSIVE:
    Do something else
It's then clear what you are doing and should the implementation change it won't fall over. Odd that all the states are in the 'bad things category' but then I have no idea what CAN does.

ilium007
Posts: 37
Joined: Tue Feb 16, 2021 10:29 am

Re: How do I make sense of constant's lookup numbers

Post by ilium007 » Wed Apr 06, 2022 12:29 pm

ahh ok, got it. I was looking at it all from the wrong side!

ilium007
Posts: 37
Joined: Tue Feb 16, 2021 10:29 am

Re: How do I make sense of constant's lookup numbers

Post by ilium007 » Wed Apr 06, 2022 12:33 pm

Shards wrote:
Wed Apr 06, 2022 11:31 am
I have no idea what CAN does.
CANbus is a physical layer protocol for communications between microcontrollers / devices. It's widely used in automotive applications / industrial applications to allow devices to talk in 'noisy' environments over just two wires.

I am building a project to allow microcontrollers to talk between themselves using a simple messaging protocol. The end goal is to control a model railway and have all the control panel inputs communicating with speed controllers, railway points, lights etc all over CANbus.
camphoto_579758561.JPG
camphoto_579758561.JPG (187.97 KiB) Viewed 2697 times

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: How do I make sense of constant's lookup numbers

Post by scruss » Wed Apr 06, 2022 5:06 pm

ilium007 wrote:
Wed Apr 06, 2022 12:33 pm
... The end goal is to control a model railway and have all the control panel inputs communicating with speed controllers, railway points, lights etc all over CANbus.
That sounds like a really impressive project. CAN would be the perfect networking technology for this: cheap and robust.

Le fond du garage
Posts: 9
Joined: Mon Dec 21, 2020 6:38 pm

Re: How do I make sense of constant's lookup numbers

Post by Le fond du garage » Sun May 08, 2022 8:06 pm

super project.

i will make a project to talk with theVESC over CAN.

can you post part of your code please to see how you make simple com between µc in CAN?

regards.

Post Reply