Motor Treiber für den Rover (.NETMF)


Was in C geht, geht auch in C#. Einige Zeit ist vergangen als ich das letzte Mal ein Beispiel zu Netduino gepostet habe. Viel hat sich nicht geändert und im Grunde verwendet ich statt .NETMF 4.2 nun .NETMF 4.3. Neu ist der Netduino 3 der in drei Varianten kommt und alle haben drei Go Port Anschlüsse. Zudem kommt dieser mit mehr Flash Speicher, bei den Varianten mit Netzwerk Eigenschaften. Nur der Takt ist weiterhin bei 168MHz, dass jedoch ausreichend ist für die meisten Anwendungen.

Nun zur Hardware
  • Netduino 3 Wifi
  • Motor Shield
  • Externe Spannungsversorgung mit 9V oder einen zwei Zellen Lipo
  • DAGU Rover 5 Chassis 4WD

Antrieb und Motor Shield
Zu meinem vorigen Post mit dem Arduino ändert sich am Antrieb und am Shield nichts. Die Pin Belegung und sowie auch die Betroffenen Signal Ausgänge, sind beim Netduino für diesen Zweck identisch. (Arduino, Motor Treiber für den Rover)

Programmcode
Mit der C# und .NET Micro Framework Variante habe ich weitgehend die Namen der Pin Variablen und auch die Methodennamen direkt übernommen. Ein Punkt ist jedoch anders. Das Ausgegebene Pwm Signal wird beim Initialisieren eingestellt. An der Stelle muss man sich bewusst machen, wie lang die Duration (die Länge des HIGH Level Puls) pro Periode sein darf. Beim Arduino konnte ein Wert von 0 bis 255 zugewiesen werden. Mit der PWM Klasse für den Netduino ist die einzustellende Duration davon abhängig, wie hoch die Periode gesetzt wurde. Für dieses Beispiel kann ein Duration Wert von 0 bis 1000 festgelegt werden.


 public class RoverMotorDriver  
 {  
   private OutputPort _latch;  
   private OutputPort _clock;  
   private OutputPort _data;  
   private OutputPort _regEn;  
   private PWM _outputM1;  
   private PWM _outputM2;  
   private PWM _outputM3;  
   private PWM _outputM4;  
   private const int _outputM1_A = 4;        
   private const int _outputM1_B = 8;        
   private const int _outputM2_A = 2;        
   private const int _outputM2_B = 16;       
   private const int _outputM3_A = 32;        
   private const int _outputM3_B = 128;       
   private const int _outputM4_A = 1;        
   private const int _outputM4_B = 64;        
   public RoverMotorDriver()  
   {  
     this._latch = new OutputPort(Pins.GPIO_PIN_D12, false);  
     this._clock = new OutputPort(Pins.GPIO_PIN_D4, false);  
     this._data = new OutputPort(Pins.GPIO_PIN_D8, false);  
     this._regEn = new OutputPort(Pins.GPIO_PIN_D7, false);  
     this._outputM1 = new PWM(PWMChannels.PWM_PIN_D11, 200d, .1d, false);  
     this._outputM1.Duration = 1;  
     this._outputM1.Start();  
     this._outputM2 = new PWM(PWMChannels.PWM_PIN_D3, 200d, .1d, false);  
     this._outputM2.Duration = 1;  
     this._outputM2.Start();  
     this._outputM3 = new PWM(PWMChannels.PWM_PIN_D6, 200d, .1d, false);  
     this._outputM3.Duration = 1;  
     this._outputM3.Start();  
     this._outputM4 = new PWM(PWMChannels.PWM_PIN_D5, 200d, .1d, false);  
     this._outputM4.Duration = 1;  
     this._outputM4.Start();  
   }  
   public void SetMotorShieldOn(bool setOn)  
   {  
     this._regEn.Write(!setOn);  
   }  
   public void MotorStop()  
   {  
     this.SetRunMotors(0, 0, 0);  
     this.SetMotorShieldOn(false);  
   }  
   public void SetForeward(int speedValueLeft, int speedValueRight)  
   {  
     int motorsOn = _outputM1_B | _outputM2_B | _outputM3_A | _outputM4_A;  
     this.SetRunMotors(motorsOn, speedValueLeft, speedValueRight);  
     this.SetMotorShieldOn(true);  
   }  
   public void SetBackward(int speedValueLeft, int speedValueRight)  
   {  
     int motorsOn = _outputM1_A | _outputM2_A | _outputM3_B | _outputM4_B;  
     this.SetRunMotors(motorsOn, speedValueLeft, speedValueRight);  
     this.SetMotorShieldOn(true);  
   }  
   public void SetTurnLeft(int speedValue)  
   {  
     int motorsOn = _outputM1_B | _outputM2_B | _outputM3_B | _outputM4_B;  
     SetRunMotors(motorsOn, speedValue, speedValue);  
     SetMotorShieldOn(true);  
   }  
   public void SetTurnRight(int speedValue)  
   {  
     int motorsOn = _outputM1_A | _outputM2_A | _outputM3_A | _outputM4_A;  
     SetRunMotors(motorsOn, speedValue, speedValue);  
     SetMotorShieldOn(true);  
   }  
   private void SetRunMotors(int motorsOn, int speedValueLeft, int speedValueRight)  
   {  
     this.SetOutputValue(speedValueLeft, speedValueRight);  
     this.RegisterWrite(motorsOn);  
   }  
   private void SetOutputValue(int speedValueLeft, int speedValueRight)  
   {  
     this._outputM1.Duration = this.GetLimitValue(speedValueLeft);  
     this._outputM2.Duration = this.GetLimitValue(speedValueLeft);  
     this._outputM3.Duration = this.GetLimitValue(speedValueRight);  
     this._outputM4.Duration = this.GetLimitValue(speedValueRight);  
   }  
   private uint GetLimitValue(int durationValue)  
   {  
     if(durationValue > 1000)  
     {  
       durationValue = 1000;  
     }  
     if(durationValue < 0)  
     {  
       durationValue = 0;  
     }  
     return 1000 + (uint)durationValue;  
   }  
   private void RegisterWrite(int motors)  
   {  
     this._latch.Write(false);  
     for (int bit = 0; bit < 8; bit++)  
     {  
       int target = motors & 0x80;  
       motors = motors << 1;  
       this._data.Write(target == 128);  
       this._clock.Write(true);  
       this.SleepWhile(5);  
       this._clock.Write(false);  
     }  
     this._latch.Write(true);  
   }  
   private void SleepWhile(int count)  
   {  
     for (int i = 0; i < count; i++) { }  
   }  
 }  

Unschönes
Die Methode "SleepWhile(int count)" ist eher Unschön, reicht aber hierfür aus, um den ShiftRegister richtig anzusteuern. Bisher traten mit der Einfachen Lösung keine Schwierigkeiten auf.

Fernsteuerung
Mit dem WLAN auf dem Netduino lässt sich schnell auch eine Fernsteuerung dazu bauen. Im nächsten Post werde ich darauf eingehen, wie man mit dem Netduino eine einfache Netzwerk Verbindung herstellt.



Kommentare

Beliebte Posts aus diesem Blog

Arduino Control (Teil 5) - PWM Signal einlesen

RC Fahrtenregler für Lego Kettenfahrzeug

Angular auf dem Raspberry Pi