Powermeasurement

This commit is contained in:
2015-12-01 15:58:07 +00:00
parent 662c59de0d
commit fac00f4a2a
10 changed files with 574 additions and 241 deletions
@@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Text.RegularExpressions;
namespace ebbits.Arduino {
abstract class AbstractArduino {
protected SerialPort arduino;
protected LinkedList<ArduinoInput> readStack = new LinkedList<ArduinoInput>();
protected void init(string port) {
arduino = new SerialPort(port, 57600);
arduino.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
arduino.Open();
arduino.WriteLine("");
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) {
SerialPort sp = (SerialPort)sender;
string s = sp.ReadLine();
Logger.Serial("in", s);
if(ArduinoInput.check(s)) {
readStack.AddLast(new ArduinoInput(s));
}
}
protected void serialSend(char key, byte pos, int val) {
char c = ArduinoInput.checksum(key + pos.ToString() + "=" + val.ToString());
string s = "*" + key + pos.ToString() + "=" + val + "$" + c + "#";
arduino.WriteLine(s);
Logger.Serial("out", s);
}
protected ArduinoInput getInput(char key, byte id = 255) {
LinkedList<ArduinoInput>.Enumerator e = readStack.GetEnumerator();
try {
while(e.MoveNext()) {
if((e.Current.key == key && e.Current.id == id) || (id == 255 && e.Current.key == key)) {
ArduinoInput a = e.Current;
readStack.Remove(a);
return a;
}
}
} catch(InvalidOperationException) {
return null;
}
return null;
}
protected ArduinoInput getInputWait(char key, byte id = 255) {
while(true) {
ArduinoInput a = getInput(key, id);
if(a != null) {
return a;
}
System.Threading.Thread.Sleep(1);
}
}
internal void clearStack() {
readStack.Clear();
}
internal void clearStack(char key) {
LinkedList<ArduinoInput>.Enumerator e = readStack.GetEnumerator();
while(e.MoveNext()) {
if(e.Current.key == key) {
readStack.Remove(e.Current);
}
}
}
}
class ArduinoInput {
public ArduinoInput(string indata) {
string reg = "\\*([a-z])(\\d)=([a-fA-F0-9]*)\\$(.)#";
Match m = Regex.Match(indata, reg);
this.key = m.Groups[1].ToString().ToCharArray()[0];
this.id = byte.Parse(m.Groups[2].ToString());
string val = m.Groups[3].ToString().ToUpper();
if(val == "") {
this.value = 0;
} else if(val.IndexOfAny(new char[] { 'A', 'B', 'C', 'D', 'E', 'F' }) != -1) {
this.value = Int64.Parse(val, System.Globalization.NumberStyles.HexNumber);
} else {
this.value = Int64.Parse(val);
}
}
public static char checksum(String data) {
char c = '$';
foreach(char t in data) {
c = (char)(c ^ t);
}
return c;
}
public static bool check(string indata) {
string reg = "\\*([a-z]\\d)=([a-fA-F0-9]*)\\$(.)#";
Match m = Regex.Match(indata, reg);
if(!m.Success) {
return false;
}
String rec = m.Groups[1].ToString() + "=" + m.Groups[2].ToString();
return (ArduinoInput.checksum(rec) == m.Groups[3].ToString().ToCharArray()[0]);
}
public char key { get; private set; }
public byte id { get; private set; }
public Int64 value { get; private set; }
}
}
+137
View File
@@ -0,0 +1,137 @@
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() {
serialSend('b', 2, 0); //Bremse aus
}
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();
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();
readStop('r',4);
trainMove();
readStop('r',5);
break;
case 4:
Logger.Info("Fahre von 4 nach 5");
trainMove();
readStop('r',5);
break;
case 3:
Logger.Info("Fahre von 3 nach 5");
trainMove();
readStop('r',2);
trainMove();
readStop('r',1);
trainMove();
readStop('r',5);
break;
case 2:
Logger.Info("Fahre von 2 nach 5");
trainMove();
readStop('r',1);
trainMove();
readStop('r',5);
break;
case 1:
Logger.Info("Fahre von 1 nach 5");
trainMove();
readStop('r',5);
break;
}
trainOn(1);
Logger.Info("Stehe nun auf: " + this.whereRead());
return (this.whereRead() == 5);
}
}
}