Thursday, November 20, 2014

Sequence Control Using Arduino

I am considering Arduino as one of the method to making controller programming easier.

Nowadays it seems that everyone is using Arduino, you can even see elementary school kids programming using Arduino.

Accessibility of Arduino is incredible.
Leaving performance out of discussion, I have never seen anything like Arduino, which makes programming sooooo easy, in my lifetime. 

Thus, I started to think "what if I make controller using Arduino...?"

MPU and libraries used in Arduino are good enough to use for controller.
This is very obvious because ATmega328 that are being used for Arduino are already widely used for controller.

However, MPU is too exposed in Input/Output circuit. CANNOT BE USED
Also, it seems that designer has not thought of any power system. CANNOT BE USED

If you think about it in another way, you can use Arduino as controller if you make modification to Input/Output circuit and power system.

Modifying Arduino as whole will be too tiresome so I am suggesting a following experimental changes.

Input/Output Circuit -> Remote IO Module for FA
Power System -> Power Supply for FA

This is the first example.

I have tried Feedback Control and Sequence Control using Arduino.

The controlled system is Drilling Machine.

Drilling Machine

Drilling Machine Structure



Machine is composed of 3 parts.


PRESS

Drilling Machine Press - holds samples that are going to be drilled firmly

DC 24V Motor with reduction gear and worm gear drives metal structure to move up and down with powerful strength.

Top part has a limit switch that decides the stop location. However bottom part does not have limit switch. Location of bottom limit changes depending on the thickness of the sample, thus it is impossible to put limit switch.

Instead of limit switch, we measure the load on the press motor to decide the bottom limit. We need to constantly measure the current in the press motor and stop the motor when the current skyrockets. This step requires lots of site tuning.


ELEVATOR

Drilling Machine Elevator - holds drill motor

This also has a DC 24V Motor with reduction gear and rack and pinion gear drives drill motor to move metal structure up and down.

It has limit switch at both top and bottom.


DRILL

Drilling Machine Drill

On platform, there is big motor with AC 220V. On the axis there is drill bits.
The motor load needs to be monitored all the time, because if the sample does not get drilled easily because it is too firm the drill bits needs to move up and down multiple times and also because if this does not work the drilling needs to be stopped.
To measure the motor current, the motor power line wraps CT twice.



Arduino Interface


For direct connection with the machine I am going to use ADAM-4050 and ADAM-4017, which are both made for industrial purposes. ADAM-4050 has 8 channel Digital Out and 7 channel Digital In and ADAM-4017 has 8 channel Analog In. Both use RS-485 connections.

Below is channel list.

ADAM-4050

DO0: Elevation Motor ON
DO1:                         Direction(+)
DO2:                         Direction(-)
DO3: Press Motor       ON
DO4:                         Direction(+)
DO5:                         Direction(-)
DO6: Drill Motor        ON
DO7: Fan Motor         ON

DI0: Limit Switch Position UP
DI1:                                 MID
DI2:                                 BOTTOM
DI3: Manual Switch           START
DI4:                                 STOP
DI5:
DI6:

Elevator Motor and Press Motor both moves up and down, which means it is controlled both ways thus it needs 3 ch per motor. Drill Motor is one way, since it only needs to start the motor. Fan Motor is 24V DC. It also only needs to be turned on. It sends air to the drill bits cooling it down. Fan Motor is kept ON for 30 seconds even after the drilling.

We use three limit switch: PRESS' midpoint, top, and bottom.

There are two switches for manual control. One for starting and another one for stop.


ADAM-4017

VIN0: Elevation Motor Current
VIN1: Press Motor Current
VIN2: Drill Motor Current
VIN3:
VIN4:
VIN5:
VIN6:
VIN7: 

It reads current flowing in each motor.
Elevation Motor and Press Motor puts shunt resistance on the motor drive and measures the current. For Drill Motor we use CT sensor to read the converted current value. Readings from CT sensor are AC, thus it shows value that goes through full-wave rectifier and integrated using 60Hz LPF. In this case we only need to know the relative value of the load thus we do not measure the RMS value.


Below are connection diagram for ADAM-4050/ADAM-4017 and the machine.
(Detailed circuit diagram for the machine is company's asset thus it is not released publicly; however, if you really need the diagram please contact privately.)

ADAM-4050/ADAM-4017 and the Machine Connection Diagram

OR

ADAM-4050/ADAM-4017 and the Machine Connection Diagram

OR

ADAM-4050/ADAM-4017 and the Machine Connection

ADAM-4050/ADAM-4017 and the Machine Connection


Sequence Control

STANDBY

At standby PRESS and ELEVATOR are all resting at the top. All motors are switched OFF and waits for the START switch to be ON.


PRESS DOWN

START switch is ON. Until one of the stop condition is satisfied run the Press Motor downward.

Stop Conditions:
  1. If the Press Motor Shunt current value has passed 120% of moving average line.
    Moving average line is the calculated average value before the press reaches the sample. When the press starts to push down on the sample the motor current rapidly increases and when it exceeds 120% it should stop.
  2. If the Press Motor Shunt voltage has passed 3V.
    This is FAIL-SAFE. For some reason if there are too much pressure pushing down on the sample with large moving average line the Press should stop.
  3. If it has been over 17 seconds after the press motor has started working.
    This is also FAIL-SAFE. The press should not be working more than 17 seconds.


ELEVATOR DOWN

Now we start to lower the elevator that holds the drill. Turn ON the Drill Motor and Fan Motor. Until one of the stop condition is satisfied run the Elevator Motor downward.

There is only one stop condition:
  1. If the elevator has reached the bottom point.
    Bottom limit switch decides this. The drill bits have successfully penetrated the sample.

For the following cases, we higher the drill for a little bit and lower it again. When it does not drill well it is better to move up and drill again rather than continuously trying to drill. 
  1. If the Drill Motor's current rises more than 7% of the moving average line.
    Raise the drill (raise elevator) until the current falls below the 7% line and then lower the drill (lower elevator) again.
  2. If the Drill Motor has been on for more than 17 seconds.
    Move the drill up until the top line (raise the elevator) and then lower (lower the elevator). This is the same as restarting. Thus set the maximum of this action to 6. If it exceeds 6, we can safely conclude that the sample is impenetrable.

ELEVATOR UP

Drill work has finished whether it succeeded or not. Let's quickly put the elevator back to where it was.

Run the Elevator motor in UP mode until it reaches the top point. Keep the Drill Motor ON. If the Drill Motor has been turned OFF it might get stuck in the sample thus preventing it from being raised.

When it reaches the top point, turn both Elevator and Drill Motor OFF.


PRESS UP

Run the Press Motor in UP mode until it reaches the MID point.
When it reaches MID point, turn the Press Motor OFF. Leave the Fan Motor ON for extra 30 seconds.


In any stages, if the STOP switch is ON, go back to STANDBY mode.



SOURCE CODE

Source code can be viewed in the following site.
https://github.com/michelleseo/Arduino_Web/blob/master/SequenceControl/MicroFactory.ino

Explanation of code will be difficult since there are too much. If necessary I will answer any questions.



The following is the working video.


Thursday, June 19, 2014

DHT Subscriber - MQTT Arduino Gateway

Use WMQTT Utility to Subscribe Uploaded DHT-11 Temperature & Humidity Data


Download WMQTT Utility

http://www14.software.ibm.com/cgi-bin/weblap/lap.pl?popup=Y&li_formnum=L-TMAN-5N7JL9&accepted_url=ftp://public.dhe.ibm.com/software/integration/support/supportpacs/individual/ia92.zip
Dowload ia92.zip. No need to install, you just need to unzip.

For example, if you unzipped in C:\Evaluation\preview\MQTT\ia92 in DOS Box
cd C:\Evaluation\preview\MQTT\ia92\J2SE
java -jar wmqttSample.jar

Manual is in page 49 of IBM MQTT RedBook. (The page number refers to PDF Viewer's page)


Subscribe

Broker TCP/IP address - broker.mqttdashboard.com
Connect, if green light turns on,
Subscribe Topic - DHT11/temphum
and you will see DHT-11 Temperature & Humidity data in History


Subscribe Using WebSocket

For full source code refer to the link at the bottom.

If you want to see only the MQTT part -> https://gist.github.com/matbor/8837988



<!DOCTYPE html>
<html>
  <head>
    <title>Temperature & Humidity Subscriber</title>
    
    <script type="text/javascript" src="jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="globalize.min.js"></script>
    <script type="text/javascript" src="dx.chartjs.js"></script>
    <script type="text/javascript" src="knockout-3.0.0.js"></script>
    <script type="text/javascript" src="mqttws31.js"></script>
    
    <script type="text/javascript">
        var client;
        var t, h;

        $(document).ready(function() {
            t = $('<div style="width: 50%; height: 100%; float: left;" />').appendTo('#chartContainer');
            h = $('<div style="width: 50%; height: 100%; float: left;" />').appendTo('#chartContainer');
            var options = { scale: { startValue: 0, endValue: 40, majorTick: { tickInterval: 5}} };

            t.dxCircularGauge($.extend(true, {}, options, {
                geometry: { startAngle: 180, endAngle: 90 },
                value: 0,
                rangeContainer: {
                    ranges: [
                      { startValue: 28, endValue: 40, color: '#CE2029' },
                      { startValue: 0, endValue: 10, color: '#0077BE' }
                  ]
                },
                valueIndicator: { type: 'rangeBar' },
                title: { text: 'Temperature' },
                scale: {
                    label: { customizeText: function(arg) {
                    return arg.valueText + '&deg;C';
                    }
                    }
                }
            }));

            h.dxCircularGauge($.extend(true, {}, options, {
                scale: { startValue: 100, endValue: 0, majorTick: { tickInterval: 10 },
                    label: { customizeText: function(arg) {
                        return arg.valueText + '%';
                    }
                    }
                },
                geometry: { startAngle: 90, endAngle: 0 },
                value: 0,
                valueIndicator: { type: 'rangeBar' },
                rangeContainer: {
                    ranges: [
                        { startValue: 33, endValue: 0, color: '#77DD77' },
                        { startValue: 67, endValue: 33, color: '#E6E200' },
                        { startValue: 100, endValue: 67, color: '#92000A' }
                    ]
                },
                title: { text: 'Humidity' }
            }));
        });

        client = new Messaging.Client("broker.mqttdashboard.com", 8000, "HTMLTempHumSensor" + parseInt(Math.random() * 100, 10));

        // Connection Lost Message
        client.onConnectionLost = function(responseObject) {
            alert("Connection Lost: " + responseObject.errorMessage);
        };

        // Message Arrival
        client.onMessageArrived = function(message) {
            var data = message.payloadString.split(",");
            var temperature = parseInt(data[0]);
            var humidity = parseInt(data[1]);
            $(t).dxCircularGauge('instance').value(temperature);
            $(h).dxCircularGauge('instance').value(humidity);
        };

        // Connect Options
        var options = {
            timeout: 3,
            onSuccess: function() {
                alert("Connected");
            },
            onFailure: function(message) {
                alert("Connection failed: " + message.errorMessage);
            }
        };

        var publish = function(payload, topic, qos) {
            var message = new Messaging.Message(payload);
            message.destinationName = topic;
            message.qos = qos;
            client.send(message);
        }
        
    </script>
  </head>
  
  <body>
    <div id="chartContainer" style="height:450px;width:1000px;" align="center"></div>
    
    <table width="1000px">
      <tr>
        <td align="right">
          <button type="button" style="width:200px;" onclick="client.connect(options);">
            Connect
          </button>
        </td>
        <td align="center"> 
          <button type="button" style="width:200px;" onclick="client.subscribe('DHT11/temphum', { qos: 2 }); alert('Subscribed');">
            Subscribe
          </button>
        </td>
        <td align="left">
          <button type="button" style="width:200px;" onclick="client.disconnect();">
            Disconnect
          </button>
        </td>
      </tr>
      
      <tr>
        <td colspan="3">
          <div id="messages"></div>
        </td>
      </tr>
    </table>
  </body>
</html>



Full codes can be found in following links





Wednesday, June 18, 2014

DHT Publisher - MQTT Arduino Gateway

Uploading DHT-11 Temperature & Humidity Data using MQTT

Refer to Log Data Upload - Humidity & Temperature Web Logger posting for more information about Arduino & DHT-11.


Download MQTT Publisher Library for Arduino



Choose MQTT Broker

There are several public MQTT brokers; however, we are going to use HTML/JS to see Temperature & Humidity Data thus we need to use WebSocket.
I am going to use broker.mqttdashboard.com because it supports WebSocket.
IP is 212.72.74.21

Other good Brokers:
  • m2m.eclipse.org (IP: 198.41.30.241)
  • test.mosquitto.org (IP: 85.119.83.194)

If you want your own Broker, use Mosquitto.
Install and run mosquitto.exe.


TOPIC NAME

broker.mqttdashboard.com only supports 1 sub-tree, which means that it is possible to use "DHT11/temphum" as TOPIC, but "DHT11/temphum/room" will not work.



For full source code refer to the link at the bottom.
#include <SPI.h>
#include <Ethernet.h>
#include <dht.h>
#include <PubSubClient.h>

#define DHT11_PIN     A0
#define POLLINTERVAL  10000   // 10seconds interval

#define CLIENTID      "ArduinoTempHumSensor"
#define TOPICNAME     "DHT11/temphum"

byte mac[]    = { 0x90, 0xA2, 0xDA, 0x0F, 0x25, 0xC4 };
byte server[] = { 212, 72, 74, 21 };    // "broker.mqttdashboard.com"

dht DHT;

PubSubClient arduinoClient(server, 1883, callback); // We don't use it

char charSensorVal[20];
double humidity    = 0.0; // Sensed Humidity
double temperature = 0.0; // Sensed Temperature
int seq = 0;


void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac);
  
  // Connect to the MQTT Server - broker.mqttdashboard.com
  beginConnection();
}

void loop() {
  if((millis() % POLLINTERVAL) == 0) {
    if(getSensorVal()){
      dtostrf(temperature, 5, 2, charSensorVal);
      charSensorVal[5] = ',';
      dtostrf(humidity, 5, 2, charSensorVal + 6);
      // One line publishing
      arduinoClient.publish(TOPICNAME, charSensorVal);
      Serial.print(seq++);
      Serial.print(":");
      Serial.println(charSensorVal);    
    }
  }
}

void beginConnection() {

  Serial.println("Try connect to MQTT server");    
  
  int connRC = arduinoClient.connect(CLIENTID); // MQTT Broker connection
  
  if(!connRC) {
    Serial.println(connRC);
    Serial.println("Could not connect to MQTT Server");
    
    delay(100);
    exit(-1);
  }
  else {
    Serial.println("Connected to MQTT Server...");
  }
}

void callback(char* topic, uint8_t* payload, unsigned int length) {}

byte getSensorVal() {
  if(DHT.read11(DHT11_PIN) == DHTLIB_OK) {
    humidity = DHT.humidity;
    temperature = DHT.temperature;
    return 1;
  }
  else return 0;
}
Full codes can be found in following links






Saturday, June 14, 2014

Introduction - Heart Rate Monitor by MQTT

The wristband looks splendid, but the accuracy of measurement is apparently crappy.

Do wristband heart trackers actually work? A checkup.
According to the above link, finger tip works the best (Samsung Galaxy S5).

Performance wise we can say: finger tip > chest strap > wristband.
Wristband is is most wearable.
Unless you are planning on doing intense workout chest strap seems inconvenient.
Getting further into it will just be a waste of time.
Thus I'm going to ignore how pretty each things are and get into how each things will be implemented (if we start getting into how to make things more prettier, we will be creating super thimble at some point).



1. Photoplethysmographic Sensor



It calculates heart rate using the change in reflectance of light source as blood pressure changes.
There are plenty of cases relating to this on the internet.

Few examples:
Apparently all the batteries will be used by IR LED.
In normal condition we should constantly increase the measuring rate, and in abnormal condition we are in emergency state anyways.
In that condition battery should not be the problem, thus let's assume that we are going to measure constantly.



2. Bluetooth

Due to problem with battery we will have to use Bluetooth Low Energy (BLE), which only has 10 m long output power ( it will decrease even more because we are using PCB antenna ).
Depending on the situation we might have to use 2 APs.
The protocol will be MQTT-SN.



3. Battery

If we want to use Bluetooth to keep 24/7 watch, it will be impossible to use same battery for months.
Henceforth, when we are picking elegant design while increasing the volume we can use Lithium Polymer (Li-Po) battery.
Or even later on when we are mass producing we can customize the battery shape so that it fits into the case ( This is the ultimate goal ).
Two set of heart rate monitor should be one package, so that when one is being used the other can be charging.
Then it should contain a LED light that will let you know when the charging is needed.




4. MQTT

I'm going to use MQTT, because I don't need to build separate Service Server.
People who have huge interest in someone's heart can just register as subscriber to the broker.
If there is problem, Broker will send an e-mail or SMS immediately.
If there is no message, you could ask Broker.
However, the most simple way will be to call the person directly.




Friday, June 13, 2014

Summary of MQTT

MQTT (Message Queue Telemetry Transport)

The official Website

http://mqtt.org/

The final version of MQTT is 3.1


The final version of MQTT-SN (SN or S, Sensor Network) is 1.2
The client is WSN node (Wireless Sensor Network, like ZigBee), and we need broker like MQTT-S Gateway.

The spec sheet for MQTT is only 42 pages long and MQTT-S is only 28 pages long.

PT Informations to Help you Understand Better

Low Latency Messaging for Mobile Apps or When HTTP and Push Notification are Simply Not Enough
Building WSN with MQTT, RPi & Arduino

MQTT - MQ Telemetry Transport
Connecting Cars with IoT MQTT

Important Documents

IBM MQTT RedBook.  
Building Smarter Planet Solutions with MQTT and IBM WebSphere MQ Telemetry



Tips

.MQTT uses Network Byte / Bit ordering. (MSB is equal to bit<0>)
However, remaining length is in LSB-MSB order.





Thursday, June 12, 2014

HOW? - Arduino Remote Sensor Logger

It's going to cost some money to follow easy and simple way.
I'm going to purchase design kit (or evaluation board) that has been prepared by companies.
Sometimes it's costly; however, it's worth it.


TMP451

Costs $75.
USB Interface can be used on something else later on.




nRF24LE1

It contains 2 sets.
To send and receive I'll need 2.
Also it comes with a lot of things that seems unnecessary.


If we connect it, it'll look something like the following.




The next step...
Try this and that and if you get the feeling that this is the one, make it smaller. 





Wednesday, June 11, 2014

Construct - Arduino Remote Sensor Logger



There is not much difference from Components - Arduino Web Logger posting after adding Arduino.
Ethernet Shield has been replaced for WiFi Shield and RS-485 Shield is replaced by nRF24LE1 Shield.

Let's focus on Temperature Remote Sensor.
It is composed of 3 parts.

  1. Temperature Sensor
  2. RF Sender
  3. Battery

Temperature Sensor

I'm going to use TMP451 from TI.
This is introduction on their site.  http://www.ti.com/product/tmp451
And the following is the datasheet.  http://www.ti.com/lit/ds/symlink/tmp451.pdf


If you look at Power, it doesn't exceed 500 uA.
This is perfect for Battery.



Sending RF

I'm going to use nRF24LE1 from Nordic.
This is the chip that has been used in wireless mouse, keyboard, and remote controller for long time.
For this model, you can program internal 8051, thus you don't need additional Controller.
Power ranges from 1.9 V - 3.6 V, which is perfect for battery. I assume the price will be perfect also.
This is introduction for this model.  http://www.nordicsemi.com/eng/Products/2.4GHz-RF
And the following is the datasheet.  http://www.nordicsemi.com/eng/Products/2.4GHz-RF/nRF24LE1

However, power consumption rate could be a bit better.
If we add more peripherals it will exceed 20 mA.



Battery

I am wanting Battery that will last at least a year.
We are going to use it once in every 10 minutes and it will be used for 10 seconds using 20 mA.
Using simple math:
20 mA * 10 seconds * 6 times / hours * 24 hours * 365 days = 10,512,000 mA / sec = 2920 mAH
Thus we need Battery with size 3000 mAH.
3000 mAH is very common in AA size batteries now.
In Alkaline, DURACELL Normal or Energizer will be good enough.
There are lots of nikel-hydride battery, http://www.nordicsemi.com/eng/Products/2.4GHz-RF/nRF24LE1
However, the size of AA is not that small.  Thus the size of remote sensor will be the size of AA battery.  Of course the weight will be the same as AA battery.