Add UART Protocol Support for PC10/PC11 (USART3)#101
Add UART Protocol Support for PC10/PC11 (USART3)#101perara wants to merge 4 commits intomjbots:mainfrom
Conversation
- Introduced `uart_fdcanusb_micro_server.h` to handle UART communication using the FDCAN USB protocol. - Updated `BUILD` file to enable UART FDCAN USB configuration. - Modified `moteus_controller.cc` and `moteus.cc` to integrate UART protocol handling and server polling. - Adjusted hardware pin definitions in `moteus_hw.cc` for UART functionality.
- Updated the constructor of `FdcanusbDevice` to include an optional `baudrate` parameter, defaulting to 9600 if not provided. - Modified the `FdcanusbFactory` to accept a command-line argument for specifying the baudrate for the serial adapter, improving flexibility for different hardware configurations.
- Changed the compilation flag from `-DMOTEUS_ENABLE_UART_FDCANUSB` to `-DMOTEUS_ENABLE_UART_PROTOCOL` to reflect the new UART protocol implementation.
- Added '.history/' to the .gitignore to prevent tracking of history files in the repository.
|
Thanks for sharing! I can't commit to merging this, but I have questions/requests:
I admit to using the fdcanusb protocol is quite ingenious and solves a lot of problems, like maintaining interoperability with all existing tools and the python and C++ library for desktops. It was not a design alternative I had considered and makes it much more feasible. It could even be used in an RS485 multiplex bus using either a direction pin, or ignoring self sends like if the n1/x1 RS422 wires are connected together. Some downsides:
As mentioned, I can't commit to merging this even if every request was addressed, so take them for what you will! |
|
Thanks for the feedback. We will have a look into them. Yes for many applications the CAN does makes sense with the issues you point out. The downside of CAN is that you would for many applications / systems need intermediate chip, like the CAN to USB or even CAN to Serial. I would agree that for applications where robustness are of utmost criticality, CAN would be the top choice, but for applications without these same needs, it is a good option to interface with simple serial. For now i have kept everything identical to your protocol, but with a bit building ontop of it (not changing it) we could add the desired robustness. Essentially, changing as little as i could to make it work well. we will address both :) |
|
FYI: I'm taking a look at doing something along these lines myself in the event you were still doing anything with it. |
|
This is awesome! It would be perfect to have this suppored upstream |
|
@jpieper we would use this feature. |
- Add fdcanusb ASCII protocol over UART (PC10/PC11, 460800 baud) - New fw/uart_fdcanusb_micro_server.h for moteus_tool/tview over serial - BUILD: enable MOTEUS_ENABLE_UART_PROTOCOL, add uart_fdcanusb_micro_server.h - moteus.cc: UART server, dual transport, pool 24k when UART enabled - moteus_controller.cc: disable debug UART when protocol on same pins - moteus_hw.cc: family 0 UART pins PC_10/PC_11 - Python: FdcanusbDevice baudrate param, exclusive=True; --fdcanusb-baud - .gitignore: .history/ Made-with: Cursor
|
So, I have a draft here: https://github.com/mjbots/moteus/tree/serial_command It requires both firmware and client side changes in order to use checksums. Is anyone watching this PR willing to test it? If so, do you need help with firmware binaries? |
|
Hi @jpieper, I'll test it this weekend. |
|
@jpieper, I'm not sure where is best to talk you about your serial_command code. I have migrated to it now and its working ok for me. I enabled CRC in my scripts to test that as well and so far its fine. I cannot really give much feedback as I rely on AI most of the time to do the coding so I not a good tester. Let me know if you have any questions and I'll try to answer them |
|
In my case, we will need to use PC11 and PC10 for the UART. The ones being DEBUG_TX/DEBUG_RX in schematics for the R4.11. Is this something that could be a compile flag? EDIT: Newbringer@390b343#diff-16813c55826128cf3a224a3168e1ca02c4edc359a59cb4c06ecde51315621dce would something like this be acceptable? |
|
What about something like this? diff --git a/fw/moteus_controller.cc b/fw/moteus_controller.cc
index f54fe970..bd0bee11 100644
--- a/fw/moteus_controller.cc
+++ b/fw/moteus_controller.cc
@@ -424,8 +424,17 @@ aux::AuxHardwareConfig GetAux2HardwareConfig() {
// ADC# CHN I2C SPI USART TIMER
{ 0, PB_8, -1, 0, I2C1, nullptr, USART3, nullptr },
{ 1, PB_9, -1, 0, I2C1, nullptr, USART3, nullptr },
+ // Select which two stm32 pins are used for the
+ // non-connectorized aux2 pins for moteus-r4. By default,
+ // the DBG1/DBG2, but if MOTEUS_R4_AUX2_RT is
+ // defined, then they will be the R and T pads.
+#ifndef MOTEUS_R4_AUX2_RT
{ 2, PC_14, -1, 0, nullptr, nullptr, nullptr, nullptr },
{ 3, PC_15, -1, 0, nullptr, nullptr, nullptr, nullptr },
+#else
+ { 2, PC_10, -1, 0, nullptr, nullptr, USART3, nullptr },
+ { 3, PC_11, -1, 0, nullptr, nullptr, USART3, nullptr },
+#endif
{ -1, NC, },
}},
aux_options, |
|
Oh, you edited your comment with a proposed diff that seems to basically accomplish a similar thing to mine. I think the only practical difference is the compile time default baud rate, which sure, but can you really not change the baud rate of your client? ;) |
|
Ah yes, the baud is not critical. The only reason I would keep it configurable is to support dev on windows through UART-TO-USB bridges like CP2102 which dont like bauds this high. But this is just minor :) Will test @9a294a1 |
|
Let's move discussion to this new PR with the code in question: #108 |
As previously discussed. Some are in need of UART support. Since it was not natively supported, we thought it nice to add. Feel free to adopt for the main repo if interesting.
Summary
This PR adds support for using the moteus firmware over UART with the fdcanusb ASCII protocol, enabling communication via serial adapters (e.g., USB-UART, RS485) as an alternative to CAN-FD. Its guarded behind compile macro.
Motivation
Changes
Firmware (
fw/)fw/uart_fdcanusb_micro_server.h(new): Complete implementation of fdcanusb ASCII protocol over UARTcan sendcommands andrcv/OKresponsesfw/moteus.cc: Conditional UART initialization and multiplex routingfw/moteus_hw.cc: Pin configuration for USART3 (PC10/PC11)fw/moteus_controller.cc: Disable debug UART when protocol enabled to prevent pin contentionfw/BUILD: Fixed macro name fromMOTEUS_ENABLE_UART_FDCANUSBtoMOTEUS_ENABLE_UART_PROTOCOLPython Library (
lib/python/moteus/)fdcanusb_device.py: Addbaudrateparameter supportexclusive=Trueto prevent multi-access issuestransport_factory.py: Add--fdcanusb-baudcommand-line argument--fdcanusb /dev/ttyUSB0 --fdcanusb-baud 460800Other
.gitignore: Added.history/for VS Code local history filesTesting
Tested with:
Technical Details
can send XXXX PAYLOAD FLAGS\n→OK\n/rcv ...)--fdcanusb-baud)Breaking Changes
None. UART support is opt-in and does not affect existing CAN-FD functionality.
Code Size Impact
uart_fdcanusb_micro_server.h)