private void StartMotor() { KickstartActive = false; if (MotorPower == 0) { StopMotor(); } else { SetMotorPower(MotorPower); if (MaxRunTimeMs > 0) { AlarmHandler.UnregisterAlarm(StartMotor); AlarmHandler.RegisterAlarm(MaxRunTimeMs, StopMotor); } } }
/// <summary> /// Sets the power of the motor.<br/> /// Value range for Power is 0-255. Power will be scaled using the MinPower and MaxPower settings. /// </summary> /// <param name="Power">Power of the gear motor.</param> public void SetPower(int Power) { if (this.Power != Power) { MotorPower = Power; if (!KickstartActive) { if (KickstartPower > 0 && Power < KickstartPower && KickstartDurationMs > 0) { KickstartActive = true; SetValue(KickstartPower); AlarmHandler.RegisterAlarm(KickstartDurationMs, StartMotor); } else { StartMotor(); } } } }
/// <summary> /// Updates the output of the toy. /// </summary> public override void UpdateOutputs() { if (Output != null) { int P = FadingCurve.MapValue(GetResultingValue().Limit(0, 255)); if (P != 0) { P = ((int)((double)(MaxPower >= MinPower ? MaxPower - MinPower : MinPower - MaxPower) / 255 * P) + MinPower).Limit(MinPower, MaxPower); } if (P == 0) { TurnedOffAfterMaxRunTime = false; } if (!TurnedOffAfterMaxRunTime) { if (CurrentMotorPower == 0) { //Motor is currently off if (P > 0) { //need to turn the motor on if (KickstartDurationMs > 0 && KickstartPower > 0 && P <= KickstartPower) { //Kickstart is defined, start with kickstart TargetMotorPower = P; if (!KickstartActive) { CurrentMotorPower = KickstartPower; Output.Value = (byte)CurrentMotorPower; KickstartActive = true; AlarmHandler.RegisterAlarm(KickstartDurationMs, KickStartEnd); } } else { //Just turn the motor on CurrentMotorPower = P; TargetMotorPower = P; Output.Value = (byte)P; KickstartActive = false; } if (MaxRunTimeMs > 0) { AlarmHandler.RegisterAlarm(MaxRunTimeMs, MaxRunTimeMotorStop); } } } else if (KickstartActive) { //Motor is in kickstart phase if (P > 0) { //Need to change the motor power TargetMotorPower = P; } else { //Turn off motor AlarmHandler.UnregisterAlarm(KickStartEnd); AlarmHandler.UnregisterAlarm(MaxRunTimeMotorStop); TargetMotorPower = 0; CurrentMotorPower = 0; Output.Value = 0; } } else { //Motor is on if (P == 0) { //Turn of motor AlarmHandler.UnregisterAlarm(KickStartEnd); AlarmHandler.UnregisterAlarm(MaxRunTimeMotorStop); TargetMotorPower = 0; CurrentMotorPower = 0; Output.Value = 0; } else if (P != CurrentMotorPower) { //Power has changed CurrentMotorPower = P; TargetMotorPower = P; Output.Value = (byte)P; } } } } }