Rocrail Arduino CAN-bus interface, MS2, Gleisbox GFP and S88

Moderator: Moderators

Rocrail Arduino CAN-bus interface, MS2, Gleisbox GFP and S88

Postby woodyboy » 01.01.2014, 18:17

The goal of this project is to control a layout with a Gleis Format Prozessor. Mostly it is delivered with a Mobile Station 2 in a startset. It uses a CAN bus to communicate between the MS2 and the GFP. The GFP has a second CAN plug to connect an additional MS2 but instead of a second MS2 a CAN shield could connected. The shield is attached on an Arduino in conjunction with an Ethernet shield.

The sensors are attached to a second Arduino which controls the S88 bus operation. This Arduino runs a separate sketch which sends all sensor events to Rocrail. An event is generated for every sensor event. The event is send in a CAN style message by the ethernet controller.

All communication between the Arduino's and Rocrail is done by Ethernet. Rocrail should be configured with the MCS2 library as Central Station. It is possible to debug via the Arduino USB/serial port.

===============================================
WARNING WARNING WARNING:
The GFP uses a H bridge to bring the power and information to the track layout. This means there is no real Gnd on the track. If you connect the S88 Arduino directly to the layout then you have a shortcircuit on your layout. There are several solutions to bypass this challenge.
===============================================

The solution can be used with a only a Gleisbox, for me the output of the GFP is insufficient to drive my tracklayout. In the time before I used Rocrail I operated the track with an Edits booster. This booster uses symmetrical power. For this reason the GFP output is taken from the IC 4555D pin 12 inside the GFP. Ground is shared between GFP, Edits booster and both Arduino's. The H bridge output is not used by me. Using the common ground TTL level output from the Gleisbox, this saved me from the short-circuit problem. It generates a new challenge to transform the TTL level to a symmetrical signal to drive the Edits booster. This solved by a RS232 driver. The needed symmetrical power supply for this chip is taken from the boosterprint. Maybe I'll modify it and move the drive circuit to the shield using a IC which mirrors the 5V to -5V.

Power enabling/disabling is taken from pin1/15 from IC 4555D inside the GFP. I used a CMOS 4093 Schmitt trigger to get a better signal and to decouple the ports on the 4555D. For a schema of the GFP look here: http://www.stummiforum.de/viewtopic.php?f=5&t=90373

The sketches containing a lot of info about the configuration and are not cleaned up. I am not a professional programmer, so improvements are welcome.

Attached a picture that shows the rerouting of the CAN controller CS signal. To my knowledge all assembled CAN shields use portnumber 10 which is also used by the Ethernetshield.

Things2do: Implementing the current sensing / MFX recognition part from the Gleisbox to the booster. I own only a single MFX so this is a low priority issue to me.

Thanks to Fabian Greif (CAN library) and Juergen Pleumann (Railuino) for the work they have done.

Instructions based on a Arduino http://arduino.cc/en/Main/Software installation. Copy the Arduino gateway library by moving the files in the "documents\Arduino\libraries\" folder.
Unzip the sketches in the "documents\Arduino\" folder.
(Re)start the Arduino environment.
Configure the board and the serial port in the Tools menu of the Arduino development environment and upload to the Arduino board.

For those who plan to use the USB sketches, I recommend the Arduino Leonardo in favour of the UNO R3. This due to the fact the Leonardo doesn't reset at opening the Serial port by Rocrail. This has nothing to do with Rocrail, its the behaviour of the Arduino bootloader.
If you want to use an UNO R3, maybe this can be a workaround http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection#.UxpESFRdWKk

It is important that the Arduino not resets at opening the Serial port, because Rocrail sends a few commands to initialize f.i the switch time from the CS2 controller dialog. In combination with an ethernet shield the Arduino doesn't reset.

Note the latest sketches uses a different pin for the 2515 CS line see the pictures in the posts from the beginning of March. The CAN gateway library is also modified. See the more recent postings in this thread.
You do not have the required permissions to view the files attached to this post.
Last edited by woodyboy on 11.09.2015, 10:06, edited 21 times in total.
Regards,

Bert

Equipment: Roco WLANMaus, MS2, Gleisbox 2x(separated switch & rollingstock). Ubuntu 16. Edits booster. Arduino: S88 CANbus interface & Ethernet-CANbus gateway
woodyboy
 

Postby woodyboy » 03.01.2014, 09:28

:shock: I noticed the sketch for reading the sensors is missing.

The sketch reads up to 512 sensors on a S88 bus. The sketch is autodetecting the number of attached sensorgroups of each 8 sensors in case the S88 IN line is connected to Vcc. This the case for Edits sensors and some other OEM variants. The original S88 and most of the OEM has S88 IN connected to ground. In that case the autodetection fails. To resolve this, define the number of attached eight bit pairs in the sketch. Read the posts in this thread from begin of May 2014, page 4/5.

On every sensor event the Arduino sends an Ethernet CAN style message to Rocrail as the CS2 with V3.x.x. firmware does. So no polling configuration in Rocrail. After starting it sends automatic the sensors with status '1' to get a proper actual view of the trackstatus in Rocview

The shortcircuit sensor is direct attached to the Arduino board and creates a normal sensor CAN message event on a programmable sensor number. The var name for this sensor is "shortDetect" Rocrail should be configured to handle this event properly.

The sketch is not cleaned so I hope there is enough comment to understand the code. If you have improvements please upload the sketch back. This sketch needs an Ethernet W5100 shield and optional a LCD 20x4 display.


Attachment removed, see for recent files a more recent post in this thread.
Last edited by woodyboy on 07.05.2014, 15:21, edited 6 times in total.
Regards,

Bert

Equipment: Roco WLANMaus, MS2, Gleisbox 2x(separated switch & rollingstock). Ubuntu 16. Edits booster. Arduino: S88 CANbus interface & Ethernet-CANbus gateway
woodyboy
 

Postby John Hoek » 09.01.2014, 21:49

Perfect programming Bert! I like the combination with Rocrail and arduino to control the MS2 'gleisbox' from Marklin, with the bidirectional CAN protocol. :D

Why did you go for the ethernet shield too. :?:
Every Arduino has USB on board. Why not using USB instead? This saves some money. :wink:
John Hoek
 

Postby woodyboy » 10.01.2014, 17:52

@John,

The Ethernetshield gives me the possibility to do better debugging and the databuffer is better guaranteed.

But on your request here i digged my last version with USB for you. It is from early december and not maintained nor do I give any guarantees on this version (but all my sketches are without any warranties :P ). The library is included in the zip.

Remember no direct attachment of the Arduino and the sensors to the tracklayout. The electric path from the tracksensor to the Arduino should be isolated due to the H bridge in the Gleisbox.
You do not have the required permissions to view the files attached to this post.
Regards,

Bert

Equipment: Roco WLANMaus, MS2, Gleisbox 2x(separated switch & rollingstock). Ubuntu 16. Edits booster. Arduino: S88 CANbus interface & Ethernet-CANbus gateway
woodyboy
 

Re: Arduino interfaces Rocrail and GFP (Gleisbox)

Postby John Hoek » 02.03.2014, 09:14

Hi Bert,

I've tried to use the USB versions of your sketches with Rocrail.
However i face ttwo problems:

USB version of the gateway:
With your sketch RRRailuino_V1 I face the problem that the gleisbox is everytime locked up (power of the track/red stop lights on) after to quick commands send at once.
For instance F0 lights on. This is combined by rocrail with speedstep 0.
The gleisbox is registering the events well. I see them act on the display of MS2.
To inform; There is definately no shortcut!
I tried to chnage the settings of the Central ; switching time to diffent valuas from 50 to 1000 -> doenst't help
Also tried different commands pauzes; also no help. It makes no diffenrence when i use the standard 10ms, 20, 40, or even 1000ms between every command. Still eveytime power switched off
-> What am i doing wrong here?!


USB S88 sensors sketch:
Thu USB sensors work well. All events are registered.
However, with the change in Rocrail to check polling on the unofficial way , Rocrail server keeps in starting mode. Of course, polling is not supported in this sketch... I dunno why this change was made. Marklin has official support for S88 sensors on event broadcasting and polling is much less sofisticated. Anyway;
-> I can solve this by using an older version of Rocrail .

---
Bert, Could you manage to update the original USB sketches with the latest changes you made on the Ethernet versions.
Witch Rocrail it is no need to use the Ethernetversion, most computers and even raspberry has two USB ports, so with only two Arduino Uno and a CANshield you are able to control a small/midsise railroad with Rocrail.
John Hoek
 

Re: Arduino interfaces Rocrail and GFP (Gleisbox)

Postby woodyboy » 02.03.2014, 23:32

Hi John,

In my memories the poll and event sketch should work with the latest Rocrail versions. In the configuration of Rocrail the version should be configured to 2 and Sensors to 0. Program FB to 0. With this configuration it should automatic switch to event notification. I have never used polling with Rocrail. It is implemented half and I continued developing and finetuned the event method.

Today I finalized a new sketch with queueing for accessory device. The GFP is not locked up anymore. I can drive 7 trains simultaneous and the complete layout with switches and semaphores. It has run several hours without problems. I expect to upload this week the sketch for the GFP & USB as you requested. The sketch for the sensors do i have to check how much effort it costs to write it back to USB. The GFP sketch for USB is nearly finished. First tests have passed. Could you tell me which model of Arduino you use? For me it is only possible to test on UNOR3, Leonardo and MegaR3.
Last edited by woodyboy on 03.03.2014, 10:44, edited 1 time in total.
Regards,

Bert

Equipment: Roco WLANMaus, MS2, Gleisbox 2x(separated switch & rollingstock). Ubuntu 16. Edits booster. Arduino: S88 CANbus interface & Ethernet-CANbus gateway
woodyboy
 

Re: Arduino interfaces Rocrail and GFP (Gleisbox)

Postby woodyboy » 02.03.2014, 23:45

CAN_UDP_gwV9Q_GFP.zip
I finished a more stable sketch which supports queueing for K83 decoders. There is a major configuration change, the CS pin moved from PIN 3 (see photo above) to PIN 6. This is done to get compatibility for future I2C LCD support on the UNO, Leonardo en Mega boards. The sketch needs an Arduino :P , a CAN bus shield and an Ethernetshield. Dynamic debugging is possible by the USB/serial connection of the Arduino.

The libraries are adapted and included in the zip. The QueueArray library dynamic sizing is disabled due to minimal dynamic memory on the UNO and Leonardo. Comments are included in the files.

If you use the original M*klin power supply in conjunction with the GFP then the variable "accPauseTime" must be configured to minimal 150 to prevent the GFP switch off due to overload, or most of time the switchtongue hasn't completely moved.

Any comments and improvements are appreciated.
You do not have the required permissions to view the files attached to this post.
Regards,

Bert

Equipment: Roco WLANMaus, MS2, Gleisbox 2x(separated switch & rollingstock). Ubuntu 16. Edits booster. Arduino: S88 CANbus interface & Ethernet-CANbus gateway
woodyboy
 

Re: Arduino interfaces Rocrail and GFP (Gleisbox)

Postby John Hoek » 03.03.2014, 20:13

Hi Bert,

I use the Arduino UNO v3 boards, with one combined with the CAN module of Watterott. Like you do. :D
I ordered a W5100 ethernet module for the arduino. Expect to arrive this week. Then i can at least use your new sketch wich solves the biggest problem ( i Hope) ; the locking of the gleisbox. :?
By the way, I don't want/need a LCD. just use the computer and MS2. :coding:

Still it would be nice to have a standalone USB version too of the gleisbox interface, without using the ethernet module on it.
The reason behind this is, that i want to use my RaspberryPi board with rocrail on it to drive the arduino boards by USB. Raspberry has two usb ports; so perfect for one arduino with CAN to control the gleisbox (trains and accessories) and the other to get the S88 events back. :beer:
So a USB standaloneversion should be nice to have. Every current Railuino user could change to Rocrail then instead of desktop Station. I tried that too, but that program is still far, far away from real automatic control. :oops:

----
The problem what i face with the ''old'' USB sketch for S88 sensors is that indeed automaticly sended events just works fine. :D Polling was always a no no thingy... :roll: To get it recognised by Rocrail, i just had to enter 1 sensormodule into the rocrail configmenu of the central. And version 1 or two for the version of the MCS2 software. This worked always perfect. 8) 8)

But recently (approx 2 weeks ago) Rocrail has undergone a program change for automatic registering S88 sensors modules connected to a MCS2. Now they use an unofficial way to poll for S88 sensors with version V1 of MCS2 firmware AND if sensors are set to a certain qty; like 1 module. If you set the MCS2 version to 2 (or 3) then Rocrail is going to use standards broadcast events; IF you set in Rocrail menu the sensor qty to 0! But in this case unfortunate NO S88 module is registered by automatic discovery! So we face now a chicken and a egg problem :twisted:
Because the arduinosketch doesn't have correct polling implemented, we should and would use auto broadcast events. So Rocrail should use the auto events method. But to get Rocrail use the S88 modules; you have to tell rocrail that you have xx qty of S88 modules. By setting 'hard' this qty, rocrail now assumes that you want to use polling... and so.... we are stuck to use the older Rocrail versions before this patch. :)
John Hoek
 

Re: Arduino interfaces Rocrail and GFP (Gleisbox)

Postby John Hoek » 03.03.2014, 20:47

I just looked at your newGFP sketch... Good documented and i am very impressed! :thumb_up:

Keep up the good work! :coding: :rr_for_ever:
John Hoek
 

Re: Arduino interfaces Rocrail and GFP (Gleisbox)

Postby woodyboy » 03.03.2014, 22:53

Hi John,

Here is the sketch for controlling the GFP. I have tested it with a Uno, Mega and a Leonardo. The Leonardo is undoubted the best to use. The great benefit of the Leonardo that it doesn't restart at opening the USB port. I can't get it right working with the UNO and the MEGA due to the fact they are restarting and in that time Rocrail sends it first packets. May be Rob will be so kind to delay about a second after opening the serial port on Rocrail, or maybe it is already a configuration item. As I wrote the Leonardo doesn't reset and it works great with the sketch. It has also slightly more RAM as the UNO, so queues can be configured some deeper.

After the Rocrail initialisation you can switch power several times till the GFP goes on. But is misses the first two packets with my current Rocrail 6793 version.

If you look in the sketch you see a lot of ethernet stuff. It has an embedded telnet server for debugging the script. I have tested the sketch without the ethernet shield attached and it works ok on the Leonardo, UNO and Mega.

If you want, you can give it a try to reconfigure in can/default.h
#define MCP2515_CS D,6 // for combi with Ethernetshield and LCD I2C
to
#define MCP2515_CS B,2

in this case the pin 10 of the shield doesn't to be rerouted, but maybe you get a conflict with the ethernetlib in the sketch. In the sketch you should also reconfigure the MCP2515_CS from 6 to 10.

I am curious about your progress and experiences with this sketch.
You do not have the required permissions to view the files attached to this post.
Regards,

Bert

Equipment: Roco WLANMaus, MS2, Gleisbox 2x(separated switch & rollingstock). Ubuntu 16. Edits booster. Arduino: S88 CANbus interface & Ethernet-CANbus gateway
woodyboy
 

Re: Arduino interfaces Rocrail and GFP (Gleisbox)

Postby woodyboy » 03.03.2014, 23:16

For the sensor sketch I will first implement the changes necessary to work correct again with Rocrail. After that, I will modify the sketch for use in conjunction with USB. I browsed the sourcecode and my knowledge of C is on the edge :roll: :shock: to understand what has changed. I hope it will be sufficient....
Regards,

Bert

Equipment: Roco WLANMaus, MS2, Gleisbox 2x(separated switch & rollingstock). Ubuntu 16. Edits booster. Arduino: S88 CANbus interface & Ethernet-CANbus gateway
woodyboy
 

Re: Arduino interfaces Rocrail and GFP (Gleisbox)

Postby John Hoek » 04.03.2014, 07:51

Hi bert,
Ill try this sketch this evening.

To inform: If i'm right, problem with Rocrail and sensors discovery started from the changes made at 9 and/or 11 februari.
John Hoek
 

Re: Arduino interfaces Rocrail and GFP (Gleisbox)

Postby John Hoek » 05.03.2014, 08:01

Hi Bert,

Yesterday evening i've tried the new sketches. The new USB version on Arduino Uno unfortunate locked up directly after i press lights on. However the behavior that you experienced that the gleisbox is not turned on when rocrail starts, is noit the case by me. tun on and off works like a charm.

So after this tests, i tried the Ethernet version (i received the w3100 shield too :wink: ) and this unfortunate shows the same behavior as with the USB version! :cry: Always locking up when too much commands were send to the loc. turnouts and lights works well. It looks that this version is a bit more stable, but far away for automatic control. Even manual switching loc DCC protocol commands give a lockup. sometimes it worked, other times... just directly a lock. when i turn on the gleisbox again everything is working again. :evil:

Because at your side Rocrail worked well. I suspect there is some problem with my layout or settings in Rocrail which gives this behavior. :?:
When i have time i reinstall Rocrail completly and remove all old parts, so that only the testtrack is on and Rocrail basic settings are set again.
From there i start new... :lol:

Bert,
Could you send me some screenshots of Rocrail with your settings, so i use the same base as you for testing on:
- MCS2-arduino central settings
- The MCS2-S88 sensor settings
- Rocrails AUTO tab settings
- version of Rocrail you use
- etc.
John Hoek
 

Re: Arduino interfaces Rocrail with GFP (Gleisbox) and S88

Postby woodyboy » 05.03.2014, 12:38

Hi John,

Power on at startup will surprise me if this works with an UNO. If the Arduino is restarted then manual power switching should be reliable as you wrote. To test the startup function please startup the GFP first with the MS2 attached. Press the stop button to manually switch of power off the track. Start Rocrail with the "power on at startup" checked configured . It is amazing if the GFP switches on. Because on opening the serial over USB the UNO resets.

With lights on, you mean the front lights or any function of a locodecoder? My recommendation is first to upgrade to minimal Rocrail 6918.

This evening I will try to publish screen shots of my configuration for the automatic and controller tabs. The same for the MS2 and gleisbox configuration. To my knowledge I have the last available firmware, 1.83 MS2 and 1.39 GFP, but I will review this.

For the S88, the good news is the sketch doesn't need te be updated. The start of Day was already implemented by events. If I read the thread in the German forum then the sketch implementation is more according to their definition/specification :mrgreen: as the original in the CS2.... Hmm, I smell a strange odor :wink:

I expect to publish the S88 USB sketch at friday or probably earlier. But with the 5100 you can also test the ethernet sketch of the S88.
Regards,

Bert

Equipment: Roco WLANMaus, MS2, Gleisbox 2x(separated switch & rollingstock). Ubuntu 16. Edits booster. Arduino: S88 CANbus interface & Ethernet-CANbus gateway
woodyboy
 

Re: Arduino interfaces Rocrail with GFP (Gleisbox) and S88

Postby John Hoek » 05.03.2014, 16:24

Yes, power on at startup works well at my side.
I have set into Rocrail into general Rocserver settings that the central is directly turned-on at startup. So when i start Rocrail by cliking Rocview, the server is started and the gleisbox turns on. this goes indeed very well. On/off by hand works also always. No retrying or what so ever needed...
My UNO board is not starting up again. (maybe there's a nold firmware on the arduno board on your side?) :?

With lights on; i mean the frontlights of my locs. As soon when i hit this button, or if Rocrail starts to move the loc; the gleisbox is switched off... Rocrail thinks that it's still on. So i have to turnof the 'lightbulb' into rocview and then on again to start the gleisbox. :oops:
In the meantime every sended command is lost. And rocrail don't acts anymore on feedback signals. It ' sees' the signals, but is not stopping the loc anymore. Even worse, the loc can't be controlled by rocrail anymore... It looks like the loc is frozen in the last condition. Moving the loc with the MS2 controller works in that case. When i reset everything in Rocrail i can drive the loc... until the next lockup. :(
This lockin-up also every time happend when i force a loc into a block and turn the frontlights off. Set the destination block. Then command the loc to autorun to this destination block. Then all turnouts are switched, the loc starts to move to thedestination block. When it arrives, it stops when the feedback sensors are triggered.. But this is a HARD stop; stop is made by (again) the gleisbox which switches off (red stop light on).

In all of this cases i run/have only one (1) loc on the track. I can drive at least 5 locs at once manaualy with MS2 without any problem. Turnouts and signals have their own powersupply, so i have sufficient power on the track. It must into the software/hardware combination... But were?... :?: :!:

I tried to parrallel look at the debug information with the Arduino USB serial monitor.
I see commands comming, but very soon i get a constant repeating message TX buffer full. In between i see CAN command flow. And then it crashes... I get a bit frustated by this. :roll:

---
I have the MS2 frimware 1.83 installed with the 1.39 version on the gleisbox.
If i use this combi together with the same board and CAn shield with desktop Station; it works perfect. No crashes. :D

I tried also last eveing the combination with the ethernet shield on other railroad software demo's. In this case it's just an CS2 without feedback modules installed.
Modellstellwerk initializes the arduino and turn on the gleisbox. After a short time i get an error that there is a miscommunication with CS2 and it siwtches off again.

With Itrain i can turn on/off the gleisbox with CS2 settings. I sometimes even hear some turnouts switcing. After some while i get an error and gleisbox is switched off. Never managed to get a train running here and/or switch a turnout by hand/forcing.

Windigipet don't want to turn on the gleisbox at all.

Traincontroller Bronze turns on the gleisbox. I can control the loc manual. But when i turn on the lights... gleisbox is locking... Were did i see that before?!.. 8)

I'm stuck here. :(
John Hoek
 

Next

Return to DIY Hardware