Skip to content
Snippets Groups Projects
Commit a03b358e authored by MuijzerF's avatar MuijzerF
Browse files

fixed bugs in sample rate command

parent 25730e0a
No related branches found
No related tags found
No related merge requests found
No preview for this file type
Source diff could not be displayed: it is too large. Options to address this: view the blob.
......@@ -9,20 +9,22 @@ using namespace helpers; // contains the verbose levels from Instr
#include <BufferSupport.h> //data buffers
// #include <SerialSupport.h> //Serial object
const float versionNumber = 1.1; // version displayed on boot
const float versionNumber = 1.2; // version displayed on boot
bool outputFloat = false; // true = ADC converted to float, false = ADC in 16 bit integer
const uint16_t serialTimeOut = 1000; // timeout in ms
const long serialBaudrate = 115200;
const byte numChars = 32; //maximum length of messages parsed on receive.
static uint16_t logFreqSamples = 200; // give samplecounter and summary every n samples
uint8_t downSampler = 16; // average n number of samples before storing. 1 = no downsampling, 2 = average two samples
const uint8_t maxDownSampler = 128; // max number of samples to average
const uint16_t minSampleRate = 10; // Lowest Acceptable Sample Rate
const uint16_t maxSampleRate = 8096; // Highest Acceptable Sample Rate
const uint8_t maxDownSampler = 200; // max number of samples to average
const uint16_t minSampleRate = 1; // Lowest Acceptable Sample Rate (Hz)
const uint32_t maxSampleRate = 100000; // Highest Acceptable Sample Rate (Hz)
const uint8_t channelCountSize = 1; // always fixed at 1 byte
const unsigned char channelCount[channelCountSize] = {0x01}; // number of active channels send to the plot, for now this is ADC. Should be 0x02 if Batt is also send.
const int spiClockDivider = 42; // ADC SPI speed
SPISettings spiSettingsA(4000000, MSBFIRST, SPI_MODE1);
const int chipSelectADCPin = 10; // ADC SPI
const u_int8_t MOSIpin = 11; // ADC SPI //not used, only one way communication
const u_int8_t MISOpin = 12; // ADC SPI
......@@ -191,15 +193,22 @@ void aquisitionTask_Callback()
// low. The data bits are then clocked, MSB first, by subsequent DCLOCK falling edges. The data is valid on both DCLOCK
// edges. Although the rising edge can be used to capture the data, a digital host also using the DCLOCK falling edge allows a
// faster reading rate, provided it has an acceptable hold time.
SPI.beginTransaction(spiSettingsA);
digitalWrite(chipSelectADCPin, LOW); // select the chip
//delayMicroseconds(2);
byte adcByteHigh = SPI.transfer(0x00); // transfer 0x00 to the device, store byte received in adcByteHigh
adcByteHigh &= 0b00000011; // corrects for 18 bit
//delayMicroseconds(2);
byte adcByteMid = SPI.transfer(0x00); // transfer 0x00 to the device, store byte received in adcByteMid
//delayMicroseconds(2);
byte adcByteLow = SPI.transfer(0x00); // transfer 0x00 to the device, store byte received in adcByteLow
//delayMicroseconds(2);
digitalWrite(chipSelectADCPin, HIGH); // deselect the chip
SPI.endTransaction();
// combine the two parts into one 18-bit number:
ADCresult = ((adcByteHigh << 14) | (adcByteMid << 6) | (adcByteLow >> 2));
//ADCresult = ((adcByteHigh << 14) | (adcByteMid << 6) | (adcByteLow >> 2));
//ADCresult = ((adcByteHigh << 16) | (adcByteMid << 8) | (adcByteLow >>0));
ADCresult = (adcByteHigh & 3) << 14 | adcByteMid << 6 | adcByteLow >> 2; // & 3 part masks the two highest bits of adcByteHigh which should be 0 anyways.
ADCresultArray[downSampleCounter] = ADCresult;
//downSampler = 4 gives: 0 1 2 3
......@@ -226,7 +235,7 @@ void aquisitionTask_Callback()
// convert current time to bytes (32 bit) //TODO: this is unsigned, uScope expects singed int for time (why!?)
// convert current time to bytes (32 bit) //TODO: this is unsigned, uScope expects singed int for time (why!?)
bufferMillis.buffer[3] = (uint8_t)(currentTime) & 0x80; // LSB //0x80 instead of 0xFF seems to fix the negative time in uScope
bufferMillis.buffer[2] = (uint8_t)(currentTime >> 8) & 0xFF;
bufferMillis.buffer[1] = (uint8_t)(currentTime >> 16) & 0xFF;
......@@ -242,14 +251,26 @@ void aquisitionTask_Callback()
//average the ADC readings according to downsample amount
ADCresult = 0; //clear ADCresult, which is also used above
//calculate average of the ADC readings
for (int i = 0; i < downSampler; i++)
{
ADCresult = ADCresult + ADCresultArray[i];
ADCresult += ADCresultArray[i];
}
ADCresult = ADCresult / downSampler; //compensate for downsampling
if(outputFloat){
// convert to float voltage
ADCvolts = (5.0 * ((float)ADCresult / 65535)) / downSampler ; //convert to float and compensate for downsample
ADCvolts = ADCvolts - 2.5; //offset to compensate for the single supply ADC
ADCvolts = (5.0 * ((float)ADCresult / 65535)) ; //convert to volts and convert tp float
ADCvolts = ADCvolts - 2.5; //offset to compensate for the single supply ADC
}
else
{
ADCvolts = (float)ADCresult ; //cast to float without conversion into volts
}
// convert to bytes
byte *bADCvolts = (byte *)&ADCvolts;
......@@ -367,7 +388,7 @@ void communicationTask_Callback()
helpers::printToLog("Stop command received: disabled aquisitionTask (stop sampling).", verboseLevels::info);
}
}
else if (strncmp(receivedChars, "SetSampleRate", 13) == 0) // command starting with "SetSampleRate", should always be followed by 4 integers, for example "SetSampleRate1000" for 1000 Hz
else if (strncmp(receivedChars, "SetSampleRate", 13) == 0) // command starting with "SetSampleRate", should always be followed by 5 integers, for example "SetSampleRate01000" for 1000 Hz
{
if (aquisitionTask.isEnabled())
{
......@@ -375,9 +396,9 @@ void communicationTask_Callback()
}
else
{
char argument[5]; // stores the value received from the host in ASCCI text
memcpy(argument, &receivedChars[13], 4); // copy characters from full input command
argument[4] = '\0'; // termination character
char argument[6]; // stores the value received from the host in ASCCI text
memcpy(argument, &receivedChars[13], 5); // copy characters from full input command
argument[5] = '\0'; // termination character
int newSampleRate = atoi(argument);
helpers::printToLog("Received SetSampleRate Command: " + String(newSampleRate), verboseLevels::info);
......@@ -391,7 +412,7 @@ void communicationTask_Callback()
}
else
{
aquisitionTaskDuration = 1000000 / newSampleRate; // µs per cycle //for aquisition of sensor data
aquisitionTaskDuration = round(1000000 / newSampleRate); // µs per cycle //for aquisition of sensor data
aquisitionTask.setInterval(aquisitionTaskDuration);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment