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: