butchergg
Posts: 29
Joined: Sun Oct 28, 2012 6:14 pm

Re: Digital Command and Control (DCC) with pigpio waves

Sat Jul 31, 2021 6:13 pm

My bad, direct pigpio CV reads do work. Not sure what I was looking at last night...

Anyway, pushed a few more commits to clean stuff up, and the most recent commit seems to work with JMRI's DecoderPro, although I'm not yet familiar enough with the software to actually do anything. Right now, i'm thinking on how to do locomotive -adaptive baseline current assessment from the 20 power-on Reset packet stream.

Oh, as promised, here's a picture of my hardware:
DSZ_8768.jpg
DSZ_8768.jpg (216.08 KiB) Viewed 726 times
No need to point out the RPi; the board to it's left is one of those L298n motor drivers and the small board below it is an Adafruit INA219 current-sense board connected to the power-in. The white wires run to a MRC power pack; I'm using the track power dialed up to about 15v to run the HOn3 Blackstone K-27 locomotive with its sound decoder.

Two groups of wires run from the Pi:
1. GPIOs to the L298n, two to bipolar-modulate the signal, one to enable/disable the H-bridge, and a ground. I'm using GPIOs 17, 27, and 22, but wavedcc will read other selection from a configuration file.
2. I2C to the INA219 current-sense Qwiik connector. GPIOs 2 and 3 are I2C bus 1, and the current-sense board is at address 0x40

The current sense board is cut into the + rail of the power, with source connected to Vin+ and the load (L298n) connected to Vin-. It also has a connection to power ground through the purple wire, which enables voltage measurement.

Female breadboard connection wires made wiring a cinch; the challenge was connecting them to screw terminals in a couple of places. For that, I just stripped a 1" segment of wire, fastened it to the screw terminal, and slide the female breadboard connector onto the other end. The track connection are another couple of stripped 1" wires soldered to the track and stuck right into the L298n's output connector. Not exactly scenic...

One more bit, I use the config file to make wavedcc use the same GPIOs for ops and programming. I only have one locomotive on that track, so no risk of messing up other locomotives. Made it easy to write the CV read/write code.

Oh, one more rather important thing: While wavedcc in it's present form happily reads real-time current measurement, it does not monitor it for an overload condition, and corresponding power shutdown. I'm going to make that configurable in the conf file, and a few lines of code in the runDCCCurrent() thread should do it. Look for it in an upcoming commit.

User avatar
Gavinmc42
Posts: 6044
Joined: Wed Aug 28, 2013 3:31 am

Re: Digital Command and Control (DCC) with pigpio waves

Sun Aug 01, 2021 1:07 am

K-27, knew that rang a bell, the big brother Baldwin to the 2-6-2 NA series compounds used in Victoria, Australia.
Got even more interested in Narrow gauge after buying a cabin and land with Puffing Billy going past the backyard.

DCC Hat?
Might have to pinch the kids HO layout ;)
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

butchergg
Posts: 29
Joined: Sun Oct 28, 2012 6:14 pm

Re: Digital Command and Control (DCC) with pigpio waves

Sun Aug 01, 2021 2:21 pm

Gavinmc42 wrote:
Sun Aug 01, 2021 1:07 am
K-27, knew that rang a bell, the big brother Baldwin to the 2-6-2 NA series compounds used in Victoria, Australia.
Got even more interested in Narrow gauge after buying a cabin and land with Puffing Billy going past the backyard.

DCC Hat?
Might have to pinch the kids HO layout ;)
Been interested in railroading since I was a kid, but I didn't warm to narrow gauge until I moved to Colorado. Helped me to appreciate taking time out to visit the Pichi Richi railway in SA during business travels to Australia in the '90s - the Ghan is an interesting story of narrow-gauge railroading in rather harsh conditions.

I'm still trying to reconcile my old N-gauge affections with the new HOn3 interest...

User avatar
Gavinmc42
Posts: 6044
Joined: Wed Aug 28, 2013 3:31 am

Re: Digital Command and Control (DCC) with pigpio waves

Mon Aug 02, 2021 1:42 am

Been on the Ghan, 4 decades ago ;)
3'6" was not narrow gauge here, that was standard in some States.
Big mix of gauges in OZ in the old days.

Back when I was modeling (pre marriage) went to the conventions and caught up with guys who did the Colorado and other NG visits.
My collection of reference books and loco plans has most Oz NG in it.
Bunch of Climax books too as the timber guys used them here.

Had N gauge when kid, decades later did 2mm scale on Z gauge.
I got into electronics that way, soldering etc.
Got lathes and mills to learn machining.

Nearly got an automatic lathe to mass produce wheels/tires.
If I had found a local source of Nickel silver rod I might have got it.
The twin WW2 Swiss lathe I got turned out not to be modifiable for rod stock.
Anyway G-code on a CNC converted China Mill running TurboCNC on FreeDOS was fun.

Hoping to do this on Pi's one day.
FreeCAD, Inkscape work.
DIY CNC Lathe is on my list.
Narrow Gauge is useful for learning new skills.

Climax gears in Nn3/2mm scale :lol:
Had to get clock n watch books for that ;)
Lots of respect for those guys.
My oldest watch lathe is nearly 120 years old.

With my eyes now NG on 45mm gauge would probably be better.
I understand now why my Grandfather used O gauge ;)
Thinking 16mm scale not 2mm.
Plenty of room to stick a Pi Zero video camera in them.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

butchergg
Posts: 29
Joined: Sun Oct 28, 2012 6:14 pm

Re: Digital Command and Control (DCC) with pigpio waves

Mon Aug 02, 2021 7:44 pm

The latest commit has a simple current overload trip. It won't automatically recover, but I'll add that to the to-do list. I also added the full set of properties to the .conf file, but note that you'll want to change these to suit your particular situation, particularly the PROG* variables, which are currently the same GPIOs as MAIN*.

Also, the load and ack counts, as well as the quiescent current margin, are all based on my experience with the Blackstone K-27. I'm going to upgrade my other RPi running wavedcc (add the current-sense module), and with that i'll be able to test on N-scale locomotives. That'll give me an idea of the sensitivity of those properties. If you've cloned my code, you'll want to evaluate the quiescent current in ops, as well as the non-ack current draw in CV reading, to determine if these setttings will work for you. Edit: Oh, and I haven't really calibrated my INA219, so my current readings are not probably accurate. Methinks they be a bit high...

Now, a bit of fun: Early on, before I tried running locomotives, I compiled the then-current wavedcc on my Ubuntu desktop and attempted to run it over my home LAN to the RPi running the pigpiod. It connected just fine, but the pulsetrain as depicted in piscope had significant gaps. Since the locomotives depend on the modulated DCC signal for continuous power, I didn't think that'd work too well.

Today, I tried it again with the most current code; this time, I hard-wired my RPi to the Ethernet switch at my desk, the same one my Ubuntu computer is connected to. Started everything, Ubuntu wavedccd connected to RPi pigpiod just fine. I then started ops mode, the piscope display showed a continuous pulsetrain with intermittent gaps of 2-10ms. So, I ran a throttle and tried running my locomotive - worked like a treat! Not a single missed command. The gaps do not appear to be long enough to allow the on-board capacitor of the locomotive decoder to drain.

Emboldened, I then switched out of ops mode and into service mode and tried to read a configuration variable - this time, results not so good. Of three attempts, only one was successful. Since the over-the-air exchange between wavedccd and pigpiod to assess the current draw for the acknowledgement is in the single-millisecond range, I think that's what's tripping up CV reading. Per the DCC spec, the locomotive has to put out a 60ma draw for 6ms, +/- 1ms, as an ack response. I think my Ethernet switch is 100Mb/sec, wonder if 1Gb/sec would do the trick...

I am amazed that any part of that worked at all..... :D

butchergg
Posts: 29
Joined: Sun Oct 28, 2012 6:14 pm

Re: Digital Command and Control (DCC) with pigpio waves

Mon Aug 02, 2021 8:04 pm

Gavinmc42 wrote:
Mon Aug 02, 2021 1:42 am
With my eyes now NG on 45mm gauge would probably be better.
I understand now why my Grandfather used O gauge ;)
Thinking 16mm scale not 2mm.
Plenty of room to stick a Pi Zero video camera in them.
Yes, that, indeed. Good Wife bought me a pair of over-the-head magnifiers, but they don't convey depth very well. I think I can manage HO scale, but N...

Cameras would be neat!

User avatar
Gavinmc42
Posts: 6044
Joined: Wed Aug 28, 2013 3:31 am

Re: Digital Command and Control (DCC) with pigpio waves

Mon Aug 02, 2021 11:50 pm

Compiled ok, I wonder if it runs on PiCore Linux?
Have run the pigpiod on Picore years ago, so I know that works too.
PiCore is my preferred embedded Linux, runs on my 24/7 gadgets.

Wish I had grabbed one of my INA219 current sensor boards from work.
Now going to have to wait until after lockdown :(
I did grab some LTC2945 boards, could the alert output be used as current alarm?
It has an extra Ain, that might be useful for something.

Forgotten all my DCC commands, been decades.
Wonder if my N size decoders will run the kids HO loco?
Finding the right box in the basement will take time, which I now have more of :lol:
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

butchergg
Posts: 29
Joined: Sun Oct 28, 2012 6:14 pm

Re: Digital Command and Control (DCC) with pigpio waves

Tue Aug 03, 2021 2:12 am

Gavinmc42 wrote:
Mon Aug 02, 2021 11:50 pm
Compiled ok, I wonder if it runs on PiCore Linux?
Have run the pigpiod on Picore years ago, so I know that works too.
PiCore is my preferred embedded Linux, runs on my 24/7 gadgets.

Wish I had grabbed one of my INA219 current sensor boards from work.
Now going to have to wait until after lockdown :(
I did grab some LTC2945 boards, could the alert output be used as current alarm?
It has an extra Ain, that might be useful for something.

Forgotten all my DCC commands, been decades.
Wonder if my N size decoders will run the kids HO loco?
Finding the right box in the basement will take time, which I now have more of :lol:
If wavedcc is compiled with -DUSEPIGPIOD_IF=ON and pigpiod compiles, they should run. If you compile without the pigpiod interface, you can use wavedcc by itself, but you'll need to run it as root.

I just skimmed over the LTC2945 datasheet, should work. Would have to change a couple of things in the code: the I2C address if it's not 0x40, and there are a couple of register sets for config and calibrate that would need to be replaced with the LTC2495 equivalent. The register addresses for current and such will probably also be different.

The alarm pin is interesting; that would relieve wavedcc from polling the device every 1ms.

Now, probably the easiest way to test wavedcc is to use the command line program, aptly called wavedcc. When you run it, it'll tell you what configuration file it read, and present you with a '>' prompt. There, you can type in DCC++ commands, either with or without the <>. Here's what it takes to get a train going:

> 1 MAIN
> t 464 2 1

'1 MAIN' turns on ops mode, 't 464 2 1' tells the locomotive with the long address 464 to so speed 2, in the forward direction. Put in a few 't's, when you're done with ops mode enter:

> 0 MAIN

to turn off the pulsetrain. Service mode works like this:

> 1 PROG
> R 1

'1 PROG' turns on service mode, 'R 1' reads CV1, which contains the short address.

Oh, functions can be sent with either the 'f' or 'F' DCC++ commands; I think 'F' is easier at the command prompt as you don't have to OR a bunch of bits in your head... :D

Here's the DCC++ Command Reference:

https://dcc-ex.com/reference/software/c ... rence.html

Don't forget, you don't need the < and > in wavedcc...

butchergg
Posts: 29
Joined: Sun Oct 28, 2012 6:14 pm

Re: Digital Command and Control (DCC) with pigpio waves

Tue Aug 03, 2021 2:34 am

Oh, if you have JMRI installed somewhere, you can use it to run trains with wavedccd. First, make sure wavedccd is running on the Pi. Then, in JMRI Preferences, add a System/Manufacturer:DCC++ Connection, System Connection:DCC++ Ethernet, and use the PI's IP address and port 9034. Once JMRI is connected to wavedccd, you can use the Throttle window to run a locomotive in your roster.

User avatar
Gavinmc42
Posts: 6044
Joined: Wed Aug 28, 2013 3:31 am

Re: Digital Command and Control (DCC) with pigpio waves

Tue Aug 03, 2021 3:08 am

Had forgotten about JMRI, oh look.
https://www.jmri.org/install/Raspbian.shtml
Looks like all the pieces are there now.

8mm scale on HO track?
Close enough to On2 or On30?
Would have to go out into the Garden for 16mm scale ;)
Probably more room in the garden ;)
SM45, SM32?
Wonder what the cost of track is these days and if the dogs will put up with them?

Code 250 Aluminium track, run from batteries with RF control?
Where are my Garden Railway books n mags?
Scratch build 16mm will be so easy after 2mm.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

butchergg
Posts: 29
Joined: Sun Oct 28, 2012 6:14 pm

Re: Digital Command and Control (DCC) with pigpio waves

Mon Aug 09, 2021 11:29 pm

Over on the TrainBoard, BigJake pointed out the err of my ways, that looping through 0..255 is not a good idea when you can verify individual bits of a configuration variable. Indeed, the DCC spec (in S-9.2.3) has bit write and verify operations in service mode, so doing 8 of those to divine a CV's value is quite a bit faster than "is it 0? No?, okay, is it 1? No? okay..."

I pushed a commit today that implements a bit-wise CV read. It starts by verifying bit 0 for both '1' and '0'; if I don't get an ack for at least one of them, I assert an error and stop. Probably no locomotive on the service track. I use these results to initialize the value variable to either 1 or 0, then I walk the rest of the bits to look for a 1. If I get an ack, I set the appropriate bit in the value variable with

Code: Select all

val = val | 1<<i;
Otherwise, I just leave the '0' that's already there alone.

Thinking on it some more, there's a small-but-not-zero chance of some interruption of the locomotive connection after the first bit. So, I'm probably going to change the logic to check both '1' and '0' for each bit. More reliable, at the expense of a bit of time.

Getting close to a usable first iteration...

butchergg
Posts: 29
Joined: Sun Oct 28, 2012 6:14 pm

Re: Digital Command and Control (DCC) with pigpio waves

Fri Aug 13, 2021 10:40 pm

So, I added a step to the read logic in the previous post: after I collect all the 1-verifies to make the CV value, I verify that value with a verify-byte packet. And, I encase the logic in a loop that retries up to four times, until the verify-byte renders an ack pulse.

Some decoders behave nicely, with a consistent ack pulse current well-above the DCC spec of 60ma. I have one that, not so much, varies significantly from verify to verify. Sometimes, its ack pulse sits right at the 60ma threshold, and sometimes the quiescent variation pushes that below the threshold. With that locomotive, I'll occasionally see 2 attempts, even 3 attempts before a valid value is divined. But, after banging at CV-reads for a while, did not get any false positives.

At this point, I'm going to change to maintenance mode - use it, find bugs, fix them. I'll also make minor improvements, like a duty-cycle-adjusted current monitor interval. After a few weeks of this, I'll tag a 0.1 release.

I'm also interested in hearing from others' experiences using it. I have two setups, one HOn3 and one N, three locomotives total, so my experience with it is pretty limited. If you find a bug, you can post an issue at the github repo if you have an account there, or here will work fine.

https://github.com/butcherg/wavedcc

Return to “Interfacing (DSI, CSI, I2C, etc.)”