Saturday, 18 March 2017

Cutting code . . .

Cutting code

Probably the part I like least about this project, cutting code.  I started off my career in IT writing bits of C for spatial data manipulation and server software on Unix, but never got into UI and Windows programming, in fact I pretty much hate it.  Which is why I'm a database admin/programmer now I guess any I digress.

Finding example code to do what I wanted was surprisingly hard.  I did find this little gem but it was in Visual Basic from Hackshed.  However it did have Bluetooth code that works straight out of the box ( well after getting the baud rate right ).

With a few little tweaks I managed to get it up and running but as I have no clue how to write VB I got a little stuck on threading and adding bits and pieces to the interface.  Luckily I have proper Windows programmer friends who could had it for me.

So the interface from Hackshed was modified to do the following.

* Drive both motors from -255 to +255. 0 being off.
* Read the RFID reader output.

So currently I'm waiting for my friend to send me back a new version that he converted to C# and added some better handling between the Arduino and the PC.

So what is my aim here.  Well I want the Arduino to just be a relay, I don't want it to have any smarts at all.  The C# interface will basically set and keep track of speed and also the RFID tags it passes over.

Hopeful Operational Theory

 RFID tags being uniquely numbered means that I can use them for location and also function.

The interface will store each tag in a database.  New tags will request the user for details.

Each tag will be used to add location and build a track map using linked lists.

Function Tags will initially be for the following.

1) Reverse for end of track
2) Stop signals ( start and stop ).
3) Speed signs ( for slowing and speeding around turns etc ).
4) Time stop delay ( for stations stops ).

I don't have anything for track switches.  There is commercial product for this so I might use that as it looks nice,

New nodes should be added as they are encountered and placed in the correct location in the list.

Location Tags will identify trains on a portion of track.   This should enable the following.

1) Location
2) Direction ( after passing two tags )
3) Collusion detection and management ( either by right of way or using Stop Signals ) for multiple trains.

Don't hold your breath

Well it's taken me about two years to actually get it to this point, because there is just waayyy too much other cool stuff to do and I want to do it all.

Besides the RFID is a bit flakey so we'll see how it works in practice once I get the new code from my mate ( and I put in all my peripherals instead of his ).

Fun with RFID

Fun with the ID-12LA

So looking at the sensor tech out there that people have implemented, I'm really not a big fan of all the wires that people are using to detect where the train is.  RFID probably isn't the best solution but I think it'll suit my needs a little better . . . if I can get it working.

So the module I got was the small ID-12LA from Sparkfun.  I Initially bought the starter kit with the USB serial reader and a few cards, but save yourself some cash and just get the reader.

You should also get the RFID Reader Breakout Board and also some standard pin headers ( 0.1" ) and some xbee pin ( 2mm ) headers as well.

Ok next step . . . ignore the pinout example on Sparkfun's website . . . it is incomplete, to the point of being frustratingly useless.

 I eventually found this on Instructables and even added my own two cents worth in a comment for clarity and emphasis on following Part 2 of the how to properly.

I was getting the FFFFFFF issue and other intermittent read issues.
The only thing that fixed it was to follow step 2 correctly with the wiring.
Ensure that you are using the +5V and NOT +3.3V
Tie pin 1(GND) and 7(FORMAT) together and connect to Ground
Tie pin 2(RES) and 11(+5V) together and connect to +5V
Pin 9 (D0) goes to RX on the Arduino
Pin 10 (LED) should go to an appropriate resistor, LED then Ground.

However a picture is worth a thousand words so here is my break out board with wiring.  The beauty of this wiring is that you only need to use one connector and three wires.



The wire I got was single core with insulation, so it was easy to solder and didn't cause a short.

I soldered all of the headers on first.

Once complete I soldered ( re-flowed ) the BLACK wire from GND to FORM.

There is a spare BLACK wire that goes from GND to nowhere, you don't need that one, it's just in case I wanted to solder a led and resistor to the READ pin for read notifications, but I don't want to now as it was only for debugging.  Also when the card/button is read there is a blue led on the MM v2 that will light up.

I then soldered ( re-flowed ) the RED wire from RES to VCC.

I crimped up the 6 Pin connector with three wires for VCC ( +3.3V ) , D0 ( Digital Sense Pin ) and FORM ( Ground ).


Wiring up the MM v2 to PF

Wiring up the Micro Magician v2 to Power functions

So with a little help from my multimeter and this website http://www.philohome.com/pf/pf.htm
I managed to wire up the PF battery box and motor successfully.

From Philos Website this diagram showed everything required to hook it all up.


Resulting in this


The battery is connected using the outside wires.  The motor using the inside wires.

It should be noted that the C1 and C2 wires on the battery box are wired in and produce 9V, the orange switch switching positive and negative, do don't use them.  The outside wires remain the same polarity regardless of the setting of the switch.

Now to put it in the train!!

Arduino and LEGO Power Function Trains

Arduino and LEGO Power Function Trains

One of my many passions is LEGO, it is awesome.  In recent years after being totally demoralized by the unending release and re-release of the Star Wars product line, I was extremely excited to see the new Power Functions Lego Train Line.  Like probably a lot of Adults I was a little disappointed in the use of Infrared controllers and the limitations that they brought.

So like many LEGO train enthusiasts out there I started looking at Arduino and trying to integrate the Power Functions nicely into that.  After buying a few Ardunio boards and playing with them with breadboards they all had pretty bad problems with them that didn't make them ideal to run my trains.

Basically the issues I had were the following:

Too Big - Too many Shields
The trouble with nearly all boards and systems out there is that in order to get the functionality you need you need to either have a really massive board or lot of Sheilds.  So Shields are those add on boards that add functionality like battery power, motor drivers, RFID readers and wireless connections.  Even the tiny Ardunio clones needed a LOT of boards to get the functionality I needed and even then the Power issue persisted.

Power
This was probably the biggest bug bear and road block.  Pretty much every single system required that you have two power supplies one for the Arduino and one for the Motors.  I looked at building my own boards with Linear Power converters and H-Bridge chips and in the end I ended up with a massive Breadboard of despair.

The Savior, the DAGU Micro Magician v2

So I almost gave up on the whole thing until I came across the DAGU Micro Magician v2.  This wondrous little board packs in a massive amount of functionality into a tiny board.  So what makes this board so good?

Power : One battery in 3 supplies out!
This little board only requires ONE power supply up to 9 volts.  This is awesome as the trains run with 9 volt battery packs this is perfect.  Also perfect is the board provides three different power sources on the board, 3.3 volts, 5 volts and the Battery Voltage from just the one external power source.

Built in H Bridge Motor driver
There are two motor drivers built into the board.  So you can drive the train motor and add some other movement functionality ( like doors ) from the one board.

A myriad of Analog and Digital pins
There are plenty of programmable pins to add all sorts of additional functionality.

Bluetooth functionality via the tiny add on HC-06 board
Using the specially placed serial port on the board you can lay flat the HC-06 blue tooth board right on top of the DAGU board.

On top of that there is also a builtin accelerometer and also an Infrared receiver.

Once it's all together, it's tiny and very functional!


So you can see the Micro Magician v2 compared to the Lego Battery Box.  The on screen configuration is :
* 1 x motor ( out of two, although I do have both working in the VB and the sketch code ).
* 1 x HC-06 Bluetooth adapter ( the green board in the middle ).
* 1 x battery pack ( I used while building and testing ) 4 x 1.5 volt.
* 1 x RFID reader ( It's an ID-12LA from Sparkfun, but that's for another time ).

I have all of the LEGO PF cables cut up and crimped, I just need to test them first to make sure that I'm not going to blow anything up ( maybe add a diode to the power cable ).

Where did it get it?  Well being Australian I got it from Robot Gear - DAGU MMv2.  You know, buy local and all that jazz!


Perils of Wizdom . . .

Welcome to the Perils of Wizdom . . .

Hopefully my many never to be finished projects will appear here in all states of disrepair.

Enjoy my total inability to stick to just one hobby.