Cactus5
Hello all,

I am currently trying to poll data from some Thermocouples connected to the Raspberry Pi via an MCC134 measurement hat. 

I'm (mostly) sure the commands going to the daqhats.h library are correct as they come from MCC's documentation - I wanted to check that i have the configuration of the ignore commands correct, and also the commands to tell OpenPLC to write data to the %IWx input pins

does anything seem out of place?

#include "ladder.h"

#include <daqhats/daqhats.h>

//-----------------------------------------------------------------------------
// DISCLAIMER: EDDITING THIS FILE CAN BREAK YOUR OPENPLC RUNTIME! IF YOU DON'T
// KNOW WHAT YOU'RE DOING, JUST DON'T DO IT. EDIT AT YOUR OWN RISK.
//
// PS: You can always restore original functionality if you broke something
// in here by clicking on the "Restore Original Code" button above.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// These are the ignored I/O vectors. If you want to override how OpenPLC
// handles a particular input or output, you must put them in the ignored
// vectors. For example, if you want to override %IX0.5, %IX0.6 and %IW3
// your vectors must be:
// int ignored_bool_inputs[] = {5, 6}; //%IX0.5 and %IX0.6 ignored
// int ignored_int_inputs[] = {3}; //%IW3 ignored
//
// Every I/O on the ignored vectors will be skipped by OpenPLC hardware layer
//-----------------------------------------------------------------------------
int ignored_bool_inputs[] = {6,7,10,13,15};
int ignored_bool_outputs[] = {5,7};
int ignored_int_inputs[] = {1,2,3};
int ignored_int_outputs[] = {-1};

//-----------------------------------------------------------------------------
// This function is called by the main OpenPLC routine when it is initializing.
// Hardware initialization procedures for your custom layer should be here.
//-----------------------------------------------------------------------------
void initCustomLayer()
{
uint8_t BAddress0 = 0;
uint8_t TcChannel0 = 0;
uint8_t TcChannel1 = 1;
uint8_t TcChannel2 = 2;
uint8_t TcChannel3 = 3;
uint8_t TcType = 1; //Ktype thermocouple
uint8_t Interval = 1; //update interval in seconds

double TcVal0 = 0;
double TcVal1 = 0;
double TcVal2 = 0;
double TcVal3 = 0;

double OTcVal0 = 0;
double OTcVal1 = 0;
double OTcVal2 = 0;
double OTcVal3 = 0;
//Open my Mcc134


int mcc134_open(BAddress0); //open board at address 0

// tell the board what type of thermocouple is attached to each output
// K types used in my system- ktype = 1

int mcc134_tc_type_write(BAddress0,TcChannel0,TcType);
int mcc134_tc_type_write(BAddress0,TcChannel1,TcType);
int mcc134_tc_type_write(BAddress0,TcChannel2,TcType);
int mcc134_tc_type_write(BAddress0,TcChannel3,TcType);
//set the temp interval to 1
int mcc134_update_interval_write(BAddress0,Interval);


}

//-----------------------------------------------------------------------------
// This function is called by OpenPLC in a loop. Here the internal input
// buffers must be updated with the values you want. Make sure to use the mutex
// bufferLock to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
void updateCustomIn()
{
//read all thermocouple inputs
OTcVal0 = int mcc134_t_in_read(BAddress0,TcAddress0,TcVal0);
OTcVal1 = int mcc134_t_in_read(BAddress0,TcAddress1,TcVal1);
OTcVal2 = int mcc134_t_in_read(BAddress0,TcAddress2,TcVal2);
OTcVal3 = int mcc134_t_in_read(BAddress0,TcAddress3,TcVal3);
//write TC data to these inputs so OpenPLC can read
int_input[1] (OTcVal1) = %IW1;
int_input[2] (OTcVal2) = %IW2;
int_input[3] (OTcVal3) = %IW3;

}

//-----------------------------------------------------------------------------
// This function is called by OpenPLC in a loop. Here the internal output
// buffers must be updated with the values you want. Make sure to use the mutex
// bufferLock to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
void updateCustomOut()
{


// Example Code - Sending %QW5 value over I2C
// If you want to have %QW5 output to be sent over I2C instead of the
// traditional output for your board, all you have to do is, first add
// %QW5 to the ignored vectors, and then define a send_over_i2c()
// function for your platform. Finally you can call send_over_i2c() to
// send your %QW5 value, like this:
// if (int_output[5] != NULL) send_over_i2c(*int_output[5]);
//
// Important observation: If your I2C pins are used by OpenPLC I/Os, you
// must also add those I/Os to the ignored vectors, otherwise OpenPLC
// will try to control your I2C pins and your I2C message won't work.
}


Thanks very much!
Reply 0 0
thiagoralves
Your syntax on updateCustomIn() is wrong. %IW is not a valid C macro nor a construct. Also, you need to move the declarations of OTcValx variables out of the initCustomLayer() as they are not used in there. This is how you should have done it:

void updateCustomIn()
{
    double OTcVal0 = 0;
    double OTcVal1 = 0;
    double OTcVal2 = 0;
    double OTcVal3 = 0;
    
    //read all thermocouple inputs
    OTcVal0 = mcc134_t_in_read(BAddress0,TcAddress0,TcVal0);
    OTcVal1 = mcc134_t_in_read(BAddress0,TcAddress1,TcVal1);
    OTcVal2 = mcc134_t_in_read(BAddress0,TcAddress2,TcVal2);
    OTcVal3 = mcc134_t_in_read(BAddress0,TcAddress3,TcVal3);
    //write TC data to these inputs so OpenPLC can read
    if (int_input[1] != NULL) *int_input[1] = (uint16_t)OTcVal1;
    if (int_input[2] != NULL) *int_input[2] = (uint16_t)OTcVal2;
    if (int_input[3] != NULL) *int_input[3] = (uint16_t)OTcVal3;
}


As you can see, it's just a matter of following the example code from the comments
Reply 0 0
Robert Lewczuk
@thiagoralves I am trying to do something similar except with a adafruit Max31855 board. It reads a single K type thermocouple using SPI. I can currently run the following code just fine in python3 on the RPI constantly putting out a Temperature in terminal. I have tried to convert this to C inorder to put that code in the "Hardware Initialization procedure" section of the HWLCB tryin a few ways including using Cython with no luck. For reference this is the Python code.

# Max31855.py - Reads Thermocouple temp from Max31855 board
import board
import busio
import digitalio
import adafruit_max31855
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
cs = digitalio.DigitalInOut(board.D5)
max31855 = adafruit_max31855.MAX31855(spi, cs)
import time
while True:
print('Temperature: {} degrees C'.format(max31855.temperature))
test = max31855.temperature*1.8+32
print('Temperature: {} degrees F' .format(test))
time.sleep(3)

It seems I will have to learn C (eventually) but for now can I alternatively have this running in python inside a terminal box on my RPI that is also running OPENPLC and have write these values to the OPENPLC? If I understand correctly I would want to write to %IW1 in the OPENPLC but how from outside OpenPLC? Could I write directly to buffer address from my python? and do something in this section below only?

//-----------------------------------------------------------------------------
// This function is called by OpenPLC in a loop. Here the internal input
// buffers must be updated with the values you want. Make sure to use the mutex
// bufferLock to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
void updateCustomIn()
{
//read all thermocouple inputs
'what do i put here' = int 'what do i put here'('what do i put here');
//write TC data to these inputs so OpenPLC can read
int_input[1] ('What do I put here')= %IW1;

}


If you havent already guess I have zero coding outside the last 3 weeks. In fact i used my first linux system when I purchased the RPI4 a month ago and immediately found your project and have been chugging away since.
Thank you,
Rob
Reply 0 0
thiagoralves

It seems I will have to learn C (eventually) but for now can I alternatively have this running in python inside a terminal box on my RPI that is also running OPENPLC and have write these values to the OPENPLC? If I understand correctly I would want to write to %IW1 in the OPENPLC but how from outside OpenPLC? Could I write directly to buffer address from my python? and do something in this section below only?

Yes, the most guaranteed method is to use C on the hardware layer code box. If you don't know C, than that's hard. What you could do from Python is to use a Python Modbus library (like PyModbus for example) to send the reading values to a Modbus register on OpenPLC. You can learn more about the modbus registers OpenPLC has in here:  https://www.openplcproject.com/reference/modbus-slave/

Just pick one register you're not using in your program, and that can be read/written from outside. Let's get Holding Register 20 for example, it is mapped to location %QW20 on OpenPLC. From your Python program, you can make the measurements and then use PyModbus to connect to OpenPLC also running on the same machine (IP address is localhost or 127.0.0.1 for the same machine communication) on port 502 and then send the data to Holding Register 20.
Reply 1 0
Robert Lewczuk
Thiago pymodbus worked perfectly for me. All is well I might take a break for a week now haha.  I now have an easy solution for integrating polled data from any python program running in terminal directly into my openplc program.

This is how I did it for anyone else I did have to have update pymodbus to python 3

# Pymodbus RPI Install
sudo pip3 install --upgrade pip
sudo pip3 install pymodbus --upgrade


If using Max31855 board from adafruit on RPI
# Max31855 Circuit Python
sudo pip3 install adafruit-circuitpython-max31855


Below is the program I run for the Max31855 board and thermocouple. Open within Terminal and run it with python 3

import pymodbus
import time

from pymodbus.client.sync import ModbusTcpClient as ModbusClient
client = ModbusClient('localhost', port = 502)
client.connect()

import board
import busio
import digitalio
import adafruit_max31855

spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
cs = digitalio.DigitalInOut(board.D5)

max31855 = adafruit_max31855.MAX31855(spi, cs)
while True:
    tempC = max31855.temperature
    tempF = tempC * 9 / 5 + 32
    print("Temperature: {} C {} F ".format(tempC, tempF))
    time.sleep(2.0)
    client.write_register(20, int((max31855.temperature)*9 / 5 + 32))


Thanks again Thiago
Reply 0 0
f9gassib
Your syntax on updateCustomIn() is wrong. %IW is not a valid C macro nor a construct. Also, you need to move the declarations of OTcValx variables out of the initCustomLayer() as they are not used in there. This is how you should have done it:

void updateCustomIn()
{
    double OTcVal0 = 0;
    double OTcVal1 = 0;
    double OTcVal2 = 0;
    double OTcVal3 = 0;
    
    //read all thermocouple inputs
    OTcVal0 = mcc134_t_in_read(BAddress0,TcAddress0,TcVal0);
    OTcVal1 = mcc134_t_in_read(BAddress0,TcAddress1,TcVal1);
    OTcVal2 = mcc134_t_in_read(BAddress0,TcAddress2,TcVal2);
    OTcVal3 = mcc134_t_in_read(BAddress0,TcAddress3,TcVal3);
    //write TC data to these inputs so OpenPLC can read
    if (int_input[1] != NULL) *int_input[1] = (uint16_t)OTcVal1;
    if (int_input[2] != NULL) *int_input[2] = (uint16_t)OTcVal2;
    if (int_input[3] != NULL) *int_input[3] = (uint16_t)OTcVal3;
}



As you can see, it's just a matter of following the example code from the comments





Hi @thiagoalvez, i am trying to implement an i2c Custom Layer to read a ITG/MPU accelerometer. I am a bit confused by the code provided by cactus5 in this discussion. 

We read our sensors values in updateCustomIn() and then, for us to be able to acces those values in our PLC programs we have to write them in the int_input vector.

Q1) 
if we write -------------------> int_input[1] = 53;

then i am writing 53 to the %IW1 .... but for raspberryPi the is no %IW1 input pins when i look on the OpenPLC I/O mapping for the Raspberry Pi header.
in this case: is %IW1 just a label i use in my PLC program to get my data unrelated to I/O pins?
Q2) If so
Can I send by i2c multiple sensor values using only 2 I/O pins and then accesing them by writing something like this?
   if (int_input[1] != NULL) *int_input[1] = (uint16_t)OTcVal1;
    if (int_input[2] != NULL) *int_input[2] = (uint16_t)OTcVal2;
    if (int_input[3] != NULL) *int_input[3] = (uint16_t)OTcVal3;
Q3)
If so 
Is there a limit for that?

 

Thank you in advance Thiago. I think your hole work here is awsome ðŸ‘

 

 

Reply 0 0
thiagoralves
Yes, that’s all you have to do. The Custom Layer has direct access to OpenPLC buffers, like any other driver would do. Even if the Raspberry Pi doesn’t have an IW1 input, the buffer location exists as the core is the same on all platforms. So you can have your custom code manipulating IW1 at will, up to IW1023. They will be available on your PLC program at %IW1 ... %IW1023.

Tip: HWLCB was discontinued. Search the forum for PSM that replaced it. PSM is a lot easier to deal with because it is based on Python
Reply 0 0
f9gassib
Yes, that’s all you have to do. The Custom Layer has direct access to OpenPLC buffers, like any other driver would do. Even if the Raspberry Pi doesn’t have an IW1 input, the buffer location exists as the core is the same on all platforms. So you can have your custom code manipulating IW1 at will, up to IW1023. They will be available on your PLC program at %IW1 ... %IW1023.

Tip: HWLCB was discontinued. Search the forum for PSM that replaced it. PSM is a lot easier to deal with because it is based on Python
Yes, that’s all you have to do. The Custom Layer has direct access to OpenPLC buffers, like any other driver would do. Even if the Raspberry Pi doesn’t have an IW1 input, the buffer location exists as the core is the same on all platforms. So you can have your custom code manipulating IW1 at will, up to IW1023. They will be available on your PLC program at %IW1 ... %IW1023.

Tip: HWLCB was discontinued. Search the forum for PSM that replaced it. PSM is a lot easier to deal with because it is based on Python




thank you so much for the educational explanation 
Reply 0 0
Reply