Integration of the Arduino Library Adafruit MAX31865 in Matlab

18 visualizaciones (últimos 30 días)
Tobias
Tobias el 15 de Nov. de 2024 a las 10:23
Editada: Pavl M. hace 7 minutos
I would like to integrate the library for the MAX31865 sensor in Matlab to measure my temperature like in Arduino IDE. For this I need a corresponding header file and a corresponding Matlab class.
Here is the code I have programmed for the header file and the Matlab class. When I try to initialise the Arduino, I get the message that the board cannot be programmed. Where is my error?
a = arduino('COM6', 'Mega2560', 'Libraries', 'Adafruit/MAX31865');
Unable to create a communication link with the serial port. Please check the input argument PORT and verify that a device
is connected.
Header File
**
* @file MAX31865Base.h
*
* Class definition for MAX31865Base class that wraps APIs of Adafruit MAX31865 library
*
* @copyright Copyright 2023
*
*/
#include "LibraryBase.h"
#include "Adafruit_MAX31865.h"
#include "BusIO_Register"
#define MAX_SENSORS 4
Adafruit_MAX31865 *max31865Sensors[MAX_SENSORS];
// Arduino trace commands
const char MSG_MAX31865_CREATE_SENSOR[] PROGMEM = "Adafruit::max31865Sensors[%d] = new Adafruit_MAX31865(%d, %d, %d, %d);\n";
const char MSG_MAX31865_DELETE_SENSOR[] PROGMEM = "Adafruit::delete max31865Sensors[%d];\n";
const char MSG_MAX31865_READ_TEMPERATURE[] PROGMEM = "Adafruit::max31865Sensors[%d]->temperature(%f, %f);\n";
#define CREATE_SENSOR 0x00
#define DELETE_SENSOR 0x01
#define READ_TEMPERATURE 0x02
class AdafruitMAX31865Trace {
public:
static void createSensor(byte sensorNum, int8_t cs, int8_t mosi, int8_t miso, int8_t clk) {
if(sensorNum < MAX_SENSORS){
if (NULL != max31865Sensors[sensorNum]) {
delete(max31865Sensors[sensorNum]);
max31865Sensors[sensorNum] = NULL;
}
max31865Sensors[sensorNum] = new Adafruit_MAX31865(cs, mosi, miso, clk);
max31865Sensors[sensorNum]->begin();
debugPrint(MSG_MAX31865_CREATE_SENSOR, sensorNum, cs, mosi, miso, clk);
}
}
static void deleteSensor(byte sensorNum) {
if(sensorNum < MAX_SENSORS){
delete max31865Sensors[sensorNum];
max31865Sensors[sensorNum] = NULL;
debugPrint(MSG_MAX31865_DELETE_SENSOR, sensorNum);
}
}
static float readTemperature(byte sensorNum, float RTDnominal, float refResistor) {
if(sensorNum < MAX_SENSORS && max31865Sensors[sensorNum] != NULL){
float temp = max31865Sensors[sensorNum]->temperature(RTDnominal, refResistor);
debugPrint(MSG_MAX31865_READ_TEMPERATURE, sensorNum, RTDnominal, refResistor);
return temp;
}
return NAN;
}
};
class MAX31865Base : public LibraryBase
{
public:
MAX31865Base(MWArduinoClass& a)
{
libName = "Adafruit/MAX31865";
a.registerLibrary(this);
}
void setup(){
for (int i = 0; i < MAX_SENSORS; ++i) {
max31865Sensors[i] = NULL;
}
}
// Implementation of LibraryBase
//
public:
void commandHandler(byte cmdID, byte* dataIn, unsigned int payloadSize)
{
switch (cmdID){
case CREATE_SENSOR:{
byte sensorNum = dataIn[0];
int8_t cs = dataIn[1];
int8_t mosi = dataIn[2];
int8_t miso = dataIn[3];
int8_t clk = dataIn[4];
AdafruitMAX31865Trace::createSensor(sensorNum, cs, mosi, miso, clk);
sendResponseMsg(cmdID, 0, 0);
break;
}
case DELETE_SENSOR:{
byte sensorNum = dataIn[0];
AdafruitMAX31865Trace::deleteSensor(sensorNum);
sendResponseMsg(cmdID, 0, 0);
break;
}
case READ_TEMPERATURE:{
byte sensorNum = dataIn[0];
float RTDnominal = *(float*)(dataIn + 1);
float refResistor = *(float*)(dataIn + 5);
float temperature = AdafruitMAX31865Trace::readTemperature(sensorNum, RTDnominal, refResistor);
sendResponseMsg(cmdID, (byte*)&temperature, sizeof(temperature));
break;
}
default:
break;
}
}
};
Matlab Class
classdef MAX31865 < matlabshared.addon.LibraryBase & matlab.mixin.CustomDisplay
properties(Access = private, Constant = true)
CREATE_SENSOR = hex2dec('00');
DELETE_SENSOR = hex2dec('01');
READ_TEMPERATURE = hex2dec('02');
end
properties(Access = protected, Constant = true)
LibraryName = 'Adafruit/MAX31865'
DependentLibraries = {'Servo', 'I2C'}
LibraryHeaderFiles = {'Adafruit_MAX31865/Adafruit_MAX31865'};
CppHeaderFile = fullfile(arduinoio.FilePath(mfilename('fullpath')), 'src', 'MAX31865Base.h')
CppClassName = 'MAX31865Base'
end
properties(Access = public)
SensorNum
end
methods(Access = public)
function obj = MAX31865(parentObj, sensorNum, cs, mosi, miso, clk)
obj.Parent = parentObj;
obj.SensorNum = sensorNum;
% Create the sensor
cmdID = obj.CREATE_SENSOR;
inputs = [sensorNum, cs, mosi, miso, clk];
sendCommand(obj, cmdID, inputs);
end
function temperature = readTemperature(obj, RTDnominal, refResistor)
% Read the temperature from the sensor
cmdID = obj.READ_TEMPERATURE;
inputs = [obj.SensorNum, typecast(single(RTDnominal), 'uint8'), typecast(single(refResistor), 'uint8')];
output = sendCommand(obj, cmdID, inputs);
temperature = typecast(output, 'single');
end
end
end
  1 comentario
Pavl M.
Pavl M. hace 8 minutos
Editada: Pavl M. hace 7 minutos
Very good valued project question, it is needed to continue to build the embeddable code working:
DependentLibraries = {'Servo', 'I2C'}
LibraryHeaderFiles = {'Adafruit_MAX31865/Adafruit_MAX31865'};
CppHeaderFile = fullfile(arduinoio.FilePath(mfilename('fullpath')), 'src', 'MAX31865Base.h')
and in
a = arduino('COM6', 'Mega2560', 'Libraries', 'Adafruit/MAX31865');
try COM1, COM2
what is in :
MWArduinoClass.m ?
MOSI and MISO is data buses (pins) of SPI com. link and not of I2C. The Arduino is connected to PC via COM port and
the sensor is connected to the Arduino board via SPI or I2C?
Also how the servo is connected to the board?
a is an instance of MWArduinoClass, right?
Would you need to run it as:
a = MWArduinoClass(...) and not a = arduino(...)
If device is connected, the port check is passed valid, check also which mechanism which custom library format how is registered with matlab and let me know>

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Modeling en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by