using System; using System.Collections.Generic; using System.Text; namespace ebbits.Arduino { class MainArduino : AbstractArduino { public const int ON = 1; public const int OFF = 0; public const int RED = 2; public const int WHITE = 7; public MainArduino(string port) { init(port); trainOn(1); } public void setLaser(byte p) { serialSend('l', 1, p); } public void setColor(byte p) { serialSend('c', 1, p); } public void trainOn(byte dir) { serialSend('p', 2, 70); //Speed = 1 changeDir(dir); //Richtung = 1 serialSend('b', 2, 1); //Bremse = an } public void trainMove(byte speed) { serialSend('b', 2, 0); //Bremse aus serialSend('p', 2, speed); } internal void changeDir(byte dir) { serialSend('d', 2, dir); //Richtung = 0 } public bool readStop(char key, byte id) { ArduinoInput a = base.getInputWait(key, id); return true; } public void measureAndStop(char key, byte id) { System.Threading.Thread t = new System.Threading.Thread(current_runnter); t.Start(); this.readStop(key, id); t.Abort(); base.clearStack('u'); } private void current_runnter(object obj) { DateTime t = DateTime.Now; float current = 0; while(true) { long c = readCurrent(); current += ((float)c / 1000 * 12 * (DateTime.Now.Ticks - t.Ticks)) / 1000000; t = DateTime.Now; Logger.Info("Train Watt: " + current.ToString()); System.Threading.Thread.Sleep(100); } } public long whereRead() { serialSend('q', 0, 0); ArduinoInput a = base.getInputWait('q'); return a.value; } public long readCurrent() { serialSend('u', 1, 0); ArduinoInput a = base.getInputWait('u'); return a.value; } public bool findStart() { changeDir(0); if(this.whereRead() == 0) { trainMove(70); System.Threading.Thread.Sleep(3000); } if(this.whereRead() == 0) { changeDir(1); System.Threading.Thread.Sleep(3000); } if(this.whereRead() == 0) { trainOn(1); return false; } trainOn(1); changeDir(0); Logger.Info("Lock gefunden und stehe auf: " + this.whereRead()); base.clearStack(); switch(this.whereRead()) { case 6: Logger.Info("Fahre von 6 nach 5"); trainMove(70); readStop('r',4); trainMove(70); readStop('r',5); break; case 4: Logger.Info("Fahre von 4 nach 5"); trainMove(70); readStop('r',5); break; case 3: Logger.Info("Fahre von 3 nach 5"); trainMove(70); readStop('r',2); trainMove(70); readStop('r',1); trainMove(70); readStop('r',5); break; case 2: Logger.Info("Fahre von 2 nach 5"); trainMove(70); readStop('r',1); trainMove(70); readStop('r',5); break; case 1: Logger.Info("Fahre von 1 nach 5"); trainMove(70); readStop('r',5); break; } trainOn(1); Logger.Info("Stehe nun auf: " + this.whereRead()); return (this.whereRead() == 5); } public bool trainSwitchIsOff() { serialSend('s', 0, 0); ArduinoInput a = base.getInputWait('s'); return a.value == 0; } } }