public void TestArduinoStateToggles() { ArduinoCommsBase motorArduino = new MotorControllerArduino(mLogger); TestDelegate connectDel = new TestDelegate(delegate() { motorArduino.StartArduinoComms(); }); Assert.AreEqual(motorArduino.ArduinoState, ProsthesisCore.Telemetry.ProsthesisTelemetry.DeviceState.Uninitialized); Assert.DoesNotThrow(connectDel); Assert.DoesNotThrow(delegate() { motorArduino.ToggleArduinoState(false); }); //Wait 100ms for the message to cycle System.Threading.Thread.Sleep(100); Assert.AreEqual(motorArduino.ArduinoState, ProsthesisCore.Telemetry.ProsthesisTelemetry.DeviceState.Disabled); Assert.DoesNotThrow(delegate() { motorArduino.ToggleArduinoState(true); }); //Wait 100ms for the message to cycle System.Threading.Thread.Sleep(100); Assert.AreEqual(motorArduino.ArduinoState, ProsthesisCore.Telemetry.ProsthesisTelemetry.DeviceState.Active); Assert.IsTrue(motorArduino.IsConnected); Assert.DoesNotThrow(delegate() { motorArduino.StopArduinoComms(true); }); Assert.IsFalse(motorArduino.IsConnected); Assert.AreEqual(motorArduino.ArduinoState, ProsthesisCore.Telemetry.ProsthesisTelemetry.DeviceState.Disconnected); }
public void TestArduinoTelemetryToggles() { MotorControllerArduino motorArduino = new MotorControllerArduino(mLogger); TestDelegate connectDel = new TestDelegate(delegate() { motorArduino.StartArduinoComms(); }); Assert.AreEqual(motorArduino.ArduinoState, ProsthesisCore.Telemetry.ProsthesisTelemetry.DeviceState.Uninitialized); Assert.DoesNotThrow(connectDel); Assert.IsFalse(motorArduino.TelemetryActive); Assert.DoesNotThrow(delegate() { motorArduino.ToggleArduinoState(false); }); //Wait 100ms for the message to cycle System.Threading.Thread.Sleep(100); Assert.IsFalse(motorArduino.TelemetryActive); Assert.AreEqual(motorArduino.ArduinoState, ProsthesisCore.Telemetry.ProsthesisTelemetry.DeviceState.Disabled); Assert.DoesNotThrow(delegate() { motorArduino.ToggleArduinoState(true); }); //Wait 100ms for the message to cycle System.Threading.Thread.Sleep(100); Assert.AreEqual(motorArduino.ArduinoState, ProsthesisCore.Telemetry.ProsthesisTelemetry.DeviceState.Active); long telemetryCount = 0; motorArduino.TelemetryUpdate += delegate(ProsthesisCore.Telemetry.ProsthesisTelemetry.ProsthesisMotorTelemetry telem) { telemetryCount++; }; const float cFastestTelemMS = 50; int fastestTelem = int.MaxValue; //Test at successively smaller telemetry periods. Find out where the arduino falls over, if at all for (int i = 1000; i >= cFastestTelemMS; i /= 2) { fastestTelem = i; Console.WriteLine(string.Format("Attemping to receive 10 seconds of telemetry at a period of {0}ms", i)); telemetryCount = 0; Assert.DoesNotThrow(delegate() { motorArduino.TelemetryToggle(i); }); //Wait 100ms for the message to cycle System.Threading.Thread.Sleep(100); Assert.IsTrue(motorArduino.TelemetryActive); System.Threading.Thread.Sleep(10000); Assert.DoesNotThrow(delegate() { motorArduino.TelemetryToggle(0); }); //Wait 100ms for the message to cycle System.Threading.Thread.Sleep(100); Assert.IsFalse(motorArduino.TelemetryActive); //Telemetry timing isn't a hard realtime component. We'll take receiving 95% of what we asked for as a pass bool passed = (float)telemetryCount * 0.95f <= 10.0f * 1000.0f / (float)i; if (passed || i > 50) { Assert.IsTrue(passed); } else { Console.Write(string.Format("Arduino failed to produce telemetry at {0}ms", i)); break; } } //Attempting 30 minutes at fastest allowed telem Console.WriteLine(string.Format("Attemping to receive 30 minutes of telemetry at a period of {0}ms with the device enabled", fastestTelem)); telemetryCount = 0; Assert.DoesNotThrow(delegate() { motorArduino.TelemetryToggle(fastestTelem); }); Assert.DoesNotThrow(delegate() { motorArduino.ToggleArduinoState(true); }); //Wait 100ms for the message to cycle System.Threading.Thread.Sleep(100); Assert.IsTrue(motorArduino.TelemetryActive); //30 minute sleep Console.WriteLine("\nSystem will now perform a 30 minute soak test to count telemetry packets at the fastest recorded rate. Press ENTER to run this test and any other key to skip it"); bool doSoak = Console.ReadKey().Key == ConsoleKey.Enter; if (doSoak) { System.Threading.Thread.Sleep(30 * 60 * 1000); //Telemetry timing isn't a hard realtime component. We'll take receiving 95% of what we asked for as a pass bool passedSoak = (float)telemetryCount * 0.95f <= 1800.0f * 1000.0f / (float)fastestTelem; Assert.IsTrue(passedSoak); Assert.DoesNotThrow(delegate() { motorArduino.TelemetryToggle(0); }); //Wait 100ms for the message to cycle System.Threading.Thread.Sleep(100); Assert.IsFalse(motorArduino.TelemetryActive); Assert.DoesNotThrow(delegate() { motorArduino.TelemetryToggle(0); }); //Wait 100ms for the message to cycle System.Threading.Thread.Sleep(100); Assert.IsFalse(motorArduino.TelemetryActive); } Assert.IsTrue(motorArduino.IsConnected); Assert.DoesNotThrow(delegate() { motorArduino.StopArduinoComms(true); }); Assert.IsFalse(motorArduino.IsConnected); Assert.IsFalse(motorArduino.TelemetryActive); Assert.AreEqual(motorArduino.ArduinoState, ProsthesisCore.Telemetry.ProsthesisTelemetry.DeviceState.Disconnected); if (fastestTelem > cFastestTelemMS) { Assert.Inconclusive(); } }
static void Main(string[] args) { mTelemetryBroadcastTimer = new Timer(kTelemetryBroadcastPeriod); mTelemetryBroadcastTimer.AutoReset = true; mTelemetryBroadcastTimer.Elapsed += OnTelemetryPublishAlarm; mTelemetryBroadcastTimer.Start(); string fileName = string.Format("Arduino-comms-{0}.txt", System.DateTime.Now.ToString("dd MM yyyy HH-mm-ss")); ProsthesisCore.Utility.Logger logger = new ProsthesisCore.Utility.Logger(fileName, true); System.DateTime start = System.DateTime.Now; string telemFileName = string.Format("Arduino-telem-{0}.csv", System.DateTime.Now.ToString("dd MM yyyy HH-mm-ss")); System.IO.TextWriter writer = new System.IO.StreamWriter(telemFileName); ArduinoCommsBase.InitializeSerialConnections(logger); MotorControllerArduino test = new MotorControllerArduino(logger); writer.WriteLine("Timestamp(ms), Duty Cycle 1, Duty Cycle 2, Pressure 1, pressure 2, P, I, D"); test.TelemetryUpdate += new Action <ProsthesisCore.Telemetry.ProsthesisTelemetry.ProsthesisMotorTelemetry>(delegate(ProsthesisCore.Telemetry.ProsthesisTelemetry.ProsthesisMotorTelemetry obj) { double ts = (System.DateTime.Now - start).Duration().TotalMilliseconds; mTelem.MotorTelem = obj; string telemRow = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}", ts, obj.MotorDutyCycles[1], obj.MotorDutyCycles[0], obj.OutputPressure[1], obj.OutputPressure[0], obj.ProportionalTunings[1], obj.IntegralTunings[1], obj.DifferentialTunings[1], obj.PressureSetPoints[0], obj.PressureSetPoints[1]); Console.WriteLine(telemRow); writer.WriteLine(telemRow); }); bool telemEnable = false; bool arduinoState = false; if (test.StartArduinoComms()) { Console.WriteLine("Press 'x' to exit. 'T' to enable telemetry, 'E' to toggle device state"); ConsoleKey key = ConsoleKey.A; test.ToggleHeartbeat(true, 1000, 3); do { System.Threading.Thread.Sleep(16); if (Console.KeyAvailable) { key = Console.ReadKey().Key; if (key == ConsoleKey.T) { telemEnable = !telemEnable; test.TelemetryToggle(telemEnable ? 25 : 0); } else if (key == ConsoleKey.E) { arduinoState = !arduinoState; test.ToggleArduinoState(arduinoState); } } }while (key != ConsoleKey.X && test.IsConnected); test.StopArduinoComms(true); } else { Console.WriteLine("Failed to connect to the correct Arduino. Press any key to exit"); do { System.Threading.Thread.Sleep(16); }while (!Console.KeyAvailable); } mTelemetryBroadcastTimer.Start(); writer.Close(); logger.ShutDown(); }