By Mike Dodaro, translated by Marco Minerva from the original English version
Il modulo GHI Electronics Motor Driver L298 fornisce un metodo per impostare la velocità di due motori che possono funzionare contemporaneamente: MoveMotor(Gadgeteer.Modules.GHIElectronics.MotorControllerL298.Motor _motorSide, int _newSpeed). C’è anche un metodo per aumentare la velocità in un certo intervallo di tempo: MoveMotorRampNB(Gadgeteer.Modules.GHIElectronics.MotorControllerL298.Motor _motorSide, int _newSpeed, int _rampingDelayMilli). Questi metodi funzionano nel modo che ci si aspetta, tenendo in considerazione che il parametro velocità rappresenta la percentuale rispetto alla velocità massima.
Aggiungendo un potenziometro in un device .NET Gadgeteer, come il GHI Electronics Potentiometer module, l’utente può modificare manualmente la velocità dei motori. Un modo per recuperare il valore del potenziometro è usare un oggetto .NET Gadgeteer.Timer nell’applicazione ed impostare di conseguenza la velocità del motore nell’evento Gadgeteer.Timer.TickEventHandler.
La seguente immagine mostra i moduli inseriti nel .NET Gadgeteer Designer (fare clic per ingrandire).
Il file Program.cs con l’impostazione della velocità del motore nell’evento Timer.Tick è mostrato di seguito.
using Microsoft.SPOT; using GT = Gadgeteer; using Gadgeteer.Modules.GHIElectronics; namespace MotorDriver { public partial class Program { GT.Timer timer; void ProgramStarted() { button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed); timer = new GT.Timer(500); timer.Tick += new GT.Timer.TickEventHandler(timer_Tick); Debug.Print("Program Started"); } void timer_Tick(GT.Timer timer) { double percent = potentiometer.ReadPotentiometerPercentage(); motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor1, (int)(percent * 100)); } void button_ButtonPressed(Button sender, Button.ButtonState state) { if (!button.IsLedOn) { motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor1, 1); timer.Start(); button.TurnLEDOn(); } else { motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor1, 0); timer.Stop(); button.TurnLEDOff(); } } } }
Se vogliamo aumentare la velocità del motore in un certo intervallo di tempo (in millisecondi), possiamo usare il seguente codice nell’evento Button.ButtonPressed (e quindi disattivare il timer).
void button_ButtonPressed(Button sender, Button.ButtonState state) { if (!button.IsLedOn) { motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, 100, 5000); button.TurnLEDOn(); } else { motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, 0, 5000); button.TurnLEDOff(); } }
Vedi anche: .NET Gadgeteer Motor Driver con due motori e un potenziometro
Nota: alcuni utenti hanno segnalato che le etichette dei connettori sul modulo Motor Driver L298 sono invertite. Dopo qualche perplessità iniziale, ho dovuto collegare il motore ai connettori M2- e M2+ per far funzionare il codice sopra riportato. GHI risolverà il problema al più presto: http://www.tinyclr.com/forum/21/6413/