From 1246539deaf7bb201e5a4b58915831dbf1fa77b8 Mon Sep 17 00:00:00 2001 From: Gabriel Koppenstein <82903393+gab-k@users.noreply.github.com> Date: Tue, 2 Aug 2022 00:24:27 +0200 Subject: [PATCH] Updated Software description (markdown) --- Software-description.md | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/Software-description.md b/Software-description.md index 36d7896..04c9c7b 100644 --- a/Software-description.md +++ b/Software-description.md @@ -13,13 +13,45 @@ The Software is structured as follows: Conveniently The Raspberry Pi Foundation provides a complete and well documented [SDK](https://datasheets.raspberrypi.com/pico/raspberry-pi-pico-c-sdk.pdf) for the RP2040. The SDK provides abstraction to a higher level and therefore there is no need to directly access any Registers directly. Everything is written using the SDK so hopefully the code is easier to understand. In the following, the motor control and the decoding of the DCC signals will be explained in particular. -Motor Control +Motor control overview ------------------------------------- ![Block Diagram](https://github.com/1nct/RP2040-Decoder/blob/main/docs/svg/Block_Diagram_Control.svg) +*** **Block diagram - Motor control** The above block diagram in essence describes the functionality of the control loop. While the central component is the PID controller, there are two other blocks that have an impact on the control output variable(pid.output). Firstly there is an additional block to counteract initial friction of the motor, this block only contributes to pid.output when the desired motor speed is very low. The second additional block establishes a baseline PWM offset which is necessary due to the motor having a certain threshold PWM level after which it starts spinning. The offset is adjusted when the absolute value of pid.e_sum reaches half of its limit. The speed helper block is corresponding to the speed_helper() function which sets the current target speed according to the configured deceleration/acceleration rates. + +PID Controller +------------------------------------- +![PID Controller](https://github.com/gab-k/RP2040-Decoder/blob/main/docs/svg/Block_Diagram_PID.svg) + +The block diagram above shows the PID controller in the s-domain. The derivative part contains an additional digital low pass filter used to filter out high-frequency noise. + +Output = E * (KP + KI/s + (KD*s)/(sτ+1)) = P + I + D + +* P = E * KP +* I = E * KI/s +* D = E * (KD*s)/(sτ+1) + +The continuous-time system representation can now be transformed into a discrete-time representation using bilinear transform [ s = 2*(z-1) / Ts*(z+1) ]. + +This results in the following difference equations which are then implemented in software: + +* pn = KP * en +* in = 0.5KI * (en + en-1) + in-1 +* dn = [ 2KD * (en - en-1) + (2τ - Ts) * dn-1 ] / [ 2τ + Ts] + +=> outputn = pn + in + dn + +Also note that instead of using derivative of error, derivative on measurement is used. This is done to prevent a high derivative part in the event of a large change in setpoint. + +![K_P](https://github.com/gab-k/RP2040-Decoder/blob/main/docs/svg/adaptive_kp.svg) + +Another aspect to consider is the implementation of gain scheduling. KP is a function of the current setpoint. Often it is favorable to have a higher proportional gain KP for slow speed. The illustration above shows the default setting. CV_54 & CV_55 are used to set KP @ x0, CV_56 & CV_57 for KP @ x1 and CV_58 & CV_59 for KP @ x2. Additionally CV_60 is used to shift x1 from the leftmost point (0% = 0/255) to the rightmost point (100% = 255/255). + + + DCC signal decoding ------------------------------------- The detection of the DCC is very simple, it works by looking at every rising and falling edge and calculating the time between them. When the time between rising and falling edge is greater than 87μs then this is equivalent to "0", else "1".