Author Topic: CMRI Library Help programming (arduino)  (Read 5228 times)

0 Members and 1 Guest are viewing this topic.

Offline ThejmrTopic starter

  • Newbie
  • Posts: 4
CMRI Library Help programming (arduino)
« on: January 19, 2016, 08:42:55 pm »
Hi all  I sometime ago Posted on using a Arduino to help run my model railway Layout Well I can now use software to help draw and run the layout even better however I have come stuck with the coding I have tried many different options and none seem to work I'm pretty sure soon as its pointed out ill go OH YH that was it. My current code I am trying to get to work is
Code: [Select]
#include <CMRI.h>

CMRI cmri;
int CT1 = 0;
int input =0;
int n = 0;
void setup() {
  Serial.begin(9600, SERIAL_8N2); // SERIAL_8N2 to match what JMRI expects CMRI hardware to use
  pinMode(13, OUTPUT);
  pinMode(53, OUTPUT);
  pinMode(52, OUTPUT);
  digitalWrite(13,LOW);
  digitalWrite(52,HIGH);
  digitalWrite(53,HIGH);
  pinMode(12, OUTPUT);
}

void loop() {
  cmri.process();
//  digitalWrite(52, cmri.get_bit(0));
//digitalWrite(53, cmri.get_bit(1));

if (cmri.get_bit(0));
{
if (n == 0)
{
  if (CT1 == 0)
  {
    Point1A();
    CT1 = 1;
    RESET();
  }
  if (CT1 == 1)
  {
    Point1B();
    CT1 = 0;
    RESET();
  }
}
}




}
void Point1A()
  {
  digitalWrite(52,LOW);
  //cmri.set_bit(3, !digitalRead(12));
  delay(500);
  digitalWrite(52,HIGH); 
}
void Point1B()
{

  digitalWrite(53,LOW);
  //cmri.set_bit(4, !digitalRead(12));
  delay(500);
  digitalWrite(53,HIGH);
 
}
void RESET()
{
  //n = -1;
  digitalWrite(13,LOW);
}
The idea is the bit number it gets and then depending on a couple of other variables it does the according actions. currently I just have the two input bouncing and no seem of control.

I am guessing here but I need to use a varable to get the bit number that is incoming (which i tried with a simple
(cmri.get_bit(n)

which is shown on the page for the library but still nothing

So in simple terms so we are all on the same page in this case Ill use bit 0 which is on the software address 1
1. Read incoming bit number eg  cmri.get_bit(0)
2. Sets a variable.
3. Script reads what this variable is eg 0
4. Acts and checks the Next variable to find if the points is in position 1 or 2
5. Acts on that info throwing the point the correct way.
6. resets the variable in step 3 to stop the looping of this action
7. Wait for next incoming bit to act on

Hope it makes sense I did last time end up writing a good script to a control panel. this is my upgrade


this is the library https://github.com/madleech/ArduinoCMRI
 

Offline Landrew2390

  • Regular Contributor
  • *
  • Posts: 68
  • Country: us
Re: CMRI Library Help programming (arduino)
« Reply #1 on: August 25, 2016, 01:20:54 pm »
You've probably solved this problem at this point, but you need to enable pull-ups on your inputs to keep them from floating and you need to ditch the delay() function.  Without the pull-ups, the pin can rapidly cycle back and forth unless it's actively being driven by something external to the Arduino.  The delay() function is nice when you're starting out, but since you're trying to communicate over a serial connection, anything that comes in during the 500ms delay will be ignored.  I also don't understand why you're throwing a turnout (switch) for 500ms and then returning it to the original position.

I recommend you read the documentation for CMRI to get an understanding of how it works, then dive back into programming.
Oh look, a new hobby . . .
 

Offline rrinker

  • Super Contributor
  • ***
  • Posts: 2046
  • Country: us
Re: CMRI Library Help programming (arduino)
« Reply #2 on: August 31, 2016, 08:30:39 pm »
 I had intended to reply to this a long time ago, since I am working on some Arduino CMRI stuff myself (something to do until I get my basement redone to support a layout).

 I don't think the OP is trying to throw a turnout and then 500ms later change it back - i suspect that output is driving a solenoid switch machine. The way to handle that with simple digital outputs is hardware, as seen in Dr. Chubb's books and articles. Off the top of my head I forget what he called that one, but he has a controller board to drive solenoid machines with a simple pulse where the digital input is steady on - that eliminates the need for a DELAY() in the Arduino code which will cause havoc with the serial IO routines as you said.

 For other on/off cases, such as a flashing signal aspect, that can be done by the host computer, or to reduce network traffic, there is a variation of the CMRI Arduino library that is non-blocking, so you can use the main program loop to do things like flash the signal, or flash crossing gates.

 To get started, I have some RS485 chips, but I think I will see about using 1/4 load versions for my actual system as I can see having some 80 nodes - I intend for even 'manual' turnouts off the main line to be controlled via an Arduino node so the dispatcher will need to unlock the turnout before the buttons on the fascia will run it. I'm using servo motors for point motors, so the Arduino is ideal there. I also don;t plan to do a whole lot of shift register stuff to add extra ports, I'd rather have more nodes close to the items they are controlling than a giant node with lots of ports and bundles or wires all over the place. I'm using Unos and Megas at the bench for development but the actual nodes I am planning to use Nanos or Minis

 

Offline Landrew2390

  • Regular Contributor
  • *
  • Posts: 68
  • Country: us
Re: CMRI Library Help programming (arduino)
« Reply #3 on: September 01, 2016, 04:33:21 am »
Rrinker, your idea of using 1/4 load transceivers will work.  I'm working on a SMINI design using shift registers, but I'm using 1/4 load transceivers and have tested up to 56 nodes without any issues.

You're probably correct on the solenoid switch machines, but 500ms of drive time will burn the actuators to a crisp.  The best way to handle that is a capacitance discharge unit.  A correctly designed CDU will throw any solenoid based switch machine and then limit the current to a safe level.  I'm using the one below and it works well if the turnouts are in good condition.

Oh look, a new hobby . . .
 

Offline rrinker

  • Super Contributor
  • ***
  • Posts: 2046
  • Country: us
Re: CMRI Library Help programming (arduino)
« Reply #4 on: September 01, 2016, 06:08:28 pm »
 No I just have to buy or build a different USB to R485 adapter, the one I have has a MAX485 full load chip in it and is only rated for 32 nodes as expected. I might be able to just swap chops, I didn't notice anything int he data sheet that would require other circuit changes to make the 1/4 load version work.

 CD is definitely the way to go with solenoids - I haven't used solenoid machines in years. 10 years ago it was all Tortoise motors, now I use RC servos, the 9G type. But way back when, a CD power supply is the first electronic project I ever built from a schematic (as opposed to the experiments in my Radio Shack 100 in 1 and 150 in one, or specific electronic kits). At the time I was in N scale (before and now, HO) for space reasons, and I was burning up switch motors like crazy, the control buttons would often stick on and instantly vaporize a coil. So I saw this circuit in a book and built it, and after that, never had a problem. I still have it, but it is nothing to look at, I didn't even use a plain perf board. Just put the 2N3055 in a heat sink (almost certainly unnecessary is retrospect) and then soldered the other components to it. The input and output wires were also just soldered directly to the appropriate component leads. Ugly, but it worked. It's effectively the same circuit but with a 2N3055 instead of the Darlington for fast recovery, also mine has a single 2200uF cap instead of a pair of 1000's. And no indicator LED on mine. I built it in '77 or '78, when I was 11 or 12. Rapid progress in electronics after that - a few years later I built a single board computer kit as my first computer.

 I need to play around a bit with the RS485 library for Arduino, as it is, since it connect to the RX and TX pins, the RS485 chip interferes with using the USB port. OK for bench testing, just pop off a couple of jumpers on the breadboard, but to use a Megas should be no problem since they have the extra RX/TX. A lot of my nodes will need no more than a Nano though - but my plan is to just socket it, so if I need to reprogram after deployment I can just pop the Nano off the board. Or just use a pair of pin header jumpers, take them off to program, put them on to run. Depends on my mood when I get that far   :D

 

Offline Landrew2390

  • Regular Contributor
  • *
  • Posts: 68
  • Country: us
Re: CMRI Library Help programming (arduino)
« Reply #5 on: September 01, 2016, 08:23:38 pm »
Just to satisfy my curiosity, what region and era do you model?  I'm doing Southern/Norfolk & Western circa 1980.
Oh look, a new hobby . . .
 

Offline rrinker

  • Super Contributor
  • ***
  • Posts: 2046
  • Country: us
Re: CMRI Library Help programming (arduino)
« Reply #6 on: September 02, 2016, 12:45:13 am »
 I model the Reading in the mid 50's. Never got to scenery on my last layout before I moved - it's in pieces in my basement but I doubt I will reuse any of it. You can see some of my stuff on my web page. https://www.readingeastpenn.com  It's readily apparent that I am a computer and electronics geek and not an artist. I do also belong to the Reading Company Technical and Historical Society and run my equipment on our huge (last count, about 28x150) modular setup we take to various places around the area. I'm also active on the Model Railroader forums, naturally in the "Electrical and DCC" section. I have part of a working plan to fill my basement once I get the walls redone, which will be pretty much a freelance layout with a Reading 'flavor' rather than trying to duplicate any particular location exactly. Generic central and northeastern PA scenery, lots of coal trains (anthracite, not the wussy bituminous  :D - but there were plenty of B&O/WM soft coal hauled to places like Bethlehem Steel so I have nearly as many B&O hoppers as I do Reading).
 Been into trains for nearly 48 years now, there exists a home movie of me at age 2 running the train around the Christmas tree - and I turn 50 next month.

 

Offline rrinker

  • Super Contributor
  • ***
  • Posts: 2046
  • Country: us
Re: CMRI Library Help programming (arduino)
« Reply #7 on: September 04, 2016, 02:08:38 am »
 Well that was almost anti-climatic, but kind of cool just the same. I finally had a chance to sit down at my bench and play around with this stuff a little bit. After wasting some time trying to fix my Nano (broke a resistor off the bottom near the USB port - tried to resolder it but it is one TINY bugger plus I am having some eye trouble so even with the smallest tweezers, the small tip on my iron, and the best magnifier I had, it was a no-go. I think it actually may be in place, but the Nano was alternately detected and not detected by my computer. I did get it to remain connected long enough ti upload Blink, but then the LED was blinking dimly and way too fast. Oh well, it was like $4), I got everything hooked up to my Uno and the basic sketch loaded that sets it up for RS485 with Pin 13 controlled as CMRI output 1 on the first node, and a once a second toggle on CMRI Sensor 1. The Gearmo USB to RS485 was detected automatically by Windows 10, and I was able to configure JMRI to use the port. Added the light and sensor and - it just worked. The sensor alternates once per second and turning the light on and off turns the pin 13 LED on and off. I hooked up another LED to pin 12 and added some lines to the sketch, and added a second light in JMRI and - it worked!  Next step is to add a second node on the RS485 line, and then work on my own controller app (I don't really want to use JMRI).

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf