• Specification
    • 1. Introduction
    • 2. Basic concepts
    • 3. Data structure description language
    • 4. CAN bus transport layer
    • 5. Application level conventions
    • 6. Application level functions
    • 7. List of standard data types
    • 8. Hardware design recommendations
  • Implementations
    • Libuavcan
      • Platforms
      • Tutorials
        • 1. Library build configuration
        • 2. Node initialization and startup
        • 3. Publishers and subscribers
        • 4. Services
        • 5. Timers
        • 6. Time synchronization
        • 7. Remote node reconfiguration
        • 8. Custom data types
        • 9. Node discovery
        • 10. Dynamic node ID allocation
        • 11. Firmware update
        • 12. Multithreading
        • 13. CAN acceptance filters
      • FAQ
    • Pyuavcan
      • Examples
        • Automated ESC enumeration
        • ESC throttle control
      • Tutorials
        • 1. Setup
        • 2. Basic usage
        • 3. Advanced usage
        • 4. Parsing DSDL definitions
    • Libcanard
    • Uavcan.rs
  • GUI Tool
    • Overview
    • Examples
    • User guide
  • Examples
    • Simple sensor node
  • Forum
Implementations /  Pyuavcan /  Examples /  ESC throttle control

ESC throttle control

This example script demonstrates how to control ESC throttle setpoint using the Pyuavcan library. Before running the script, make sure that no other node on the bus issues contradictory ESC commands concurrently.

Writing the script

One code sample is worth 1024 words:

#!/usr/bin/env python3

import uavcan, time, math


# Publishing setpoint values from this function; it is invoked periodically from the node thread.
def publish_throttle_setpoint():
    # Generating a sine wave
    setpoint = int(512 * (math.sin(time.time()) + 2))
    # Commanding ESC with indices 0, 1, 2, 3 only
    commands = [setpoint, setpoint, setpoint, setpoint]
    message = uavcan.equipment.esc.RawCommand(cmd=commands)
    node.broadcast(message)


if __name__ == '__main__':
    # Initializing a UAVCAN node instance.
    # In this example we're using an SLCAN adapter on the port '/dev/ttyACM0'.
    # PyUAVCAN also supports other types of adapters, refer to the docs to learn more.
    node = uavcan.make_node('/dev/ttyACM0', node_id=10, bitrate=1000000)

    # Initializing a dynamic node ID allocator.
    # This would not be necessary if the nodes were configured to use static node ID.
    node_monitor = uavcan.app.node_monitor.NodeMonitor(node)
    dynamic_node_id_allocator = uavcan.app.dynamic_node_id.CentralizedServer(node, node_monitor)

    # Waiting for at least one other node to appear online (our local node is already online).
    while len(dynamic_node_id_allocator.get_allocation_table()) <= 1:
        print('Waiting for other nodes to become online...')
        node.spin(timeout=1)

    # This is how we invoke the publishing function periodically.
    node.periodic(0.05, publish_throttle_setpoint)

    # Printing ESC status message to stdout in human-readable YAML format.
    node.add_handler(uavcan.equipment.esc.Status, lambda msg: print(uavcan.to_yaml(msg)))

    # Running the node until the application is terminated or until first error.
    try:
        node.spin()
    except KeyboardInterrupt:
        pass

Running the script

Save the above code somewhere and run it. The connected ESC will be changing their RPM in a sine pattern, slowly accelerating and decelerating. The script will periodically print output similar to this:

### Message from 124 to All  ts_mono=19376.645693  ts_real=1470440665.872391
error_count: 0
voltage: 13.2812
current: 1.3379
temperature: 313.15
rpm: 1514
power_rating_pct: 13
esc_index: 3

Using the UAVCAN GUI Tool

It is also possible to run the above script (with minor modifications) directly from the interactive console of the UAVCAN GUI Tool, because the UAVCAN GUI Tool is built on top of Pyuavcan. In that case you won’t need to create a new node yourself in the script - just use the application’s own node, it is accessible from the interactive console. For details, please read the documentation of the UAVCAN GUI Tool.

Obsolete

This website is dedicated to the experimental version of the protocol known as UAVCAN v0 that is now obsolete. To learn more about the stable release UAVCAN v1.0, please visit the main website.

Pyuavcan

  • Examples
    • Automated ESC enumeration
    • ESC throttle control
  • Tutorials
    • 1. Setup
    • 2. Basic usage
    • 3. Advanced usage
    • 4. Parsing DSDL definitions

License

This work is licensed under a Creative Commons Attribution 4.0 International License.
  • Discussion forum
  • GitHub organization
  • Report a problem with this website

Generated Thu, 17 Feb 2022 16:27:38 +0000 © UAVCAN development team