# Communication Watchdog

**URL:** <https://discourse.odriverobotics.com/t/communication-watchdog/1841>\
**Category:** General\
**Created:** [January 30, 2019, 3:35pm UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841 "2019-01-30T15:35:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Belanger](https://discourse.odriverobotics.com/user_avatar/discourse.odriverobotics.com/paul_belanger/32/1107_2.png) [@Paul\_Belanger](https://discourse.odriverobotics.com/u/Paul_Belanger)\
**Post date:** [January 30, 2019, 3:35pm UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/1 "2019-01-30T15:35:59Z")

</div>

There are many applications where the ODrive is used where the various  
communications channels (USB, UART, etc.) are used to dynamically control the  
controller settings, such as setting the desired velocity of the motor based on  
some sort of external control.

This presents an issue in situations where the communication channel between the  
ODrive and the external controller becomes interrupted either due to a crash on  
the external controller or a disconnection of the communication wiring.  
Currently, in such a case, the ODrive continues on its last configured  
instruction. Depending on what’s connected to it, this can cause a host of  
issues such as a vehicle running at full speed into a wall or person.

One way to help with this issue is to implement some sort of communication  
watchdog for the ODrive motor control classes. This watchdog system would  
prevent the Controller class from driving the motor MOSFETs if a property has  
not been changed for longer than a specified interval. Setting a property on the  
Controller class would act as a reset to the watchdog and allow the Controller  
class to again control the motors.

The way I see this working is as follows:

- A new property `watchdog_timeout` is added to each axis. This property  
specifies the watchdog timeout, in seconds, for this axis. If the watchdog  
has not been fed for a time exceeding this timeout, then the motor is  
disarmed and not driven. The value of this property would be saved in  
non-volatile memory. Setting a value of 0 for `watchdog_timeout` will  
disable this watchdog.

- A new property `watchdog_feed` is added to each axis. Writing any value to  
this property will reset the watchdog interval and allow the motor to be  
driven.

- External control loops will write the `watchdog_feed` property after setting  
updating any setpoints or other parameters of the axis. If the external  
controller crashes or otherwise ceases communication, then the watchdog\_feed  
property will not be written and the axis will eventually transition into  
safe mode until the watchdog is fed again.

I’d appreciate any thoughts on this subject. Would there be a better way to add  
fail-safe functionality to the communication channels? Is this something that could be  
considered for inclusion into the default firmware for the ODrive?

Cheers,

Paul Belanger

---

<div class="post-metadata">

**Author:** ![Wetmelon](https://discourse.odriverobotics.com/user_avatar/discourse.odriverobotics.com/wetmelon/32/10_2.png) [@Wetmelon](https://discourse.odriverobotics.com/u/Wetmelon)\
**Post date:** [January 31, 2019, 7:58pm UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/2 "2019-01-31T19:58:39Z")

</div>

I like it in general - implementaton specifics need to be worked out. I think it’s best to put a single communications watchdog somewhere for that comms thread. And then any axis -\> comms thread interface would have a separate watchdog

External \< === \> Comms thread \< === \> Axis 0/1

---

<div class="post-metadata">

**Author:** ![mikh3x4](https://discourse.odriverobotics.com/user_avatar/discourse.odriverobotics.com/mikh3x4/32/1200_2.png) [@mikh3x4](https://discourse.odriverobotics.com/u/mikh3x4)\
**Post date:** [February 4, 2019, 7:14pm UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/3 "2019-02-04T19:14:55Z")

</div>

I would like to just like to add support to this idea. We are using odrives on quite a big robot and already needed to use a hard estop when the odrives lost communication.

---

<div class="post-metadata">

**Author:** ![madcowswe](https://discourse.odriverobotics.com/user_avatar/discourse.odriverobotics.com/madcowswe/32/1138_2.png) [@madcowswe](https://discourse.odriverobotics.com/u/madcowswe)\
**Post date:** [February 9, 2019, 8:02am UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/4 "2019-02-09T08:02:19Z")

</div>

@Paul_Belanger Your proposal as written sounds perfect. If you are up for writing this yourself, then I’d suggest to decrement the timeout variable [right here](https://github.com/madcowswe/ODrive/blob/devel/Firmware/MotorControl/axis.hpp#L124), and add a variable like `communication_ok`.

We could add in calls to `watchdog_feed()` in the `p, v, c, t` commands on the ascii protocol while we are at it, since calling those would constitute runtime type communication.

Let me know if you are not comfortable to dive into the firmware code, in which case I can add this when I find some time.

---

<div class="post-metadata">

**Author:** ![Paul\_Belanger](https://discourse.odriverobotics.com/user_avatar/discourse.odriverobotics.com/paul_belanger/32/1107_2.png) [@Paul\_Belanger](https://discourse.odriverobotics.com/u/Paul_Belanger)\
**Post date:** [March 6, 2019, 7:59pm UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/5 "2019-03-06T19:59:22Z")

</div>

I’ve created an initial implementation of the watchdog, and have submitted a pull request: [https://github.com/madcowswe/ODrive/pull/316](https://github.com/madcowswe/ODrive/pull/316)

I’ve added a `watchdog_timeout` property to the Axis configuration, as well as a `watchdog_feed()` method to the axis api.

For the ascii protocol, I’ve added watchdog feeding to all setpoint commands, and added an additional `u` command to update the watchdog of a motor axis.

I haven’t had the hardware availability to actually test this patch yet, but I should be able to get my hands on some hardware by next week or so.

---

<div class="post-metadata">

**Author:** ![marc.s.cohn](https://discourse.odriverobotics.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@marc.s.cohn](https://discourse.odriverobotics.com/u/marc.s.cohn)\
**Post date:** [December 2, 2019, 3:44pm UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/6 "2019-12-02T15:44:12Z")

</div>

Has this been implemented yet? Had my project try to run away from me yesterday due to a broken wire between arduino and odrive. Failsafe needed.

---

<div class="post-metadata">

**Author:** ![towen](https://discourse.odriverobotics.com/letter_avatar_proxy/v4/letter/t/bcef8e/32.png) [@towen](https://discourse.odriverobotics.com/u/towen)\
**Post date:** [December 2, 2019, 4:46pm UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/7 "2019-12-02T16:46:47Z")

</div>

> [@marc.s.cohn](#):
>
> Had my project try to run away from me yesterday

😆

I have had many a project run away from me. On the time axis though, not spatial coordinates 😛

Yes I believe this is implemented in master now. Update your firmware.  
I think you can simply run `odrvtool dfu` to automatically update to the latest release.

You should see a config option called `axisN.config.watchdog_timeout` - a non-zero value means that you have to continually call axisN.watchdog\_feed() faster than this value or else it will go to idle state.

---

<div class="post-metadata">

**Author:** ![marc.s.cohn](https://discourse.odriverobotics.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@marc.s.cohn](https://discourse.odriverobotics.com/u/marc.s.cohn)\
**Post date:** [December 3, 2019, 2:59am UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/8 "2019-12-03T02:59:39Z")

</div>

thank you,

I assume milliseconds for the timeout value?

---

<div class="post-metadata">

**Author:** ![Paul\_Belanger](https://discourse.odriverobotics.com/user_avatar/discourse.odriverobotics.com/paul_belanger/32/1107_2.png) [@Paul\_Belanger](https://discourse.odriverobotics.com/u/Paul_Belanger)\
**Post date:** [December 3, 2019, 12:02pm UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/9 "2019-12-03T12:02:46Z")

</div>

From the docs:

> Each axis has a configurable watchdog timer that can stop the motors if the control connection to the ODrive is interrupted.
> 
> Each axis has a configurable watchdog timeout: `axis.config.watchdog_timeout` , measured in seconds. A value of `0` disables the watchdog functionality. Any value `> 0` will stop the motors if the watchdog has not been fed in the configured time interval.
> 
> The watchdog is fed using the `axis.watchdog_feed()` method of each axis.

---

<div class="post-metadata">

**Author:** ![marc.s.cohn](https://discourse.odriverobotics.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@marc.s.cohn](https://discourse.odriverobotics.com/u/marc.s.cohn)\
**Post date:** [December 5, 2019, 12:09am UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/10 "2019-12-05T00:09:47Z")

</div>

Thanks for your help. Works for me.

---

<div class="post-metadata">

**Author:** ![daisukes](https://discourse.odriverobotics.com/user_avatar/discourse.odriverobotics.com/daisukes/32/5237_2.png) [@daisukes](https://discourse.odriverobotics.com/u/daisukes)\
**Post date:** [March 18, 2020, 9:26pm UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/11 "2020-03-18T21:26:47Z")

</div>

I was trying to enable watchdog\_timer from ascii interface but it looks like the time was not triggered by a command like “w axis0.config.watchdog\_timer 1.0”.

Should I use native protocol to trigger the timer?

---

<div class="post-metadata">

**Author:** ![Paul\_Belanger](https://discourse.odriverobotics.com/user_avatar/discourse.odriverobotics.com/paul_belanger/32/1107_2.png) [@Paul\_Belanger](https://discourse.odriverobotics.com/u/Paul_Belanger)\
**Post date:** [March 18, 2020, 9:47pm UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/12 "2020-03-18T21:47:56Z")

</div>

You mean the watchdog wasn’t fed?

That’s by design. The watchdog will only be updated when you explictly call `axis.watchdog_feed()` or use a function that modifies the controller setpoint. For the ASCII protocol that would be the `t`, `q`, `p`, `v`, `c`, and `u` commands. Changing parameters with the `w` command does not update the watchdog.

I also noticed you mis-spelled the parameter name in your comment(`watchdog_timer` instead of `watchdog_timeout`).

---

<div class="post-metadata">

**Author:** ![daisukes](https://discourse.odriverobotics.com/user_avatar/discourse.odriverobotics.com/daisukes/32/5237_2.png) [@daisukes](https://discourse.odriverobotics.com/u/daisukes)\
**Post date:** [March 19, 2020, 12:52am UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/13 "2020-03-19T00:52:52Z")

</div>

No, watchdog was fed, but writing watchdog\_timeout property does not start the timer.

> w axis0.config.watchdog\_timeout 1.0  
> (as you mentioned, it was misspelled)

I have been reading the code and I found that ascii protocol does not trigger “update\_watchdog\_settings” which actually set the counter.

Ascii protocol calls set\_string function of ProtocolProperty [here](https://github.com/madcowswe/ODrive/blob/767a2762f9b294b687d761029ef39e742bdf4539/Firmware/communication/ascii_protocol.cpp#L247), but it won’t trigger [property handler](https://github.com/madcowswe/ODrive/blob/767a2762f9b294b687d761029ef39e742bdf4539/Firmware/MotorControl/axis.hpp#L243) which eventually calls update\_watchdog\_settings.

I think it may need to call written\_hook\_ like the handle function.

---

<div class="post-metadata">

**Author:** ![jorgemia](https://discourse.odriverobotics.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jorgemia](https://discourse.odriverobotics.com/u/jorgemia)\
**Post date:** [September 9, 2020, 2:14pm UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/14 "2020-09-09T14:14:19Z")

</div>

I’m using two odrives for my robot and I’m trying to implement the communication watchdog.

The problem I’m encountering is that my main script (ros launch file) has to load to start sending the watchdog\_feed command when I turn on the robot. The problem is that by the time the script has loaded the odrives have already timed out as they’ve been powered on for a couple of seconds and the odrive exits closed loop control and coasts.

Is there any way to get the odrive to start the watchdog only when a first connection has been established/after it has received its first message? Or what do you recommend I do? Should the watchdog feed be placed somewhere else?

---

<div class="post-metadata">

**Author:** ![Paul\_Belanger](https://discourse.odriverobotics.com/user_avatar/discourse.odriverobotics.com/paul_belanger/32/1107_2.png) [@Paul\_Belanger](https://discourse.odriverobotics.com/u/Paul_Belanger)\
**Post date:** [September 9, 2020, 6:13pm UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/15 "2020-09-09T18:13:20Z")

</div>

When the watchdog fails it sets the `AXIS_ERROR_WATCHDOG_TIMER_EXPIRED` (`0x800`) bit on the axis error state (`axisN.error`). Once you’ve finished your initialization code and are calling watchdog\_feed at a suitable rate, you can try clearing the axis error state by setting the error to `0`.

---

<div class="post-metadata">

**Author:** ![jorgemia](https://discourse.odriverobotics.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jorgemia](https://discourse.odriverobotics.com/u/jorgemia)\
**Post date:** [September 10, 2020, 10:18am UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/16 "2020-09-10T10:18:12Z")

</div>

Thanks that worked! I was only calling watchdog\_feed after setting the errors to 0 so i believe it was timing out again. I started calling the watchdog and then set the errors to 0 and it worked.

---

<div class="post-metadata">

**Author:** ![Wetmelon](https://discourse.odriverobotics.com/user_avatar/discourse.odriverobotics.com/wetmelon/32/10_2.png) [@Wetmelon](https://discourse.odriverobotics.com/u/Wetmelon)\
**Post date:** [September 10, 2020, 1:47pm UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/17 "2020-09-10T13:47:13Z")

</div>

Ah, I bet that the watchdog isn’t looking for a rising edge, it’s just looking to see if the timer has expired. That means it’s re-applying the errors at 8kHz 😃 So yeah, send the watchdog first

---

<div class="post-metadata">

**Author:** ![Paul\_Belanger](https://discourse.odriverobotics.com/user_avatar/discourse.odriverobotics.com/paul_belanger/32/1107_2.png) [@Paul\_Belanger](https://discourse.odriverobotics.com/u/Paul_Belanger)\
**Post date:** [September 11, 2020, 12:32am UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/18 "2020-09-11T00:32:43Z")

</div>

Yeah, it’s set up that way to avoid the trap of someone clearing the error, but forgetting to call `watchdog_feed()`

---

<div class="post-metadata">

**Author:** ![furushchev](https://discourse.odriverobotics.com/user_avatar/discourse.odriverobotics.com/furushchev/32/4149_2.png) [@furushchev](https://discourse.odriverobotics.com/u/furushchev)\
**Post date:** [July 12, 2021, 4:11am UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/19 "2021-07-12T04:11:36Z")

</div>

Hi,

I’m planning to change communication between computer and ODrive controllers from USB to CAN for isolating electrical lines.  
Does the watchdog timer work on CAN communication?

Thanks!

---

<div class="post-metadata">

**Author:** ![Wetmelon](https://discourse.odriverobotics.com/user_avatar/discourse.odriverobotics.com/wetmelon/32/10_2.png) [@Wetmelon](https://discourse.odriverobotics.com/u/Wetmelon)\
**Post date:** [July 17, 2021, 2:28am UTC](https://discourse.odriverobotics.com/t/communication-watchdog/1841/20 "2021-07-17T02:28:02Z")

</div>

Yes. The CAN protocol kicks the watchdog any time a valid message arrives
