Skip to content

Commit

Permalink
Beginning of q learning
Browse files Browse the repository at this point in the history
  • Loading branch information
thewithz committed Feb 27, 2020
1 parent 7e7f135 commit 1f3366e
Show file tree
Hide file tree
Showing 33 changed files with 2,104 additions and 1,131 deletions.
59 changes: 46 additions & 13 deletions OdysseyNow/.idea/.idea.OdysseyNow/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 26 additions & 53 deletions OdysseyNow/Assets/Ardity/Scripts/Threads/AbstractSerialThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

using UnityEngine;

using System;
using System.IO;
using System.IO.Ports;
Expand All @@ -21,8 +20,7 @@
* open this file unless you are introducing incompatibilities for upcoming
* versions.
*/
public abstract class AbstractSerialThread
{
public abstract class AbstractSerialThread {
// Parameters passed from SerialController, used for connecting to the
// serial device as explained in the SerialController documentation.
private string portName;
Expand Down Expand Up @@ -66,8 +64,7 @@ public AbstractSerialThread(string portName,
int baudRate,
int delayBeforeReconnecting,
int maxUnreadMessages,
bool enqueueStatusMessages)
{
bool enqueueStatusMessages) {
this.portName = portName;
this.baudRate = baudRate;
this.delayBeforeReconnecting = delayBeforeReconnecting;
Expand All @@ -81,10 +78,8 @@ public AbstractSerialThread(string portName,
// ------------------------------------------------------------------------
// Invoked to indicate to this thread object that it should stop.
// ------------------------------------------------------------------------
public void RequestStop()
{
lock (this)
{
public void RequestStop() {
lock (this) {
stopRequested = true;
}
}
Expand All @@ -95,8 +90,7 @@ public void RequestStop()
// type of the returned object.
// It returns null if no message has arrived since the latest invocation.
// ------------------------------------------------------------------------
public object ReadMessage()
{
public object ReadMessage() {
if (inputQueue.Count == 0)
return null;

Expand All @@ -108,8 +102,7 @@ public object ReadMessage()
// output queue, later the method 'RunOnce' reads this queue and sends
// the message to the serial device.
// ------------------------------------------------------------------------
public void SendMessage(object message)
{
public void SendMessage(object message) {
outputQueue.Enqueue(message);
}

Expand All @@ -124,25 +117,20 @@ public void SendMessage(object message)
// device, reading messages and sending messages. This loop can be stopped
// by invoking 'RequestStop'.
// ------------------------------------------------------------------------
public void RunForever()
{
public void RunForever() {
// This 'try' is for having a log message in case of an unexpected
// exception.
try
{
while (!IsStopRequested())
{
try
{
try {
while (!IsStopRequested()) {
try {
AttemptConnection();

// Enter the semi-infinite loop of reading/writing to the
// device.
while (!IsStopRequested())
RunOnce();
}
catch (Exception ioe)
{
catch (Exception ioe) {
// A disconnection happened, or there was a problem
// reading/writing to the device. Log the detailed message
// to the console and notify the listener.
Expand All @@ -165,26 +153,23 @@ public void RunForever()

// Before closing the COM port, give the opportunity for all messages
// from the output queue to reach the other endpoint.
while (outputQueue.Count != 0)
{
while (outputQueue.Count != 0) {
SendToWire(outputQueue.Dequeue(), serialPort);
}

// Attempt to do a final cleanup. This method doesn't fail even if
// the port is in an invalid status.
CloseDevice();
}
catch (Exception e)
{
catch (Exception e) {
Debug.LogError("Unknown exception: " + e.Message + " " + e.StackTrace);
}
}

// ------------------------------------------------------------------------
// Try to connect to the serial device. May throw IO exceptions.
// ------------------------------------------------------------------------
private void AttemptConnection()
{
private void AttemptConnection() {
serialPort = new SerialPort(portName, baudRate);
serialPort.ReadTimeout = readTimeout;
serialPort.WriteTimeout = writeTimeout;
Expand All @@ -197,17 +182,14 @@ private void AttemptConnection()
// ------------------------------------------------------------------------
// Release any resource used, and don't fail in the attempt.
// ------------------------------------------------------------------------
private void CloseDevice()
{
private void CloseDevice() {
if (serialPort == null)
return;

try
{
try {
serialPort.Close();
}
catch (IOException)
{
catch (IOException) {
// Nothing to do, not a big deal, don't try to cleanup any further.
}

Expand All @@ -217,10 +199,8 @@ private void CloseDevice()
// ------------------------------------------------------------------------
// Just checks if 'RequestStop()' has already been called in this object.
// ------------------------------------------------------------------------
private bool IsStopRequested()
{
lock (this)
{
private bool IsStopRequested() {
lock (this) {
return stopRequested;
}
}
Expand All @@ -232,13 +212,10 @@ private bool IsStopRequested()
// protection mechanism when the port is faster than the Unity progeram.
// If not, we may run out of memory if the queue really fills.
// ------------------------------------------------------------------------
private void RunOnce()
{
try
{
private void RunOnce() {
try {
// Send a message.
if (outputQueue.Count != 0)
{
if (outputQueue.Count != 0) {
SendToWire(outputQueue.Dequeue(), serialPort);
}

Expand All @@ -247,20 +224,16 @@ private void RunOnce()
// this line so it eventually reaches the Message Listener.
// Otherwise, discard the line.
object inputMessage = ReadFromWire(serialPort);
if (inputMessage != null)
{
if (inputQueue.Count < maxUnreadMessages)
{
if (inputMessage != null) {
if (inputQueue.Count < maxUnreadMessages) {
inputQueue.Enqueue(inputMessage);
}
else
{
else {
//Debug.LogWarning("Queue is full. Dropping message: " + inputMessage);
}
}
}
catch (TimeoutException)
{
catch (TimeoutException) {
// This is normal, not everytime we have a report from the serial device
}
}
Expand Down
Loading

0 comments on commit 1f3366e

Please sign in to comment.