Tag Archives: agent

MTConnect Agent – FANUC Macro Variables

Macro variables are used in just about every shop process because of their flexibility and utility.  Being able to read them is a critical part of any data collection system.  As MTConnect focuses on standardization, the variables are excluded.  It is however simple to add them to any MTConnect connection to a FANUC CNC control.  Once the Adapter is setup to read macro variables ( see tutorial here) It’s time to setup the Agent.

The MTConnect agent setup is surprisingly simple.  Almost too simple.  Initially I struggled for hours trying to learn and understand how to extend the agent schema files to accept the macro variables.  I used this tutorial here as a base.  It turns out the only necessary step is to add the following line to the device xml file…

VMC-3Axis.xml

<DataItem type="VARIABLE" category="EVENT" id="mc1" name="whale">

That’s it…  That’s That and That’s all.  The type and id can be anything you like.  In this example I’ve used VARIABLE” and “mc1“.  The agent simply matches the value of the name property, with the matching name that is output from the adapter. 

adapter.ini

[adapter]
port = 7878
service = MTC Focus 1

[focus]
host = 10.211.55.5

[macros]
whale = 500

[pmc]
SspeedOvr = 30
Fovr = 12

Macro variable 500 is tied to  the name “whale” at the adapter.  The agent matches the name “whale” and continues to pass it on to the client.

macro screen

Too easy.

MTConnect Blog Posts

MTConnect Links

As the number of posts increases, some are getting lost.  Just to keep things organized here is a list of posts grouped by topic.

Machine

FANUC FS0iD FOCAS Setup

Adapter

MTConnect FANUC Adapter on Ubuntu Linux

MTConnect Adapter for Windows

MTConnect FANUC Adapter PMC Addresses

MTConnect-FANUC Macro Variables

Agent

MTConnect Agent on Ubuntu Linux

MTConnect Agent – FANUC Macro Variables

Client

This would be a great place to make any requests for tutorials or posts.  I’m happy to entertain.  Of course the target should be MTConnect :)

MTConnect for FANUC Overview

 

 

When first starting developing with MTConnect all of the pieces can be very overwhelming.  I thought it might be helpful to break down the pieces and the connection between them.  Even though the MTConnect Adapter for FANUC and the MTConnect agent are complete from the GitHub repository, when developing client applications or setting up the pieces an overview is helpful.

overview

Click the picture for a larger view

Devices

Setting up MTConnect starts at the FANUC control.  The control must have an Ethernet connection and the optional FOCAS function.  Most modern controls have FOCAS available from the factory via an embedded Ethernet port on the main board of the CNC control.  Check this blog post here to setup the FOCAS connection.

The adapter and agent are best run on a server based on either Linux or Microsoft Windows.  Additionally, running the adapter on a low cost Linux platform located directly in the CNC can help reduce server and network load.

Communication

The FANUC control only speaks FOCAS, a very robust and powerful API that personal computers use to read and write information on the CNC.  The MTConnect adapter does all the heavy lifting and converts FOCAS to an MTConnect data stream.   The adapter streams data to the agent via http protocol which is human readable from Internet Explorer.  Here is the sample output from the FANUC adapter:  http://adapter.ip.address:7878/

2014-07-08T20:45:54.373983Z|system|NORMAL||||
2014-07-08T20:45:54.373983Z|Xtravel|NORMAL||||
2014-07-08T20:45:54.373983Z|Xoverheat|NORMAL||||
2014-07-08T20:45:54.373983Z|Xservo|NORMAL||||
2014-07-08T20:45:54.373983Z|Ytravel|NORMAL||||

Collation

As all of the pieces of MTConnect are based on some type of TCP communication, the devices find each other by knowing the IP Address and port number of the previous device.  The client knows the IP address of the agent, the agent knows the adapter, and the adapter knows the FANUC CNC.

Finally

Ultimately the client will consume XML requested from the agent.  The structure of the XML is determined by the schema specified for each machine.  The agent takes adapter data and matches the schema name‘s with adapter stream labels.  It records the stream into a buffer.  It is that buffer of data that is served when the client requests the XML.

 

MTConnect Agent on Ubuntu Linux

Preparation 

Before MTConnect can be installed some prerequisites must be in place.  A clean install of Ubuntu 14.04 LTS will not have many of the packages required.  Install these packages using the apt-get command.

$ sudo apt-get install libxml2 libxml2-dev cmake git libcppunit-dev build-essential screen ruby

After all the required packages are in place, the MTConnect source code needs to be downloaded from GutHub.  The source code should compile without any modifcation using cmake and make.

$ cd ~
$ git clone https://github.com/mtconnect/cppagent.git
$ cd cppagent
$ cmake .
$ make

MTConnect Agent Installation

Now that binaries are compiled from the MTConnect source, they just need to be put into appropriate directories.   The agent binary and the configuration file are first.

$ sudo mkdir /etc/mtconnect
$ sudo cp ~/cppagent/agent/agent.cfg /etc/mtconnect/
$ sudo cp ~/cppagent/agent/agent /bin/agent

The source from GitHub contains a script to run MTConnect as a daemon.  I instead opted to write a BASH script that suited my needs a little better.  The script is available at the end of blog.  The blog downloads it from this server.  After it is downloaded it is given permission to execute and added to the startup scripts.

$ cd ~
$ wget http://www.ellisware.com/downloads/agent.sh
$ sudo mv ~/agent.sh /etc/init.d/agent
$ sudo chmod +x /etc/init.d/agent
$ sudo update-rc.d agent defaults

The script requires the user ‘mtconnect’.  So we need to add that user.  Pick a password that is appropriate for you.  For security reasons, you probably should pick a different username and change it in the script, however, this blog uses the default.

$ sudo adduser mtconnect

MTConnect Simulator Installation

The default configuration file that was downloaded with the source code defines a simulator.  For the MTConnect agent to start, it needs to connect to all devices listed in its configuration file.  So now we setup the simulator device.

$ sudo mkdir /etc/mtconnect/simulator
$ sudo rsync -avr ~/cppagent/simulator/ /etc/mtconnect/simulator/

The default configuration file expects the simulator adapter to be one directory above where we placed it.  To correct this, edit the agent.cfg file and point to the correct path for the simulator.

$ sudo nano /etc/mtconnect/agent.cfg

Original Path::  “Devices = ../simulator/VMC-3Axis.xml”

New Path:: “Devices = ./simulator/VMC-3Axis.xml”

Finally, the simulator is started up in a fresh screen.  The simulator blocks the process so just detach the screen using control A, D, after it is up and running.

$ screen -S simulator
$ cd /etc/mtconnect/simulator
$ ruby -v run_scenario.rb -l VMC-3Axis-Log.txt

<cntrl><a> followed by <d>

Testing

Testing the MTConnect agent is very simple.  First the agent is started using the script we created, if the script completes successfully we can make sure it is listening for incoming connections.

$ sudo service agent start
$ netstat - tcp -l

Active Internet connections (only servers)
tcp 0 0 *:7878 *:* LISTEN
tcp 0 0 *:5000 *:* LISTEN

With the default configuration the agent is listening on port 5000 and the simulator is listening on port 7878.  If these are listed in the netstat, then everything is good.   For the final confirmation, any browser can be used as client.

agent.ellisware.com:5000/ or agent.ellisware.com:5000/probe

agent.ellisware.com:5000/current

agent.ellisware.com:5000/assets

agent.ellisware.com:5000/sample

 

Conclusion

Please let me know any mistakes, omissions or improvements in the code.  It took a great deal of patience to get this up and running, so feel free to ask any questions in the comments.

 

###### agent.sh #######

 #!/bin/bash
 # /etc/init.d/agent
 # version 0.0.1 2014-05-09 (YYYY-MM-DD)
 
 ### BEGIN INIT INFO
 # Provides: agent
 # Required-Start: $local_fs $remote_fs
 # Required-Stop: $local_fs $remote_fs
 # Should-Start: $network
 # Should-Stop: $network
 # Default-Start: 2 3 4 5
 # Default-Stop: 0 1 6
 # Short-Description: MtConnect server
 # Description: Starts the MTConnect Agent server
 ### END INIT INFO
 
 #Settings
 SERVICE='agent'
 OPTIONS='agent.cfg'
 USERNAME='mtconnect'
 CONFIGPATH='/etc/mtconnect/'
 INVOCATION="$SERVICE $OPTIONS"
 ME=`whoami`

as_user() {
if [ "$USERNAME" != "$ME" ]; then
  su - $USERNAME -c "$1"
else
  bash -c "$1"
fi
}

mc_start() {
if netstat - tcp -l | grep -q 5000
then
  echo "$SERVICE is already running!"
else
  echo "Starting $SERVICE..."
  cd $CONFIGPATH
  as_user "cd $CONFIGPATH && screen -dmS agent $INVOCATION"
  sleep 10
  if netstat - tcp -l | grep -q 5000
  then
    echo "$SERVICE is now running."
  else
    echo "Error! Could not start $SERVICE!"
  fi
fi
}

mc_stop() {
if netstat - tcp -l | grep -q 5000
then
  echo "Stopping $SERVICE"
  as_user "screen -p 0 -S agent -X stuff \"^C\""
  sleep 10
else
  echo "$SERVICE was not running."
fi
if netstat - tcp -l | grep -q 5000
then
  echo "Error! $SERVICE could not be stopped."
else
  echo "$SERVICE is stopped."
fi
}

#Start-Stop here
case "$1" in
start)
mc_start
;;
stop)
mc_stop
;;
restart)
mc_stop
mc_start
;;
status)
if netstat - tcp -l | grep -q 5000
then
  echo "$SERVICE is running."
else
  echo "$SERVICE is not running."
fi
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac
 
exit 0

<noinclude>[[Category:Guides]]</noinclude>