Hallo an alle,
ich programmiere gerade ein Prüfstand auf einen ESP32S3- DevKitC v1. Kurz zum Aufbau, es ist ein ADS1256 Modul und ein DAC DFR0971 eingebaut. Die Schaltung wurde schon getestet und funktioniert einwandfrei. Jetzt wollte ich das eigentliche Programm beginnen, dabei benötige ich FreeRTOS, da ich einige Sachen parallel betreiben will, ohne die Laufzeit von kritischen Tasks zu beeinflussen. Ihr müsst wissen ich bin neu in Sachen FreeRTOS.
Ich habe begonnen zwei Tasks zu erstellen, der erste Task "adcTask" soll mir die ADC Werte in ein Array schreiben bis es voll ist. Dieses wird dann in ein zweites Array kopiert und dem zweiten Task "averageTask" übergeben. adcTask läuft dabei auf den 1 Core und averageTask auf den 0 Core. Ich habe es so gelöst damit der adcTask nicht von der Mittelwert Berechnung beeinflusst wird.
Das größte Problem was ich jetzt habe ist die Serial.Print() Funktion gibt mir nur manchmal werte aus. Was mache ich Falsch?
In der Loop() habe ich noch mein altes Testprogramm aus kommentiert, damit ich die SPS vergleichen kann.
Vielen dank schonmal an euch für eure Hilfe.
LG Stefan
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include <ADS1256.h>
#include <SPI.h>
#include <DFRobot_GP8403.h>
#include <Wire.h>
bool check = false;
// Konstanten für ADC
// Globale Variablen für die Werte und den Mittelwert
const int ADC_SAMPLES = 100;
float current_1[ADC_SAMPLES];
float current_2[ADC_SAMPLES];
float voltage_1[ADC_SAMPLES];
float voltage_2[ADC_SAMPLES];
volatile int adcIndex = 0;
float adcAverage = 0;
float clockMHZ = 7.68; // crystal frequency used on ADS1256
float vRef = 2500; // voltage reference
// Construct and init ADS1256 object
ADS1256 adc(clockMHZ, vRef, false);
//Variabeln für SPS Berechnung
float sps;
long lastTime, currentTime, elapsedTime;
int counter;
DFRobot_GP8403 dac(&Wire, 0x5F);
void setup() {
Serial.begin(2000000);
Serial.println("Starting ADC");
adc.begin(ADS1256_DRATE_15000SPS, ADS1256_GAIN_2, false);
Serial.println("ADC Started");
//DAC init
while (dac.begin(46, 3) != 0) {
Serial.println("init error");
delay(1000);
}
Serial.println("init succeed");
dac.setDACOutRange(dac.eOutputRange10V);
// DAC auf Null setzen
dac.setDACOutVoltage(0, 0);
dac.setDACOutVoltage(0, 1);
dac.store();
//Task generieren
xTaskCreatePinnedToCore(adcTask, "adcTask", 4096, NULL, 1, NULL, 1);
xTaskCreatePinnedToCore(averageTask, "averageTask", 4096, NULL, 1, NULL, 0);
// Set MUX Register to AINO-AIN1 so it start doing the ADC conversion
adc.setChannel(2, 3);
}
void loop() {
/*
currentTime = millis();
elapsedTime = currentTime - lastTime;
if (elapsedTime >= 1000) {
sps = counter * 1.0 / elapsedTime * 1000;
lastTime = currentTime;
counter = 0;
Serial.print(" SR: ");
Serial.print(sps);
Serial.print(" V: ");
Serial.print(voltage, 15); // print with 2 decimals
Serial.print(" I: ");
Serial.println(current, 15); // print with 2 decimals
}
adc.waitDRDY(); // wait for DRDY to go low before changing multiplexer register
adc.setChannel(4, 5); // Set the MUX for differential between ch2 and 3
current = adc.readCurrentChannel() / 0.0125; // DOUT arriving here are from MUX AIN0 and AIN1
adc.waitDRDY();
adc.setChannel(2, 3);
voltage = adc.readCurrentChannel() * 2; //// DOUT arriving here are from MUX AIN2 and AIN3
//print the result.
counter++;
//Serial.print("C: ");
//Serial.print(counter);*/
}
//ADC einlesen
void adcTask(void *parameter) {
for (;;) {
//ADC Werte einlesen
adc.waitDRDY();
adc.setChannel(4, 5);
current_1[adcIndex] = adc.readCurrentChannel() / 0.0125;
adc.waitDRDY();
adc.setChannel(2, 3);
voltage_1[adcIndex] = adc.readCurrentChannel() * 2;
adcIndex++;
//SPS errechnen
counter++;
currentTime = millis();
elapsedTime = currentTime - lastTime;
if (elapsedTime >= 1000) {
sps = counter * (1.0 / elapsedTime) * 1000;
lastTime = currentTime;
counter = 0;
//Serial.println(sps);
}
//Wenn das Array voll ist wird es in ein neues Array gespeichert
if (adcIndex >= ADC_SAMPLES) {
if (check == false) {
memcpy(current_2, current_1, sizeof(current_1));
// Indizes zurücksetzen
adcIndex = 0;
check = true;
}
}
}
}
//Mittelwert bilden
void averageTask(void *parameter) {
for (;;) {
if (check == true) {
float sum = 0;
for (int i = 0; i < ADC_SAMPLES; i++) {
sum += current_2[i];
}
adcAverage = sum / ADC_SAMPLES;
Serial.println(adcAverage);
check = false;
}
vTaskDelay(1);
}
}
Serial Monitor Output:
15:18:37.526 -> Starting ADC
15:18:37.608 -> ADC Started
15:18:37.608 -> init succeed
//Reset Button gedrückt
15:18:38.374 -> Starting ADC
15:18:38.462 -> ADC Started
15:18:38.462 -> init succeed
15:18:38.587 -> -19.67
15:18:38.675 -> -17.80
15:18:38.761 -> -17.25
//Reset Button gedrückt
15:18:40.379 -> Starting ADC
15:18:40.464 -> ADC Started
15:18:40.464 -> init succeed
//Reset Button gedrückt
15:18:41.769 -> Starting ADC
15:18:41.840 -> ADC Started
15:18:41.840 -> init succeed
15:18:41.975 -> -18.97
15:18:42.065 -> -18.09
15:18:42.156 -> -17.71
15:18:42.276 -> -18.48
15:18:42.367 -> -18.04
15:18:42.456 -> -18.07
15:18:42.578 -> -18.25
15:18:42.667 -> -17.97