Commit 5270ceb6 authored by Pedro Henrique Kopper's avatar Pedro Henrique Kopper

Criar canal específico para leitura do termopar

parent 702436bd
Pipeline #19 failed with stages
......@@ -107,6 +107,33 @@ void setRate(void *ip, uint16_t rate) {
drvp->rate = rate;
}
float readMillivoltsTC(void *ip) {
const ADS1115Driver *drvp = (const ADS1115Driver *)ip;
uint16_t config = ADS1115_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
ADS1115_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
ADS1115_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
ADS1115_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
ADS1115_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
config |= ADS1115_REG_CONFIG_PGA_0_256V;
config |= drvp->rate;
config |= ADS1115_REG_CONFIG_MUX_DIFF_0_1;
config |= ADS1115_REG_CONFIG_OS_SINGLE; // Trigger conversion
write16bits(ip, ADS1115_REG_POINTER_CONFIG, config);
chThdSleepMilliseconds(1); // Wait for conversion to start
while(ADS1115_REG_CONFIG_OS_BUSY == (read16bits(ip, ADS1115_REG_POINTER_CONFIG) & ADS1115_REG_CONFIG_OS_MASK)) {
chThdSleepMilliseconds(1);
}
(void)chMsgSend(chRegFindThreadByName("status"), (msg_t)"Reading\r\n");
uint16_t res = read16bits(ip, ADS1115_REG_POINTER_CONVERT);
return ((int16_t) res) * ADS1115_VOLTS_PER_BIT_GAIN_SIXTEEN;
}
float readMillivolts(void *ip, uint8_t channel) {
const ADS1115Driver *drvp = (const ADS1115Driver *)ip;
......@@ -136,7 +163,7 @@ float readMillivolts(void *ip, uint8_t channel) {
static const struct ADS1115VMT vmt_ads1115 = {
setScale, setRate, readMillivolts
setScale, setRate, readMillivolts, readMillivoltsTC,
};
/*===========================================================================*/
......
......@@ -115,7 +115,8 @@
#define _ads1115_methods \
void (*setScale)(void *ip, uint16_t scale); \
void (*setRate)(void *ip, uint16_t rate); \
float (*readMillivolts)(void *ip, uint8_t channel);
float (*readMillivolts)(void *ip, uint8_t channel); \
float (*readMillivoltsTC)(void *ip);
struct ADS1115VMT {
_ads1115_methods
......@@ -157,6 +158,9 @@ typedef struct ADS1115Driver {
#define ads1115ReadMillivolts(ip, channel) \
(ip)->vmt->readMillivolts(ip, channel)
#define ads1115ReadMillivoltsTC(ip) \
(ip)->vmt->readMillivoltsTC(ip)
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
......
......@@ -71,7 +71,7 @@ int main(void) {
*/
//chThdSleepMilliseconds(500);
chThdCreateStatic(waStatus, sizeof(waStatus), NORMALPRIO, Status, NULL);
chThdCreateStatic(waDisplay, sizeof(waDisplay), NORMALPRIO, Display, NULL);
// chThdCreateStatic(waDisplay, sizeof(waDisplay), NORMALPRIO, Display, NULL);
chThdCreateStatic(waSensors, sizeof(waSensors), NORMALPRIO, Sensors, NULL);
chThdCreateStatic(waComm, sizeof(waComm), NORMALPRIO, Comm, NULL);
......
......@@ -7,6 +7,7 @@
#include "scpi/scpi.h"
#include "comm.h"
#include "sensors.h"
#include "display.h"
/* Comm THREAD
......@@ -34,12 +35,13 @@ static char scpi_input_buffer[SCPI_INPUT_BUFFER_LENGTH];
scpi_t scpi_context;
scpi_choice_def_t trigger_source[] = {
scpi_choice_def_t channels_mapping[] = {
{"A0", 1}, // A0 and A1 are swapped onboard for easier routing
{"A1", 0},
{"A2", 2},
{"A3", 3},
{"A4", 4},
{"TC", 5},
SCPI_CHOICE_LIST_END /* termination of option list */
};
......@@ -51,20 +53,63 @@ static size_t scpi_write(scpi_t * context, const char * data, size_t len) {
static scpi_result_t scpi_measure_voltage(scpi_t * context) {
int32_t param;
if (!SCPI_ParamChoice(context, trigger_source, &param, TRUE)) {
if (!SCPI_ParamChoice(context, channels_mapping, &param, TRUE)) {
return SCPI_RES_ERR;
}
float *reading = (float *) chMsgSend(chRegFindThreadByName("sensors"), (msg_t)param);
sacada_sensor_req req = {
.type = SACADA_READ_CHANNEL,
.channel = param
};
float *reading = (float *) chMsgSend(chRegFindThreadByName("sensors"), (msg_t)&req);
SCPI_ResultDouble(context, *reading);
return SCPI_RES_OK;
}
scpi_choice_def_t scales_mapping[] = {
{"0.256V", SACADA_0_256V},
{"0.512V", SACADA_0_512V},
{"1.024V", SACADA_1_024V},
{"2.048V", SACADA_2_048V},
{"4.096V", SACADA_4_096V},
{"6.144V", SACADA_6_144V},
{"18V", SACADA_18V},
{"9V", SACADA_9V},
{"4.5V", SACADA_4_5V},
{"2.25V", SACADA_2_25V},
{"1.125V", SACADA_1_125V},
SCPI_CHOICE_LIST_END /* termination of option list */
};
static scpi_result_t scpi_set_scale(scpi_t * context) {
int32_t channel, scale;
if (!SCPI_ParamChoice(context, channels_mapping, &channel, TRUE)) {
return SCPI_RES_ERR;
}
if (!SCPI_ParamChoice(context, scales_mapping, &scale, TRUE)) {
return SCPI_RES_ERR;
}
sacada_sensor_req req = {
.type = SACADA_SET_SCALE,
.channel = channel,
.scale = scale,
};
chMsgSend(chRegFindThreadByName("sensors"), (msg_t)&req);
return SCPI_RES_OK;
}
static scpi_command_t scpi_commands[] = {
{ .pattern = "*IDN?", .callback = SCPI_CoreIdnQ,},
{ .pattern = "*RST", .callback = SCPI_CoreRst,},
{ .pattern = "MEASure:VOLTage:DC?", .callback = scpi_measure_voltage,},
{ .pattern = "INput:SCALE", .callback = scpi_set_scale,},
SCPI_CMD_LIST_END
};
......
......@@ -50,7 +50,6 @@ static const ADS1115Config ads1115cfg = {
static ADS1115Driver ADS1115;
THD_WORKING_AREA(waSensors, 1024);
THD_FUNCTION(Sensors, arg) {
(void)arg;
......@@ -58,22 +57,55 @@ THD_FUNCTION(Sensors, arg) {
ads1115ObjectInit(&ADS1115);
ads1115Start(&ADS1115, &ads1115cfg);
ads1115SetScale(&ADS1115, ADS1115_REG_CONFIG_PGA_0_256V);
while (true) {
thread_t *tp = chMsgWait();
uint8_t channel = (uint8_t)chMsgGet(tp);
if(channel < 3) {
// Precision ADC channel
float volts = ads1115ReadMillivolts(&ADS1115, channel);
chMsgRelease(tp, (msg_t) &volts);
} else {
// General purpose ADC
// Right now it only reads ADC1
adcsample_t sample;
adcConvert(&ADCD1, &adcgrpcfg1, &sample, 1);
float volts = (sample / 4095.0f) * 18.0f;
chMsgRelease(tp, (msg_t) &volts);
sacada_sensor_req *req = (sacada_sensor_req*)chMsgGet(tp);
switch(req->type) {
case SACADA_READ_CHANNEL:
if(req->channel < 3) {
// Precision ADC channel
float volts = ads1115ReadMillivolts(&ADS1115, req->channel);
chMsgRelease(tp, (msg_t) &volts);
} else if(req->channel >= 3 && req->channel < 5) {
// General purpose ADC
// Right now it only reads ADC1
adcsample_t sample;
adcConvert(&ADCD1, &adcgrpcfg1, &sample, 1);
float volts = (sample / 4095.0f) * 18.0f;
chMsgRelease(tp, (msg_t) &volts);
} else if(req->channel == 5) {
float volts = ads1115ReadMillivoltsTC(&ADS1115);
chMsgRelease(tp, (msg_t) &volts);
}
break;
case SACADA_SET_SCALE:
if(req->channel < 3) {
switch(req->scale) {
case SACADA_6_144V:
ads1115SetScale(&ADS1115, ADS1115_REG_CONFIG_PGA_6_144V);
break;
case SACADA_4_096V:
ads1115SetScale(&ADS1115, ADS1115_REG_CONFIG_PGA_4_096V);
break;
case SACADA_2_048V:
palSetLine(LINE_LED0);
ads1115SetScale(&ADS1115, ADS1115_REG_CONFIG_PGA_2_048V);
break;
case SACADA_1_024V:
ads1115SetScale(&ADS1115, ADS1115_REG_CONFIG_PGA_1_024V);
break;
case SACADA_0_512V:
ads1115SetScale(&ADS1115, ADS1115_REG_CONFIG_PGA_0_512V);
break;
case SACADA_0_256V:
ads1115SetScale(&ADS1115, ADS1115_REG_CONFIG_PGA_0_256V);
break;
}
}
break;
default:
break;
}
//char buf[16] = {0};
//sprintf(buf, "Volts: %d.%d", (int)volts, (int) ((volts - ((int) volts)) * 100));
......
......@@ -4,4 +4,36 @@
extern THD_WORKING_AREA(waSensors, 1024);
THD_FUNCTION(Sensors, arg);
typedef enum {
SACADA_SET_SCALE,
SACADA_SET_SPEED,
SACADA_READ_CHANNEL,
SACADA_READ_THERMOCOUPLE,
} sacada_sensor_req_type;
typedef enum {
// Precision ADC scale settings
SACADA_0_256V = 0,
SACADA_0_512V,
SACADA_1_024V,
SACADA_2_048V,
SACADA_4_096V,
SACADA_6_144V,
// High-speed/voltage ADC scale setttings
SACADA_18V,
SACADA_9V,
SACADA_4_5V,
SACADA_2_25V,
SACADA_1_125V,
} sacada_sensor_scale;
typedef struct {
sacada_sensor_req_type type;
uint8_t channel;
uint8_t speed;
sacada_sensor_scale scale;
} sacada_sensor_req;
#endif /* SENSORS_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment