Skip to content

Closed loop stepper motor controller with CANbus capabilities. This Project open source hardware and code, support platformio build and upload firmware.

Notifications You must be signed in to change notification settings

killinen/StepperServoCAN

 
 

Repository files navigation

Sonarcloud Status CodeFactor Codacy Badge

StepperServoCAN - firmware

  • This firmware is for two phase stepper motor actuator controller.
  • It allows for smooth (close-loop) torque control by reading shaft angle from a magnetic sensor
  • it support CANbus communication. It can interpret values based on specified motor gearing

Hardware

  • PCB Schematics repo here
  • STMicroelectronics' 32-bit MCU, STM32F103C8T6 ARM 32-bit, Cortex™-M3 CPU Core, 72MHz maximum frequency, 20k RAM, 64k Flash (but really 128k ??).
  • TLE5012 15bit magnetic (GMR) angle sensor. Also has temperature sensor.
  • A4950 current drivers (integrated mosfets) - IMPORTANT - do not back drive the motor without the board modification

Firmware

  • Uses ST's (old) Standard Peripheral Library (src/lib) for registers configuration
  • CAN handling c-code is generated from dbc file using cantools. See generate_Msg.sh
  • It uses Platformio to build, upload and debug the program
  • User can interract with Function buttons (F1 and F2) and Reset button (Rst) or via debug virtual serial, aka semihosting

Build and upload

  • Specify your programming dongle in firmware/platformio.ini - default is stlink
  • The easiest way to build is to load firmware folder into VScode workspace and loading Platformio IDE extension. Quick start.
  • Alternatively Platformio Core can be used to build from command line:
    cd /firmware
    platformio run
  • Upload firmware to the board by pressing Upload arrow at the status bar in VScode
  • Eeprom is not erased when flashing the firmware - any future calibration will not be lost.

Configuration

  • In firmware/src/BSP/actuator_config.c set:
    • rated_current (single phase) and rated_torque from motor spec or measurment. Note, this is just a datapoint and will be extrapolated up to 3.3A. Choose motor wisely.
    • motor_gearbox_ratio - gearbox attached to the motor
    • final_drive_ratio - any additional gearing - separate parameter for convenience
  • Depending on mounting orientation and gearing the motor rotation direction may be reversed. You can change the direction by setting motor_gearbox_ratio or final_drive_ratio to a negative value.

LED idnicators

BLUE LED (Function):

  • short single blink after user long presses F1 button indicating button can be released
  • solid - waiting for user confirmation [of calibration] (either with F1 button or Enter in Platformio OpenOCD debugger virtual serial console) RED LED (Error):
  • solid/dim/flickering - Motion task CPU overrun (shall not happen)
  • slowly blinking every 1s- encoder initialization error
  • solid with short interruption every 1s - waiting for power supply voltage to be above 8V (checks voltage every 1s)

Calibration and first run

  1. On first start defualt parameters are loaded to be later stored in Flash.
  2. During first start two phases are briefly actuated and based on angle sensor movement motorParams.motorWiring is determined automatically.
  3. Next the controller automatically waits (blue LED on) for the user to confirm sensor calibration. Press F1 button to start calibration. The motor will be calibrated and values stored in Flash. Calibration can be repeated any time by long pressing F1 button until first short blink of the blue LED.
  4. Actuator physical values (gearing, torque, current, etc) need to be specified firmware/actuator_config.h. It affectes signal values read from CANbus to internal control. CANbus values are represented in actuator domain (i.e. considering motor gearbox). Change gearbox and final gear ratios in firmware/actuator_config.h file. Available parameters are rated_current, rated_torque, motor_gearbox_ratio, final_drive_ratio.
  5. Ądditionally one can extract sensor calibration values (point 3) from the Flash using readCalibration.py:
  • CalibrationPlot

CAN interface

Actuator accepts commands via CANbus as defined by dbc file in Retropilot/Opendbc/ocelot_controls.dbc

CAN Command - expect rate is 10ms

  • 0x22E (0558) STEERING_COMMAND
    • STEER_TORQUE (Nm)
    • STEER_ANGLE (deg)
    • STEER_MODE
      • 0 - "Off" - instant 0 torque
      • 1 - "TorqueControl" - uses STEER_TORQUE signal to control torque
      • 2 - "RelativeControl" To be DEPRICATED - uses STEER_ANGLE signal to control relative angle using PID and STEER_TORQUE as feedforward
      • 3 - "SoftOff" - ramp torque to 0 in 1s - meant to be used for coommunication error safe mode
    • COUNTER
    • CHECKSUM

Actuator will report back its status every 10ms:

  • 0x22F (0559) STEERING_STATUS
    • STEERING_ANGLE (deg)
    • STEERING_SPEED (rev/s)
    • STEERING_TORQUE (Nm)
    • CONTROL_STATUS
    • COUNTER
    • CHECKSUM
    • TEMPERATURE (C)
    • DEBUG_STATES

Interfacing with Openpilot

Reference implementation can be found in my bmw openpilot repo:

Interfacing from a PC

Use this StepperServoCANtester.py GUI program to quickly test the StepperServoCAN motor.

Requirements

pip install tkinter cantools python-can

git submodule init opendbc
git submodule update opendbc

Usage

  1. Connect the StepperServoCAN motor to the computer via CAN interface supported by python-can.

  2. Run the program:

    python StepperServoCANtester.py
    
  3. The program will automatically detect and display a list of available CAN interfaces to the user. In case multiple interfaces are available, the user will be prompted to select one.

  4. Upon connecting to the selected CAN interface, the program will present a user-friendly GUI with two input fields for setting the torque and angle values of the motor.

  5. The user can update the injected values to the motor by clicking the "Update Torque/Angle Value" button or by pressing the "Return" key on the keyboard.

  6. Once the user selects either "TorqueControl" and provides a "Steer Torque" value, or selects "RelativeControl" and provides a "Steer Angle" value, the StepperServoCAN motor will start spinning.

  7. To exit the program, the user can click the "Exit" button, press the "ESC" key, or simply close the window.

Notes

  • The torque value should be between -16 and 16, and the angle value should be between -4096 and 4096.
  • To use the program with SocketCAN on Linux, you can set up a virtual CAN interface with the following commands:
    sudo modprobe vcan
    sudo ip link add dev vcan0 type vcan
    sudo ip link set up vcan0
    ip -details -brief link | grep can | grep UP
    

If SocketCAN interfaces are not found, the program will abort.

Contributing

  • Develop using MISRA C:2012 standard and analyzed using Cppcheck. Project is preconfigured with (misra.json) for C/C++ Advanced Lint and SonarLint to highlight violations in VScode. Ask priv about the misra_rules_set_cppcheck.txt for cppcheck.

BSP Firmware License

  • The firmware is based on Misfittech project which is based on nano_stepper project and it inherited GPL V3 license
  • I continue GPL v3 license scheme as required for the software derivatives
  • Derivatives of this software under GPLv3 license will also need to remain open source if distributed commercially.

Atributions

For initial software and hardware:

About

Closed loop stepper motor controller with CANbus capabilities. This Project open source hardware and code, support platformio build and upload firmware.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 97.8%
  • Python 1.7%
  • Other 0.5%