Programming a loosely coupled system: Gadgeteer Robot-F6
After I made my first steps in programming a Gagdgeteer project. See also: Gadgeteer GHI Extender Module, combining functionality. I was not happy about always ending up with one Program.cs with all code in it. Therefore I decided to improve the code. Below is a picture of the system: the controls, sensors and brain of my robot:
  • Cerberus motherboard
  • Wifi module RN171
  • Extender module for:
    • Motor control
    • distance measurement
    • power supply when not programming
  • Humidity/temperature module
  • USBclientSP (only for programming)
Basic setup
Basic setup
Robot web control
Robot web control

The robot has a web interface and via ajax calls to the "web server", the robot can be controlled. I created a class called WebRequestHandler.cs which handles all web requests, but I shouldn't have any knowledge about the motor controls, humidity sensor, etc. This can be achieved by using the command-pattern. All the necessary commands must implement the ICommand-interface:

using System; using Microsoft.SPOT; namespace Robot_F6 {     public interface ICommand     {       void Execute();     } }

The WebRequestHandler handler has no knowledge about the different implementations. It just checks whether a command has been supplied when a specific WebRequest is handled. If a command is available, the WebRequestHandler calls the ICommand.Execute() method.

using System; using System.Collections; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Presentation; using Microsoft.SPOT.Presentation.Controls; using Microsoft.SPOT.Presentation.Media; using Microsoft.SPOT.Touch; using Gadgeteer.Networking; using GT = Gadgeteer; using GTM = Gadgeteer.Modules; using Gadgeteer.Interfaces; using Gadgeteer.Modules.GHIElectronics; using Gadgeteer; using Gadgeteer.Modules.Seeed; namespace Robot_F6 {     public class WebRequestHandler : IWebRequestHandler     {         public ICommand NorthCommand { get; set; }         public ICommand NorthEastCommand { get; set; }         public ICommand EastCommand { get; set; }         public ICommand SouthEastCommand { get; set; }         public ICommand SouthCommand { get; set; }         public ICommand SouthWestCommand { get; set; }         public ICommand WestCommand { get; set; }         public ICommand NorthWestCommand { get; set; }         public ICommand StopCommand { get; set; }         public ICommand StartCommand { get; set; }         public ICommand FireCommand { get; set; }         public ICommand FastCommand { get; set; }         public ICommand SlowCommand { get; set; }         public ICommand BreakCommand { get; set; }         public ITemperatureMeasurer TemperatureMeasurer { get; set; }         public IHumidityMeasurer HumidityMeasurer { get; set; }         public WebRequestHandler()         {         }         public void HandleRequest(GTM.GHIElectronics.HttpStream request)         {             string requestedURL = request.Request.URL;             if (requestedURL.IndexOf("/_") == 0)             {                 HandleAjaxHttpRequest(request, requestedURL);             }             else             {                 HandleHttpRequest(request, requestedURL);             }         }         private void HandleHttpRequest(GTM.GHIElectronics.HttpStream request, string requestedURL)         {             string response = "Unkown action";             if (requestedURL == "/index.html" || requestedURL == "/") { response = "Welcome"; }             else { response = "Unknown action"; }             response = "HTML HERE" + response + "HTML HERE"; // see below             SendResponse(request, response);         }         private void HandleAjaxHttpRequest(GTM.GHIElectronics.HttpStream request, string requestedURL)         {             bool responseWasHandled = false;             string response = "";             switch (requestedURL)             {                 case "/_index":                     response = "Welcome";                     break;                 case "/_northwest":                     response += "north west";                     if(NorthWestCommand != null){ NorthWestCommand.Execute();}                     break;                 case "/_north":                     response += "north";                     if(NorthCommand != null){ NorthCommand.Execute();}                     break;                 case "/_northeast":                     response += "north east";                     if(NorthEastCommand != null){ NorthEastCommand.Execute();}                     break;                 case "/_west":                     response += "west";                     if(WestCommand != null){ WestCommand.Execute();}                     break;                 case "/_east":                     response += "east";                     if (EastCommand != null) { EastCommand.Execute(); }                     break;                 case "/_southwest":                     if( SouthWestCommand!= null){ SouthWestCommand.Execute();}                     break;                 case "/_south":                     response = "We're going down south";                     if(SouthCommand != null){ SouthCommand.Execute();}                     break;                 case "/_southeast":                     if(SouthEastCommand != null){ SouthEastCommand.Execute();}                     break;                 case "/_start":                     response = "Let's go";                     if(StartCommand != null){ StartCommand.Execute();}                     break;                 case "/_fast":                     response = "Fast";                     if (FastCommand != null) { FastCommand.Execute(); }                     break;                 case "/_slow":                     response = "Slow";                     if (SlowCommand != null) { SlowCommand.Execute(); }                     break;                 case "/_stop":                     response = "Hold it, right there!";                     if(StopCommand != null){ StopCommand.Execute();}                     break;                 case "/_fire":                     if(TemperatureMeasurer != null)                     {                         response = "Temp: " + TemperatureMeasurer.GetTemperature().ToString("f2") + "C ";                     }                     if(HumidityMeasurer != null)                     {                         response += " Humidity: " + HumidityMeasurer.GetHumidity().ToString("f2") + "%";                     }                     if (FireCommand != null) { FireCommand.Execute(); }                     break;                 case "/_getdataX":                     if(TemperatureMeasurer != null)                     {                         response = "Temp: " + TemperatureMeasurer.GetTemperature().ToString("f2") + "C ";                     }                     if(HumidityMeasurer != null)                     {                         response += " Humidity: " + HumidityMeasurer.GetHumidity().ToString("f2") + "%";                     }                     break;                 default:                     response = "Unknown action";                     break;             }             if (!responseWasHandled)             {                 SendResponse(request, response);             }         }         private void SendResponse(GTM.GHIElectronics.HttpStream request, string response)         {             byte[] document = System.Text.Encoding.UTF8.GetBytes(response);             request.Response.HeaderData["Content-type"] = "text/html; charset=utf-8";             request.Response.HeaderData["Connection"] = "close";             request.Response.HeaderData["Cache-Control"] = "no-cache";             request.Response.HeaderData["Content-Length"] = document.Length.ToString();             request.Response.StatusCode = GTM.GHIElectronics.HttpResponse.ResponseStatus.OK;             request.Response.Send(document);         }     } }

The example response html did mess up the 'code view' a little. There fore I removed it and copied in my 'normal text'-view. Hope to fix that some other time.

response = "<!DOCTYPE html>\n<html lang=\"en\" xmlns=\"\">\n<head>\n <meta charset=\"utf-8\" />\n <title>Robot F6</title>\n</head>\n<body>\n <style>\n td {\n width:160px;\n vertical-align:central\n }\n input[type=\"button\"] {\n font-size:144px;\n background-color:beige;\n text-align:center;\n color:black;\n font-weight:bold;\n width:150px;\n height:150px;\n vertical-align:central\n\n }\n\n input[type=\"button\"].x {\n font-size:12px;\n }\n\n #btnStop {\n background-color:red;\n font-size:32px;\n }\n\n #btnStart {\n background-color:green;\n font-size:32px;\n }\n \n #btnFast {\n background-color:midnightblue;\n color:white;\n font-size:32px;\n }\n \n #btnSlow {\n background-color:yellow;\n font-size:32px;\n }\n #lbInfo {\n display: block;\n float: right;\n width:300px;\n background-color:white;\n font-size:24px;\n text-align:center\n }\n\n </style>\n <script type=\"text/javascript\">\n var xmlhttp;\n if (window.XMLHttpRequest) {\n xmlhttp = new XMLHttpRequest();\n }\n else {\n xmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\");\n }\n\n function submitAction(action) {\n\n xmlhttp.onreadystatechange = function () {\n if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {\n document.getElementById(\"lbInfo\").innerHTML = xmlhttp.responseText;\n }\n }\n\"GET\", \"\" + action, true);\n xmlhttp.send();\n }\n\n\n window.setInterval(submitAction('getdataX'), 1000); </script>\n <table>\n <tr>\n <td><input type=\"button\" class=\"x\" value=\"x\" id=\"btnNorthWest\" onclick=\"submitAction('northwest');\"/></td>\n <td><input type=\"button\" value=\"/\\\" id=\"btnUp\" onclick=\"submitAction('north');\"/></td>\n <td><input type=\"button\" class=\"x\" value=\"x\" id=\"btnNorthEast\" onclick=\"submitAction('northeast');\"/></td>\n <td><input type=\"button\" value=\"START\" id=\"btnStart\" onclick=\"submitAction('start');\"/></td>\n <td><input type=\"button\" value=\"STOP\" id=\"btnStop\" onclick=\"submitAction('stop');\"/></td>\n </tr>\n <tr>\n <td><input type=\"button\" value=\"<\" id=\"btnWest\" onclick=\"submitAction('west');\"/></td>\n <td><input type=\"button\" class=\"x\" value=\"x\" id=\"btnCenter\" onclick=\"submitAction('fire');\"/></td>\n <td><input type=\"button\" value=\">\" id=\"btnEast\" onclick=\"submitAction('east');\"/></td>\n <td colspan=2><label id=\"lbInfo\">Welcome to Robot-F6. Please tell me what to do.</label></td>\n\n </tr>\n <tr>\n <td><input type=\"button\" class=\"x\" value=\"x\" id=\"btnSouthWest\" onclick=\"submitAction('southwest');\"/></td>\n <td><input type=\"button\" value=\"\\/\" id=\"btnSouth\" onclick=\"submitAction('south');\"/></td>\n <td><input type=\"button\" class=\"x\" value=\"x\" id=\"btnSouthEast\" onclick=\"submitAction('southeast');\"/></td>\n <td><input type=\"button\" value=\"SLOW\" id=\"btnFast\" onclick=\"submitAction('slow');\"/></td>\n <td><input type=\"button\" value=\"FAST\" id=\"btnFast\" onclick=\"submitAction('fast');\"/></td>\n </tr>\n </table>\n</body>\n</html>";

As you can see, the WebRequestHandler has no knowledge of the command implementations. Besides the ICommand interface there are two other interfaces that are used by the WebRequestHandler: ITemperatureMeasurer and IHumidityMeasurer

using System; using Microsoft.SPOT; namespace Robot_F6 { &nbsp;&nbsp;&nbsp;&nbsp;public interface ITemperatureMeasurer &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double GetTemperature(); &nbsp;&nbsp;&nbsp;&nbsp;} }

using System; using Microsoft.SPOT; namespace Robot_F6 { &nbsp;&nbsp;&nbsp;&nbsp;public interface IHumidityMeasurer &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double GetHumidity(); &nbsp;&nbsp;&nbsp;&nbsp;} }

Both interfaces are implemented by the TemperatureHumidityModuleWrapper class which wraps the Gadgeteer.Modules.Seeed.TemperatureHumidity module. So if you want to use different temperature and/or humidity modules, you don't have to change the WebRequestHandler. Just create another implementation of the IHumidityMeasurer and/or ITemperatureMeasurer and tie them together (loosely).

using System; using Microsoft.SPOT; using Gadgeteer.Modules.Seeed; namespace Robot_F6 { &nbsp;&nbsp;&nbsp;&nbsp;public class TemperatureHumidityModuleWrapper : IHumidityMeasurer, ITemperatureMeasurer &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private double _humidity = 0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private double _temperature = 0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private TemperatureHumidity _temperatureHumidityModule; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public TemperatureHumidityModuleWrapper(TemperatureHumidity temperatureHumidityModule) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_temperatureHumidityModule = temperatureHumidityModule; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public double GetHumidity() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return _humidity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void SetHumidity(double humidity) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_humidity = humidity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public double GetTemperature() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return _temperature; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void SetTemperature(double temperature) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_temperature = temperature; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }

For the ICommand implementation I created different classes: one for each command. I could have combined some, but there's not much functionality in the implementations. For example the MoveLeftBackwardsCommand looks like this:

using System; using Microsoft.SPOT; namespace Robot_F6 { &nbsp;&nbsp;&nbsp;&nbsp;public class MoveLeftBackwardsCommand : ICommand &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private IDualMotorDriver _dualMotorDriver; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public MoveLeftBackwardsCommand(IDualMotorDriver dualMotorDriver) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_dualMotorDriver = dualMotorDriver; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void Execute() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_dualMotorDriver.MoveLeftBackwards(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} }

As you can see, there's not much functionality in it. All movement commands that I created use an IDualMotorDriver interface. This way the ICommand implementations are loosely coupled to the DualMotorDriver implementation.

using System; using Microsoft.SPOT; namespace Robot_F6 { &nbsp; public interface IDualMotorDriver &nbsp; { &nbsp; /// <summary> &nbsp; /// Set M1 and M2 to move left forwards &nbsp; /// </summary> &nbsp; void MoveForwards(); &nbsp; &nbsp; /// <summary> &nbsp; /// Set M1 and M2 to move right backwards &nbsp; /// </summary> &nbsp; void MoveBackwards(); &nbsp; /// <summary> &nbsp; /// Set M1 and M2 to turn clockwise: M1 backwards, M2 forwards &nbsp; /// </summary> &nbsp; void TurnClockwise(); &nbsp; &nbsp; /// <summary> &nbsp; /// Set M1 and M2 to turn counter clockwise: M1 forwards, M2 backwards &nbsp; /// </summary> &nbsp; void TurnCounterClockwise(); &nbsp; &nbsp; /// <summary> &nbsp; /// Set M1 and M2 to move left forwards: M1 moves slower than M2 &nbsp; /// </summary> &nbsp; void MoveLeftForwards(); &nbsp; /// <summary> &nbsp; /// Set M1 and M2 to move left forwards: M1 moves faster than M2 &nbsp; /// </summary> &nbsp; void MoveRightForwards(); &nbsp; /// <summary> &nbsp; /// Set M1 and M2 to move left backwards: M1 moves slower than M2 &nbsp; /// </summary> &nbsp; void MoveLeftBackwards(); &nbsp; /// <summary> &nbsp; /// Set M1 and M2 to move left backwards: M1 moves faster than M2 &nbsp; /// </summary> &nbsp; void MoveRightBackwards(); &nbsp; /// <summary> &nbsp; /// Set M1 and M2 to stop &nbsp; /// </summary> &nbsp; void Stop(); &nbsp; /// <summary> &nbsp; /// Set M1 and M2 to break &nbsp; /// </summary> &nbsp; void Break(); &nbsp; /// <summary> &nbsp; /// Move M1 and M2 one step &nbsp; /// </summary> &nbsp; void Step(); &nbsp; /// <summary> &nbsp; /// Change to 'fast motion' &nbsp; /// </summary> &nbsp; void Fast(); &nbsp; /// <summary> &nbsp; /// Change to 'slow motion' &nbsp; /// </summary> &nbsp; void Slow(); &nbsp; void NormalSpeed(); &nbsp; } }

So how do we tie things together? Partially by creating a factory that instantiates the implementations of specific interfaces.

using System; using Microsoft.SPOT; using Gadgeteer.Modules.GHIElectronics; using GT = Gadgeteer; using GTM = Gadgeteer.Modules; using Gadgeteer.Interfaces; using Gadgeteer.Modules.Seeed; namespace Robot_F6 { public class F6Factory { public static IMotor CreateMotor(GT.Interfaces.DigitalOutput contact1, GT.Interfaces.DigitalOutput contact2) { return new Motor(contact1, contact2); } public static IDualMotorDriver CreateDualMotorDriver(int socketNumber, DigitalOutput m1a, DigitalOutput m1b, DigitalOutput m2a, DigitalOutput m2b) { return new L298_H_BridgeDriver(socketNumber, m1a, m1b, m2a, m2b); } public static IWebRequestHandler CreateWebRequestHandler() { return new WebRequestHandler(); } } }

All other parts are tied together in Program.cs. First the extender module is configured, then the temperaturehumiditywrapper and the webRequestHandler. To prevent the ProgramStarted method from locking, a run once timer is intialized and started.

void ProgramStarted() &nbsp; { &nbsp; wifi_RN171.Reboot(); &nbsp; int socketNumber = 4; &nbsp; // Distance_US3 uses pin3 and pin4. Other IO pins are unused. &nbsp; _distanceMeterFront = new Distance_US3(socketNumber); &nbsp; &nbsp; DigitalOutput m1a = extender.SetupDigitalOutput(Socket.Pin.Nine, false); &nbsp; DigitalOutput m1b = extender.SetupDigitalOutput(Socket.Pin.Eight, false); &nbsp; DigitalOutput m2a = extender.SetupDigitalOutput(Socket.Pin.Seven, false); &nbsp; DigitalOutput m2b = extender.SetupDigitalOutput(Socket.Pin.Six, false); &nbsp; _iDualMotorDriver = F6Factory.CreateDualMotorDriver(socketNumber, m1a, m1b, m2a, m2b); &nbsp; _temperatureHumidityModuleWrapper = new TemperatureHumidityModuleWrapper(temperatureHumidity); &nbsp; &nbsp; _webRequestHandler = F6Factory.CreateWebRequestHandler(); &nbsp; GT.Timer timer = new GT.Timer(1000, GT.Timer.BehaviorType.RunOnce); &nbsp; timer.Tick += timer_FirstTick; &nbsp; timer.Start(); &nbsp; Debug.Print("Robot F6 started..."); &nbsp; }

In the timer_FirstTick, all components are configured: WebRequestHandler, Wifi, Sensors and a Helloworld is given to the user: Robot spins short Clockwise and Counter Clockwise. After that an interval timer is started. Each timer tick the Robot can 'think and act' about what to do in the current state.

private void timer_FirstTick(GT.Timer timer) &nbsp; { &nbsp; timer.Stop(); &nbsp; timer.Tick -= timer_FirstTick; &nbsp; SetupWebRequestHandler(); &nbsp; SetupWifi(); &nbsp; SetupTemperatureAndHumidityMeasurements(); &nbsp; HelloWorld(); &nbsp; StartIntervalTimer(); &nbsp; } &nbsp; private void StartIntervalTimer() &nbsp; { &nbsp; GT.Timer intervalTimer = new GT.Timer(_intervalmsec, GT.Timer.BehaviorType.RunContinuously); &nbsp; intervalTimer.Tick += intervalTimer_Tick; &nbsp; intervalTimer.Start(); &nbsp; } &nbsp; private void HelloWorld() &nbsp; { &nbsp; _iDualMotorDriver.TurnClockwise(); &nbsp; Thread.Sleep(1000); &nbsp; _iDualMotorDriver.Stop(); &nbsp; Thread.Sleep(1000); &nbsp; _iDualMotorDriver.TurnCounterClockwise(); &nbsp; Thread.Sleep(1000); &nbsp; _iDualMotorDriver.Stop(); &nbsp; }

In the SetupWebRequestHandler all the commands and the humidity and temperature measurers are created and attached to the WebRequestHandler.

private void SetupWebRequestHandler() &nbsp;{ &nbsp; _webRequestHandler.NorthCommand = new MoveForwardsCommand(_iDualMotorDriver); &nbsp; _webRequestHandler.NorthEastCommand = new MoveRightForwardsCommand(_iDualMotorDriver); &nbsp; _webRequestHandler.EastCommand = new TurnClockwiseCommand(_iDualMotorDriver); &nbsp; _webRequestHandler.SouthEastCommand = new MoveRightBackwardsCommand(_iDualMotorDriver); &nbsp; _webRequestHandler.SouthCommand = new MoveBackwardsCommand(_iDualMotorDriver); &nbsp; _webRequestHandler.SouthWestCommand = new MoveLeftBackwardsCommand(_iDualMotorDriver); &nbsp; _webRequestHandler.WestCommand = new TurnCounterClockwiseCommand(_iDualMotorDriver); &nbsp; _webRequestHandler.NorthWestCommand = new MoveLeftForwardsCommand(_iDualMotorDriver); &nbsp; _webRequestHandler.FireCommand = new DelegatedCommand(new DelegatedCommand.DoSomething(this.ToggleAutoPilot)); <br/> &nbsp; _webRequestHandler.SlowCommand = new SlowCommand(_iDualMotorDriver); &nbsp; _webRequestHandler.FastCommand = new FastCommand(_iDualMotorDriver); &nbsp; _webRequestHandler.StopCommand = new StopCommand(_iDualMotorDriver); &nbsp; _webRequestHandler.BreakCommand = new BreakCommand(_iDualMotorDriver); &nbsp; _webRequestHandler.StartCommand = new StartCommand(_iDualMotorDriver); &nbsp; _webRequestHandler.TemperatureMeasurer = _temperatureHumidityModuleWrapper; &nbsp; _webRequestHandler.HumidityMeasurer = _temperatureHumidityModuleWrapper; &nbsp;}

The SetupWifi enables the HttpServer and subscribes to the HttpRequestReceived event.

private void SetupWifi() &nbsp;{ &nbsp; wifi_RN171.SetDebugLevel(GTM.GHIElectronics.WiFi_RN171.DebugLevel.DebugAll); &nbsp; wifi_RN171.Initialize(GTM.GHIElectronics.WiFi_RN171.SocketProtocol.TCP_Server); &nbsp; wifi_RN171.EnableHttpServer(); //Enable HTTP Parsing &nbsp; wifi_RN171.HttpRequestReceived += new GTM.GHIElectronics.WiFi_RN171.HttpRequestReceivedHandler(wifi_RN171_HttpRequestReceived); <br/> &nbsp;} public void wifi_RN171_HttpRequestReceived(GTM.GHIElectronics.HttpStream request) &nbsp;{ &nbsp; _webRequestHandler.HandleRequest(request); &nbsp;}

The SetupTemperatureAndHumidityMeasurements starts continuous measurements and subscribes to MeasurementComplete events of the 'TemperatureHumidity' module.

private void SetupTemperatureAndHumidityMeasurements() &nbsp;{ &nbsp; temperatureHumidity.MeasurementComplete += temperatureHumidity_MeasurementComplete; &nbsp; temperatureHumidity.StartContinuousMeasurements(); &nbsp;}

When the Robot is in auto pilot mode, the intervaltimertick will change the robot's direction based on the measured distance. When in manual mode, the Step method of the dualmotordriver is called.

private void intervalTimer_Tick(GT.Timer intervalTimer) &nbsp;{ &nbsp; intervalTimer.Stop(); &nbsp; int distance = _distanceMeterFront.GetDistanceInCentimeters(); &nbsp; if (autoPilot) &nbsp; { &nbsp; _counter++; &nbsp; if (_counter * _intervalmsec < 5000) &nbsp; { &nbsp; if (distance < 0 || distance > mininumDistance) &nbsp; { &nbsp; _iDualMotorDriver.MoveForwards(); &nbsp; } &nbsp; else &nbsp; { &nbsp; _iDualMotorDriver.TurnClockwise(); &nbsp; } &nbsp; } &nbsp; else if (_counter * _intervalmsec < 7500) &nbsp; { &nbsp; _iDualMotorDriver.Stop(); &nbsp; } &nbsp; else &nbsp; { &nbsp; _counter = 0; &nbsp; } &nbsp; } &nbsp; //else if (distance > 0 && distance < mininumDistance) &nbsp; //{ &nbsp; // autoPilot = true; &nbsp; //} &nbsp; else &nbsp; { &nbsp; _iDualMotorDriver.Step(); &nbsp; } &nbsp; intervalTimer.Start(); &nbsp;}

One could argue to create all commands in the factory as well, but then I would suggest to move all logic from Program.cs as well. The source code can be downloaded here.

Back to List

All form fields are required.
A confirmation mail for the comments will be send to you.