This commit is contained in:
2017-09-26 20:21:42 +02:00
parent e144bde202
commit 319ca0cbbb
194 changed files with 10149 additions and 0 deletions
@@ -0,0 +1,114 @@
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();
try {
while(e.MoveNext()) {
if(e.Current.key == key) {
readStack.Remove(e.Current);
}
}
} catch(InvalidOperationException) {
return;
}
}
}
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; }
}
}
@@ -0,0 +1,142 @@
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;
}
}
}
+35
View File
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace ebbits {
class Logger {
public static void Info(string msg) {
Logger.Message(msg, "INFO");
}
private static void Message(string msg, string type) {
DateTime d = DateTime.Now;
Console.WriteLine("[" + d.ToLongTimeString() + "." + d.Millisecond.ToString().PadLeft(3,'0') + "] " + type + ": " + msg);
}
public static void Notice(string msg) {
Logger.Message(msg, "NOTICE");
}
public static void Warn(string msg) {
Logger.Message(msg, "WARN");
}
public static void Message(string msg) {
Logger.Message(msg, "MESSAGE");
}
public static bool LogSerial { get; set; }
public static void Serial(string dir, string msg) {
if(LogSerial) {
Logger.Message(msg, "SERIAL "+dir.ToUpper());
}
}
}
}
+100
View File
@@ -0,0 +1,100 @@
/*- AdvancedServo simple -
0****************************************************************************************
* This example simply displays the Phidget AdvancedServo serial number when it is
* attached and sets the servo positions, velocity, and acceleration to 0. Then we will
* do a simple simulation of a basic movement of a servo motor at 100.00 velocity and
* 100.00 Acceleration. I decided to leave out the current change event handler for
* easier readability. For a more detailed example, see the Servo-full example.
*
* Please note that this example was designed to work with only one Phidget AdvanceServo
* connected.
* For an example showing how to use two Phidgets of the same time concurrently, please see the
* Servo-multi example in the Servo Examples.
* Copyright 2007 Phidgets Inc. All rights reserved.*/
using System;
using System.Collections.Generic;
using System.Text;
//Needed for the AdvancedServo class, phidget base classes, and PhidgetException class
//Needed for the event handling classes
using Phidgets.Events;
//Using this simply for the sleep() method so that the for loop will wail for the motor
//to finish moving to the previous new position before setting a new position
using System.Threading;
using ebbits.Robots;
using ebbits.Arduino;
namespace ebbits {
class Program {
static void Main(string[] args) {
Logger.LogSerial = false;
MainArduino a = new MainArduino("COM50");
//AbstractRobot l = new LaserBot(169861, a);
AbstractRobot p = new PaintBot(169889, a);
//AbstractRobot g = new GlassBot(169887);
Console.WriteLine("All Engaged. Press Key to Start.");
Console.ReadLine();
while(true) {
a.clearStack();
a.trainOn(1);
if(a.whereRead() != 5) {
Logger.Warn("Achtung! Steht nicht auf Start!");
//Console.ReadLine();
if(!a.findStart()) {
Logger.Warn("ABBRUCH! Konnte die Bahn nicht Finden oder einstellen!");
//Console.ReadLine();
continue;
}
}
while(a.trainSwitchIsOff()) {
Thread.Sleep(100);
}
Logger.Message("Move from Start to Welding Pos");
a.trainMove(70);
a.measureAndStop('r', 1);
Logger.Message("Welding");
Thread.Sleep(1000);//l.run();
Logger.Message("Move from Welding to Paint Pos");
a.trainMove(68);
a.measureAndStop('r', 2);
Logger.Message("Paint");
p.run();
Console.ReadLine();
Logger.Message("Move from Paint to Glas Pos");
a.trainMove(68);
a.measureAndStop('r', 3);
Logger.Message("Glas");
Thread.Sleep(1000);//g.run();
Logger.Message("Move from Glas to Turn around Pos");
a.trainMove(78);
a.measureAndStop('r',6);
Logger.Message("Turn aroud and move from Turn around to Waiting Pos");
System.Threading.Thread.Sleep(500);
a.changeDir(0);
a.trainMove(90);
a.readStop('r',4);
Logger.Message("Move from Waiting Pos to Start Pos");
a.trainMove(90);
a.readStop('r', 5);
Logger.Info("Runde zuende!");
}
}
}
}
@@ -0,0 +1,33 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("AdvancedServo-simple")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Phidgets Inc.")]
[assembly: AssemblyProduct("AdvancedServo-simple")]
[assembly: AssemblyCopyright("Copyright © Phidgets Inc. 2007")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("35c7dfbc-b70d-4603-9844-3fd32bce5b60")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Text;
using Phidgets;
using System.Threading;
namespace ebbits.Robots {
public abstract class AbstractRobot {
protected AdvancedServo servo;
protected int cservos;
protected string name;
private Thread t;
protected void init(int id) {
servo = new AdvancedServo();
servo.open(id);
Logger.Info("Waiting for Servo \"" + id + "\" is attached");
servo.waitForAttachment();
if(!servo.Attached) { throw new AccessViolationException("Robot not Found"); }
}
public abstract void run();
protected void setPosition(int[] pos, int speed) {
for(int i = 0; i < cservos; i++) {
if(pos[i] != -1) {
AdvancedServoServo s = servo.servos[i+ 5 - cservos];
s.Acceleration = speed;
s.Position = pos[i];
}
}
bool waiting = true;
while(waiting) {
waiting = false;
for(int i = 0; i < cservos; i++) {
if(pos[i] != -1) {
AdvancedServoServo s = servo.servos[i + 5 - cservos];
s.Position = pos[i];
try {
if(Math.Abs(s.Position - pos[i]) > 0.01) {
waiting = true;
}
} catch(PhidgetException) {
Logger.Warn("Exeption!");
s.Position = pos[i];
}
Thread.Sleep(10);
}
}
}
}
protected void setStartPosition(int[] positions, int sleep) {
for(int i = 0; i < cservos; i++) {
AdvancedServoServo s = servo.servos[i + 5 - cservos];
s.Type = ServoServo.ServoType.TOWERPRO_MG90;
s.Acceleration = 10000;
s.VelocityLimit = 200;
s.Position = positions[i];
s.Engaged = true;
}
Thread.Sleep(sleep);
}
protected void shutdownServo() {
for(int i = 0; i < cservos; i++) {
servo.servos[i + 5 - cservos].Engaged = false;
}
if(t.IsAlive) {
t.Abort();
}
}
protected void startPowerMess() {
this.t = new System.Threading.Thread(current_runnter);
t.Start();
}
private void current_runnter(object obj) {
DateTime t = DateTime.Now;
double current = 0;
while(true) {
double c = 0;
for(int i = 0; i < 5; i++) {
c += this.servo.servos[0].Current;
}
current += (c * 12 * (DateTime.Now.Ticks - t.Ticks)) / 1000000;
t = DateTime.Now;
Logger.Info(this.name+" Watt: " + current.ToString());
System.Threading.Thread.Sleep(100);
}
}
}
}
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace ebbits.Robots {
class GlassBot : AbstractRobot {
public GlassBot(int id) {
init(id);
this.cservos = 5;
base.name = "Glas";
}
public override void run() {
base.startPowerMess();
setStartPosition(new int[] { 20, 68, 175, 175, 20 }, 500);
setPosition(new int[] { 20, 68, 160, 162, 20 }, 400);
setPosition(new int[] { -1, 154, 88, 88, -1 }, 400);
setPosition(new int[] { -1, -1, 86, 116, 42 }, 200);
setPosition(new int[] { -1, -1, 77, -1, 48 }, 50);
setPosition(new int[] { 160, -1, -1, -1, -1 }, 200);
setPosition(new int[] { -1, -1, 85, -1, -1 }, 20);
setPosition(new int[] { -1, -1, -1, 106, -1 }, 200);
setPosition(new int[] { -1, -1, 118, 122, 174 }, 200);
setPosition(new int[] { -1, 13, -1, -1, -1 }, 300);
setPosition(new int[] { -1, -1, 90, 100, 20 }, 200);
setPosition(new int[] { -1, -1, 80, 94, 26 }, 20); // Fine Settings
System.Threading.Thread.Sleep(1000);
setPosition(new int[] { -1, -1, 90, 90, 20 }, 20); // Fine Lift
setPosition(new int[] { -1, -1, 103, 105, 80 }, 150);
setPosition(new int[] { -1, 154, 118, 122, 174 }, 300);
setPosition(new int[] { -1, -1, 86, 102, 42 }, 300);
setPosition(new int[] { -1, -1, 83, 113, 48 }, 100);
setPosition(new int[] { -1, -1, -1, 117, -1 }, 20);
setPosition(new int[] { 20, -1, 78, -1, -1 }, 150);
setPosition(new int[] { -1, -1, 81, 111, -1 }, 20);
setPosition(new int[] { -1, -1, 90, -1, -1 }, 100);
setPosition(new int[] { 20, 68, 160, 162, 20 }, 400);
setPosition(new int[] { 20, 68, 175, 175, 20 }, 400);
shutdownServo();
}
}
}
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;
using ebbits.Arduino;
namespace ebbits.Robots {
class LaserBot : AbstractRobot {
private Arduino.MainArduino Arduino;
public LaserBot(int id, MainArduino a) {
init(id);
base.cservos = 4;
this.Arduino = a;
base.name = "Laser";
}
override public void run() {
base.startPowerMess();
base.setStartPosition(new int[] {77, 175, 155, 84 }, 500);
base.setPosition(new int[] { 77, 157, 134, 84 }, 400);
base.setPosition(new int[] { 83, 96, 114, 44 }, 200);
Arduino.setLaser(MainArduino.ON);
base.setPosition(new int[] { 101, 91, 107, 45 }, 20);
base.setPosition(new int[] { 98, 78, 88, 49 }, 20);
base.setPosition(new int[] { 83, 82, 95, 46 }, 20);
base.setPosition(new int[] { 83, 96, 114, 44 }, 20);
Arduino.setLaser(MainArduino.OFF);
base.setPosition(new int[] { 77, 157, 134, 84 }, 400);
base.setPosition(new int[] { 77, 175, 155, 84 }, 400);
base.shutdownServo();
}
}
}
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Text;
using ebbits.Arduino;
namespace ebbits.Robots {
class PaintBot : AbstractRobot {
private MainArduino Arduino;
public PaintBot(int id, MainArduino a) {
init(id);
this.cservos = 4;
this.Arduino = a;
base.name = "Paint";
}
public override void run() {
base.startPowerMess();
setStartPosition(new int[] {87, 175, 160, 92 }, 500);
setPosition(new int[] {87, 153, 138, 92 }, 400);
setPosition(new int[] {47, 101, 158, 48 }, 200);
Arduino.setColor(MainArduino.RED);
setPosition(new int[] {91, 122, 161, 78 }, 50);
Arduino.setColor(MainArduino.OFF);
setPosition(new int[] {-1, 140, -1, -1 }, 200);
setPosition(new int[] {-1, 71, 55, 110 }, 200);
setPosition(new int[] {-1, 43, 42, 175 }, 200);
Arduino.setColor(MainArduino.RED);
setPosition(new int[] { 79, 42, 39, -1 }, 50);
setPosition(new int[] { 68, 39, 30, -1 }, 50);
setPosition(new int[] { -1, 36, 20, -1 }, 50);
setPosition(new int[] { 64, -1, -1, -1 }, 50);
Arduino.setColor(MainArduino.OFF);
setPosition(new int[] {-1, 44, -1, 150 }, 200);
setPosition(new int[] {57, 68, 70, 139 }, 200);
Arduino.setColor(MainArduino.WHITE);
setPosition(new int[] {53, 74, 79, 158 }, 50);
setPosition(new int[] {89, 86, 98, 150 }, 50);
setPosition(new int[] {-1, 75, 85, 143 }, 50);
setPosition(new int[] { 57, 68, 70, 139 }, 50);
Arduino.setColor(MainArduino.OFF);
setPosition(new int[] { -1, 153, 138, 92 }, 400);
setPosition(new int[] { 87, 175, 160, 92 }, 400);
shutdownServo();
}
}
}
+64
View File
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{94AEADEA-85AF-4DBD-AA16-4BE165FF538B}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ebbits</RootNamespace>
<AssemblyName>AdvancedServo-simple</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>2.0</OldToolsVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Phidget21.NET, Version=2.1.2.0, Culture=neutral, PublicKeyToken=c2b1f2a3594bce52, processorArchitecture=x86">
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Arduino\AbstractArduino.cs" />
<Compile Include="Arduino\MainArduino.cs" />
<Compile Include="Logger.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Robots\AbstractRobot.cs" />
<Compile Include="Robots\GlassBot.cs" />
<Compile Include="Robots\LaserBot.cs" />
<Compile Include="Robots\PaintBot.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
+20
View File
@@ -0,0 +1,20 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ebbits", "AdvancedServo-simple\ebbits.csproj", "{94AEADEA-85AF-4DBD-AA16-4BE165FF538B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{94AEADEA-85AF-4DBD-AA16-4BE165FF538B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{94AEADEA-85AF-4DBD-AA16-4BE165FF538B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{94AEADEA-85AF-4DBD-AA16-4BE165FF538B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{94AEADEA-85AF-4DBD-AA16-4BE165FF538B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal