Hi,
I am trying to use my mbed (NUCLEO F767I) to monitor some sensor data in response to air pressure controlled by an analog signal. I am doing this with two tickers - one firing every second to step up the air pressure by increasing the AnalogOut value and the other firing every 20 microseconds to poll the sensors and read the analog output and add them to a queue. My main then loops a printf to send the next set of data to the PC and pop them out of the queue.
The problem I am having is that with more than one variable in the printf line, the AnalogOut value reaches a ceiling and then resets back to 0. This ceiling decreases as I make the output lines longer, so I can reach 3.1V output with two values being sent, but no more than 0.25 with 4 variables being read out (time, pressure and data from two sensors). To make this even more confusing, the being sent to the DAC behaves correctly if I change its declaration from AnalogOut to a simple float.
My code (such as it is) is below:
#include <mbed.h>
#include <mbed_events.h>
#include <queue>
void readADC();
double voltage(AnalogIn &adc);
double acceleration(AnalogIn &adc);
void incrementPressure();
Ticker readVolt1;
Ticker setPressure;
Timer clocky;
AnalogIn z1(PA_3);
AnalogIn z2(PC_0);
AnalogIn z3(PC_3);
AnalogIn z4(PF_3);
AnalogIn z5(PF_5);
AnalogIn z6(PF_10);
AnalogOut pressure(PA_5);
std::queue<int> timestamps;
std::queue<double> z1data;
std::queue<double> z2data;
std::queue<double> z3data;
std::queue<double> z4data;
std::queue<double> pressures;
Serial pc(USBTX, USBRX);
DigitalOut myled(LED1);
int main() {
//Set up serial connection
pc.baud(921600);
pc.printf("Accelorometer values follow:\n\n");
//Set up tickers and the clock
readVolt1.attach(&readADC, 0.0002);
setPressure.attach(&incrementPressure, 1);
clocky.start();
//Start pressure at 0
pressure = 0.0f;
while(1) {
//If there is data queued, send it to the PC and clear it out of the queue
if(!timestamps.empty()){
pc.printf("%i, %.3f, %.6f, %.6f, %.6f, %.6f\n", timestamps.front(), pressures.front(), z1data.front(), z2data.front(), z3data.front(), z4data.front());
z1data.pop();
z2data.pop();
z3data.pop();
z4data.pop();
pressures.pop();
timestamps.pop();
}
//Don't run forever
if (clocky.read() > 120){
readVolt1.detach();
setPressure.detach();
pc.printf("Program ended");
}
}
}
//Read data from sensors and timestamp it
void readADC(){
int readStartTime = clocky.read_us();
z1data.push(voltage(z1));
z2data.push(voltage(z2));
z3data.push(voltage(z3));
z4data.push(voltage(z4));
//pressures.push(pressure.read());
pressures.push(pressure.read()*3.3*(-10));
int readEndTime = clocky.read_us();
int measurementTime = readStartTime + (( readEndTime - readStartTime ) / 2 );
timestamps.push(measurementTime);
}
//Helper function to get voltage from ADC
double voltage(AnalogIn &adc){
return (adc.read() * 3.0);
}
//Helper function to get acceleration from ADC (not finished)
double acceleration(AnalogIn &adc){
return (voltage(adc) * (10/3));
}
//increase signal to pressure regulator
void incrementPressure(){
pc.printf("pressure change\n");
if (pressure.read() <= 0.95){
pressure = (pressure + 0.02);
}
else{
pressure = (pressure - 0.02);
}
return;
}
Is there any reason printf() would be interfering with AnalogOut in this way?