# The Transfer Function

## Continuous Transfer Function

A ramp-up / ramp-down feature is implemented using a first order transfer function. This saves the motor more than a linear ramp-up / ramp-down.

Y(s)/U(s) = 10 / [1s+1]

The continuous function has a time constant of 1 second and therefore it takes the motor about 5 seconds to reach its desired speed. Also it spits out numbers from 0..1000 if the input is the percentage of the desired motor speed. This is useful as the hpwm duty cycle is a 10bit value (0..1023). So if the output of the function is close to 1000, the motor is just set to full speed.

## Discrete Transfer Function

The discrete implementation of the transfer function has a loop time of 100ms.

Y(z)/U(z) =  0.9516 / [z – 0.9048]

Y(z)[1 – 0.9048*z^(-1)] = U(z)[0.9516*z^(-1)]

Y(new) = [ (U-1)*0.9516 + (Y-1)*0.9048 ]

## Implementation with Integer Math

Because the PICAXE only supports integers, the discrete transfer function had to be scaled up to make full use of the range of values available in an integer.

Y(new) = [ (U-1)*9516*(1/10,000) + (Y-1)*9048*(1/10,000) ]

Y(new) = [ (U-1)*156*61*(1/10,000) + (Y-1)*156*58*(1/10,000) ]

Y(new) = [ ( [ (U-1)*61 + (Y-1)*58 ] / 200 ) * 156 ] / 50

Furthermore, a short dead zone was implemented in the transfer function output to save the MOSFETs. Once the transfer function reaches a value under twenty percent, the PWM duty cycle sent to the motor is driven to zero while the transfer function is still decreasing. That also allows the MOSFETs to be completely switched off before the motor changes directions.

Also, when the transfer function reaches almost full speed, the value is just set to full speed to prevent the MOSFETs from having to switch off for an only short period of time.

The implementation of the final transfer function is the following: ``` ; ----------------------------------------------------------- ; This subroutine calculates the duty cycle of the hpwm ; with a first order transfer function. ; time constant  = 1s ; gain           = 10 ; ; input: ;    setpoint    percentage of desired motor speed (0%...100%) ; output: ;    tf_value     ramped speed of the motor (0...1000) ; ----------------------------------------------------------- tf_var1 = 61 * setpoint          ; 6,100 Max tf_var2 = tf_value * 58          ; 5,800 Max tf_var1 = tf_var1 + tf_var2      ; 64,100 Max tf_var1 = tf_var1 / 200          ; 320 Max tf_var1 = tf_var1 * 156          ; 49998 Max tf_value = tf_var1 / 50          ; Theoretically 999 Max, Really 960 ; set duty cycle to 1023 when tf_value >= 960 ```