As the previous article was "current-status". Progress brought new insights, and so the following corrections apply:

I develop in VS2015 under Windows, so I didn’t need to install Akka.Net on the RPi; installing fsharp for Debian/Ubuntu Linux is enough;

The Self-Hosted Websharper template does not work -fully- as was described, see below;

﻿Control Panel﻿

The goal is to write a webapplication, which provides a Control Panel for the Robot Arm. I want to run the Control Panel via my home-network on my iPad.

The webapplication should show all four Servo-motors, and two buttons per motor, with which I can turn the motor clockwise or anticlockwise. Ifthe buttons are untouched, the motors don't move. If a button is pressed, the motor should rotate at a constant speed, and not too fast.

The robot arm, together with the control panel. It works on my home-network, via Wifi! It almost crashed when my 2-year-old showed his interest..

Application Architecture

The visual part of the Control Panel is written using FSharp and Websharper. Websharper enables the visual interface and to send events (button pressed) to the Webserver on the Raspberry Pi.

At the server side a user event is sent to a "Directional Controller" (later more about this) which is created using Akka.Net. The directional controller receives the events and converts this into motion of the Servo-motors. There is one "Directional Controller" per motor.

We are going to use the ServoBlaster installed in part 1. According to this article, the following Linux shell command will rotate one of the Servo motors:

echo0=50%>/dev/servoblaster

We want to be able to write move commands to this device, for all motors concurrently. But to avoid a sharing violation, these commands need to be throttled: one per time.

Akka.Net provides a good way to throttle. We just create a "device-actor" which writes to the device. If the directional controller wants to invoke motion, it send a move command to the device-actor. The device-actor handles all messages in order, and one per time, so we'll have no Sharing Violation issues.

Another reason to use Akka.Net is to gain a bit experience; we are considering using Akka.Net at work, in C# though, but still.

Directional ControllerFor the directional controller, I modelled a strange kind of pressure based structure. For the metaphor, I assume that water delivers the pressure. I couldn't find a real-world product, so I made up a name. The schematics are below:

From the left, pressure (ie water) flows into the structure and this pressure spreads evenly over the upper and lower part. If you pull the upper valve, water will flow through the upper part, pushing the centre gear rotating clockwise, which then rotates the attached robot-arm limb. If you pull the lower valve, the centre gear will rotate anticlockwise, pushing the limb in the other direction. If you pull neither, or both, the centre gear does not move. Of course, these "valves" represent the two buttons for a Servo motor in the Control Panel.

I used this metaphor of the "directional controller" in my source code, which you can find here on GitHub.

Running the Control Panel

Get the code from GitHub, and compile it in Visual Studio 2015. In part 1 you made a shared drive on your Raspberry Pi, and you should copy the relevant files to this share, from your Windows machine. Copy everything except .fs source code files, "packages" file, "Visual Studio Project" file.

Lessons LearnedThislittle summer project had lessons to learn. Some in the code (I've added comments), and some in the bigger picture.

Screw aroundWhile screwing the parts into a Robot-Arm, I was just following orders, with no experience whatsoever. And some joints are just rough, which is why they resist rotating well. I was just screwing around, and felt sorry later. However, if I paid a bit more attention in the beginning, I would probably have made similar mistakes. Currently I am re-screwing (around) to ease up the joints. Not satisfied yet, so I carry-on screwing.

SolderingIt has been quite a while the last time I soldered something. A few pins needed to be soldered onto the card, which interfaces on the RPi, and to which you can connect the Servo's. I didn't do a good job the first time, and when I plugged in my RPi, something got smelly. Later on, I found my Micro SD card was melted and broken in two. Lessons learned? Don't under-estimate this, things can really break.

WebserverInitially, my plan was to run Websharper as WebApp framework, under a webserver from the F# community. Unfortunately my plan failed. I tried Suave.io with Websharper, but I could not find any way to connect these two. From Freya I understood that it still requires some work 5) for Freya to run Websharper. As time was running out, I decided to use Websharper's Warp. First I tried the self-hosted template, mentioned in part-1, but at closer look, only the server-side HTML worked, while the dynamic HTML created with Javascript did not work. Another possibility is that I did something wrong, but like I said - time was running out, I did not follow-up.I used the self-hosted client-server template from Websharper to create the project, but deleted the code in the Main.fs and replaced it with Warp code (example).

It worked fine, but sometimes I copied all the stuff from my Windows machine to the Raspberry Pi, ran it, and it showed no changes, even when I erased the whole WebApp. Caching anyone? No there wasn't.

As I moved stuff around to make my deploy procedure much easier, I found out that the folder "Scripts" was created in unexpected locations. Then it came to me. I used the relative folder".." as WebApp-root. In some cases Websharperuses this relative to the binary's location and in other cases relative to the execution folder (which caused "Scripts" in unexpected locations). When I cd'd to the bin folder and executed the binary from there, the issue was gone.

Mono CrashesSo I created everything. First proof of concept of all parts, ran it on my PC, then ran it on the Raspberry Pi 2B. And when I felt confident, I put it all together, in one FSharpArm solution, with Websharper, Akka and Websharper.UI.Next v3.4 (which is awesome). It worked on my Windows PC, but when I copied everything to the Raspberry Pi, I got this screen:

After a long investigation, I discovered that all my working Proof-of-Concepts were using F# v4.0, while my final project, FsharpArm, was using F# 3.1. When I changed this to F# 4.0, everything worked like a charm. Ehm, feature-request, how about showing an error-message for this?

Akka.Net

The first thing I received from Akka.Net was a "DeadLetterNotification", when the first message was send. It appeared that I used keyword "use" instead of "let" to create the actor system in a function. When the function exited, the actor-system was disposed. With "let" you don't have this problem. That "use" came from example code.

Did Akka.Net add any value? I think it did. It worked perfectly for throttling the writes to the device. You can press two buttons in the Control Panel at the same time, and I did almost nothing to solve this concurrency problem. The "pressure" in the controller is done by (see source code) a "pulsar", which sends a message to the controller at a fixed time-interval. Thanks to the scheduler (and a co-worker who tipped me this feature) I didn't need "Quartz for .Net" or write something myself. Using a structure which open/closes valves to block or let messages through, I have a system with which I can give the Servo-motors a constant slow speed. I experimented with higher speeds and found that the Robot-arm may lose its balance.

To Conclude

This was it for the FSharpArm summer-project. Summer is finished here in Northern Europe and I will fly for holidays to the City of the Sun in Brazil, in September. It was nice to see, how little is needed to have an interesting learning experience.

I know what's like in the summertime, a bit quiet, so I thought it would be fun to build a Robot Arm, and to control it from a Raspberry Pi using self written F# software. Project goals are: have fun, learn some, and if possible, do a "Sean's dad" trick: inspire a kid.

I would not start with inspiring the kid, especially if they're very young. In my case, my two-year old just lied down on the Robot arm parts, which is his way of saying, can I play Dance Central on the XBox please.

﻿Constructing the Robot Arm﻿

The MeArm robot arm is an "open source" Robot Arm. If you were able to buy it, it should come with a construction manual. If it did not, then try to find it on the internet. In my case, I received a printed copy of the PDF found here. Take your time to construct it, be thorough, patient and careful not to break the materials. And don't forget to have some fun.

This is a typical example of a staged photograph. That's not curiosity, he is thinking of a cunning plan how to get away ASAP. He won btw, those little people are a clever lot.

You can control it from the command line, using servoblaster (see below).

Preparing the Raspberry Pi 2B

I came quite novice to this ground, the following is to give a Raspberry Pi quickstart.

At Raspberry's website, you can download the image of Raspbian, then you should use your Windows PC to install the image on the micro SD card. Even though my first encounter with Linux was somewhere in 1998, I've never worked with Debian. But it works the same as my usual choice - Ubuntu, I like it.

After installation, put the micro SD card in the appropriate slot of your RPi, hook it op to your TV-set with an HDMI cable, and connect it to you home-network with a network cable and plugin the USB keyboard. You need internet access. The first steps will be via the TV-set, but we change ASAP to accessing our RPi via our Windows PC. So there can't be any Dance Central on Xbox yet.

Finally enter "ifconfig", and you should see the RPI's ip-address, in the "eth0" section, write it down.

Now you can disconnect your RPi from the TV-set, take it to another place in your home and connect it to the network. If you haven't downloaded Putty yet, do it now. Start it up and enter the hostname in the "hostname" field, port stays 22 and click "Open". The first time you do this, you need to accept the encryption key. If it does not work with the hostname, perhaps you can try again with the IP-address you wrote down before.

Login to your Rapsberry Pi, go to root mode with "sudo su".

What is nice to have, is that your RPi has an easy access shared drive using Samba. I've searched the internet over and over, but I can never ever find a quick way to configure Samba, to share me a folder to my Windows desktop, without entering credentials, fiddling with file-permissions or assigning incorrect file-owners. No need for a rigorous security policy when doing simple in-home stuff.

The solution is simple, follow these EasySamba instructions from my Github account.

Now, on your Windows PC, you can right-click on your Desktop and add a short-cut. Where it asks for the path, enter "\\<hostname>" or "\\<ip-address>", give the shortcut a name and it should work.

Finally, we need a device driver, enabling us to write to the pins of the Raspberry Pi, to signal the Servo Motors. For older models of Raspberry Pi you can use this software. However this does not work for the Raspberry Pi 2B. For the Raspberry Pi 2B, you need to download and install the driver's upgrade, which you'll find here. I understand they both have the same interface, so I assume this project should also work on older RPi's.

Some optional cool stuff. I installed "vssh" on my iPad, which is like Putty, and a great way to login into your RPi. You can use your iPad as a second screen to run "top". Or you can demonstrate the Fsharp Interactive to your little bright nephew and show what's programming about (Enter "fsharpi" in your Linux shell after the setup above).

Another cool thing is that the setup above is enough to run Websharper. In Visual Studio 2015 with Websharper 3.4 installed, I created a new project using the "Self-hosted Client-Server Application" template. This runs a console application.

Build it. Copy everything from the project except *.fs to the shared folder on your RPi. With Putty go to the root-folder and enter "bin/yourapplicationname.exe . http://`hostname`:8080 ", note the ticks around "hostname".

(part of the menu is missing due to the small browser window)

End of part 1. Not very much FSharp at this point. We'll get to that later.