iforce2d / weenyPRU

LinuxCNC component and firmware for Raspberry Pi to communicate with a STM32 microcontroller.
GNU General Public License v2.0
49 stars 9 forks source link

'weeny' PRU for LinuxCNC on Raspberry Pi

This is a LinuxCNC component and firmware that uses the SPI bus on a Raspberry Pi to communicate with a STM32 microcontroller as a programmable realtime unit, for motor control, digital I/O, spindle PWM etc.

This project was inspired by and uses some code from the 'Remora' system https://github.com/scottalford75/Remora

The firmware for the realtime unit targets STM32F103C8 aka 'Blue Pill'. Source code includes a project file for STM32CubeIDE.

Unlike Remora, this firmware is not conveniently configurable by SD card, so all modifications will need to be changed in the source code.

Tested on Raspberry Pi 3B, 3B+, 4B and ZeroW with 4.19.71 PREEMPT, LinuxCNC 2.8.4


Features


About step pulse length

The default step pulse length (the time the pin is held high) is set to 4 microseconds. You can change this in the call to __HAL_TIM_SET_COMPARE in stepgen.c if your stepper drivers require a longer pulse. Keep in mind that at 50kHz there is only 20 microseconds available for a full pulse cycle.


About analog input

The analog inputs are intended for use as a joystick for jogging two axes. To use this correctly, the joystick should not be moved for 10 seconds on startup, to calibrate the center position value. There is a sizeable deadband enforced so that small jitters around the center position do not cause joint movement, but it would still be wise to use a switch to only enable joystick jogging when actually intended.


About connection status LED

Pin PC13 is used to indicate that SPI connection to Raspberry Pi is established. This LED should light up when you remove e-stop, and remain lit while SPI connection is active.


About RGB LEDs

The WS2812 protocol is used to control up to 16 addressable RGB LEDs on PB5. Although these are not officially supposed to work with 3.3V signals, in my experience many variants do actually work just fine. You can control the individual red/green/blue components of each LED (simply on or off, no gradual dimming) via HAL pins. Note that when SPI connection is lost, all RGB LEDs will also turn off.


About TMC2209 UART control

On a v1.2 board, Trinamic TMC2209 stepper drivers can be controlled via UART to alter microstep and RMS current settings. To do this, bridge the 'UART' jumper. This also requires that UART control is enabled in the firmware (which is the default for the pre-built 'v1.2' binary).

The default values are 8x microstepping and 200mA current. Changes are made by setting pin values, eg.

setp  weeny.tmc.0.microsteps  16
setp  weeny.tmc.0.current     400

Bridge the MS1/MS2 pins to set the address of each driver as desired. The example above will affect all drivers with both MS1 and MS2 left open (address 0).

Microsteps should be one of 0, 2, 4, 8, 16, 32, 63, 128, 256

Current should be a value between 0-2000 (milliamps). Note that this is an approximate setting, as the TMC2209 can only control current in 32 distinct steps and will choose whatever is the closest value it can actually perform.

Changes will be relayed to the drivers at a rate of once per second.

This was tested with these drivers, which worked out of the box without needing any solder pads bridged. I also tried another variant which ignored all attempts to change settings via UART (and didn't appear to have any empty solder pads to bridge), so for now I can only recommend this MKS one.

alt text


About I2C pressure sensor

On a v1.2 board, you can read values from a XGZP I2C pressure sensor. To do this, bridge both the 'I2C' jumpers, which will connect pullups to pins D13 and D14. This also requires that XGZP usage is enabled in the firmware (which is the default for the pre-built 'v1.2' binary).

You can then connect your XGZP sensor with:

The pressure value will appear as the HAL pin 'weeny.pressure.0' in units of kPa, with update rate of about 100Hz.


LinuxCNC component

To install the LinuxCNC component, invoke halcompile from the components/weeny folder like this:

sudo halcompile --install weeny.c

These pins are created by the component:

See components for more info.