154 lines
8.4 KiB
Plaintext
154 lines
8.4 KiB
Plaintext
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy for HTML5 for Apple macOS version 5.8.0">
|
|
<title>Hardware support: Multi-bit and Byte Communication Using CMRI</title>
|
|
<!-- Copyright 2020 Jerry Grochow -->
|
|
<meta name="author" content="Jerry Grochow">
|
|
<meta name="keywords" content="Arduino CMRI JMRI Byte Communication">
|
|
<!--#include virtual="/help/en/parts/Style.shtml" -->
|
|
</head>
|
|
<body>
|
|
<!--#include virtual="/help/en/parts/Header.shtml" -->
|
|
|
|
<div id="mBody">
|
|
<!--#include virtual="Sidebar.shtml" -->
|
|
|
|
<div id="mainContent">
|
|
<h1>Hardware support: Multi-bit and Byte Communication Using CMRI and arduinos</h1>
|
|
|
|
<div class="para">
|
|
<p>JMRI implements the <a href="../cmri/CMRI.shtml">CMRInet protocol</a> to send and
|
|
receive information via changing the state of JMRI objects:</p>
|
|
|
|
<ul>
|
|
<li>sending to a CMRI node that a turnout should be thrown or a light turned on or off
|
|
(any accessory attached to a CMRI output),</li>
|
|
|
|
<li>receiving from a CMRI node to indicate that a sensor has become active or inactive
|
|
(any CMRI input).</li>
|
|
</ul>
|
|
|
|
<p>JMRI objects are associated with a single CMRI bit in the case of sensors (input) and
|
|
lights (output), and one or two bits in the case of turnouts (see the CMRI help page
|
|
<a href="../cmri/CMRI.shtml#L405">section on setting up turnouts and lights</a> for use
|
|
with CMRI). With the introduction of microcontroller technology such as the <a href=
|
|
"https://www.arduino.cc/" target="_blank">arduino</a> CMRI nodes need no longer be restricted to
|
|
operate layout hardware based on single (or two) bit commands. Arduinos are quite capable
|
|
computers that can process CMRINet messages and act on what they receive under local
|
|
program control. <a href="index.shtml#CMRI">An arduino emulating a CMRI node</a> can be
|
|
programmed to start animations, play a series of sounds, light buildings in a realistic
|
|
way, or control the speed of accessory motors -- just about anything the modeller can dream
|
|
up.</p>
|
|
|
|
<p>Similarly, it is also possible to send information back to JMRI over CMRINet in the form
|
|
of numbers or letters, or any string that can be encoded in a few bytes of information.
|
|
JMRI would receive each byte as setting the state of eight sensors, but a simple JMRI
|
|
script can translate that binary information into the decimal number or character it
|
|
represents.</p>
|
|
|
|
<p class="note">It is also possible to send numbers and characters directly to JMRI from
|
|
arduinos by using <a href="index.shtml#serial">serial communications</a> without taking
|
|
advantage of JMRI's CMRInet capabilities. Each approach has its advantages and
|
|
disadvantages, as discussed <a href="index.shtml#connect">here.</a></p>
|
|
|
|
<p class="im"><img src="images/CMRISensorsByte.png" class="floatLeft" height="165" width=
|
|
"275" alt="Sensor Table with 8 sensors> Create eight sensors"> One example of this type
|
|
of usage would be using two JMRI lights (connected to one CMRI bit apiece) to tell the
|
|
arduino to either increase or decrease the speed of a motor attached to an animation
|
|
(perhaps a rotating carousel). The arduino can change the speed of the motor and then send
|
|
back a number indicating the current speed (perhaps 1 to 8) by encoding that number in
|
|
several bits to be sent as part of the CMRINet transmission. A <a href=
|
|
"../../tools/scripting/index.shtml">JMRI script</a> can be <a href=
|
|
"../../tools/scripting/HowTo.shtml#multturnouts">listening to changes</a> in these "speed"
|
|
bits and use them to create a decimal number in a <a href=
|
|
"../../tools/Memories.shtml"></a>JMRI memory variable for display on a panel, as
|
|
illustrated in a <a href="https://www.jmri.org/jython/CmriBitsToBytes.py" target=
|
|
"_blank">this example script.</a> The image here shows sensors 0, 1, 2, and 3 active which
|
|
would translate into the decimal number 15.</p>
|
|
|
|
<p class="im"><img src="images/CMRINode-MotorControl.png" alt=
|
|
"Example Panel Using Bit Display" class="floatRight" height="225" width="135">
|
|
Alternatively, a number of sensor bits can be allocated to a speed display with the arduino
|
|
activating the sensors to indicate percentage of maximum speed, as shown in the layout
|
|
panel image. Four of the eight bits of this carousel speed control are "lit" or active,
|
|
representing a speed of 50% of maximum. The panel also shows the on-off slider control and
|
|
clickable greater-than/less-than signs to command the arduino to run the carousel faster or
|
|
slower. The bottom part of the panel shows another example with a curved arrow which when
|
|
clicked tells the arduino to change the direction of the attached Ferris wheel (currently
|
|
off).</p>
|
|
</div>
|
|
|
|
<div class="list">
|
|
<h1>Example of JMRI setup</h1>
|
|
|
|
<p>The steps are fairly straightforward:</p>
|
|
|
|
<dl>
|
|
<dt class="left">Configure a CMRI node</dt>
|
|
|
|
<dd class="first">
|
|
<p><a href="../cmri/CMRI.shtml#L268">Configure a CMRI node</a> representing the number
|
|
of output bits (JMRI-&gt;CMRI node) and input bits (CMRI node-&gt;JMRI)
|
|
desired. Remember that <a href="../cmri/CMRI.shtml#L391">JMRI lights and turnouts
|
|
output to CMRI and JMRI sensors provide inputs</a> (think of it in terms of the CMRI
|
|
node: physical sensors input information to the node; the node then outputs information
|
|
to control physical turnouts and lights).</p>
|
|
</dd>
|
|
|
|
<dt class="left">Create light objcts</dt>
|
|
|
|
<dd>
|
|
<p>Create as many <a href="../../tools/Lights.shtml">JMRI lights</a> as you need to
|
|
tell the arduino to perform some task. Controlling whether a motor is on or off can be
|
|
done with a single light (INACTIVE (which sends a 0) for OFF, ACTIVE (which sends a 1)
|
|
for ON). Controlling its speed requires two lights - one to tell it to increase, one to
|
|
tell it to decrease, neither on to stay the same speed.</p>
|
|
</dd>
|
|
|
|
<dt class="left">Create sensor objects</dt>
|
|
|
|
<dd>
|
|
<p>Create <a href="../../tools/Sensors.shtml">sensors</a> as shown above for each byte
|
|
of information (number or character) you want to receive back from the arduino. If you
|
|
want to receive the motor speed as a number from zero to seven, you need three sensors.
|
|
If you want to receive larger numbers, you need more sensors to get more bits. Eight
|
|
bits can represent numbers up to 255 (the picture here shows bits corresponding to the
|
|
decimal number 14). Make sure the sensors are linked to the appropriate bits on your
|
|
arduino/CMRI node.</p>
|
|
|
|
<p class="note">While JMRI sends and receives a 0 or 1 in the CMRI bit steam, it
|
|
translates a zero into a sensor INACTIVE state, which is actually stored as 4
|
|
(requesting "state" and printing it as a number in a script will return a 4) and ACTIVE
|
|
as a 2. This is important in writing your JMRI script to translate received
|
|
sensor states as binary bits.]</p>
|
|
</dd>
|
|
|
|
<dt class="left">Create memory variable</dt>
|
|
|
|
<dd>
|
|
<p>Create <a href="../../tools/Memories.shtml">memory variable</a> to hold each byte of
|
|
information that will be computed by the script from the eight sensors.</p>
|
|
</dd>
|
|
|
|
<dt class="left">Run script</dt>
|
|
|
|
<dd>
|
|
<p>Create <a href="../../tools/scripting/HowTo.shtml#multturnouts">a listening
|
|
script</a> as discussed above to translate the sensor states into bits and create a
|
|
number of letter in the memory variable. An example script to convert eight sensor
|
|
states (bits) into a number can be found <a href=
|
|
"https://www.jmri.org/jython/CmriBitsToBytes.py" target="_blank">here.</a></p>
|
|
</dd>
|
|
</dl>
|
|
</div>
|
|
<!-- closes #list -->
|
|
<!--#include virtual="/help/en/parts/Footer.shtml" -->
|
|
</div>
|
|
<!-- closes #mainContent-->
|
|
</div>
|
|
<!-- closes #mBody, from before Sidebar -->
|
|
<script src="/js/help.js"></script>
|
|
</body>
|
|
</html>
|