giuliomoro
Ah ok no worries I understand what you mean. I worked out how to get the onboard SDK installed and running by using a different installation approach. The drone onboard SDK now runs on Bela and when I run the telemetry sample file, the data is printed to the terminal.
I’m now working on incorporating the serial communication to the current code that I have. At present the programs are run within the onboard SDK (installed on Bela). I would like this data to be received by Bela within the audio recording code that I’ve written. Rather than only within the SDK on Bela.
So that the program I’m running is receiving the serial data from the drone and essentially logging it. It also gives me options to use the data later. I have modified the current code to incorporate serial communication but wasn't too sure what would go in the file1.log();
. I assume it is possible to log the drone data coming in from the serial connection? In this case it would be logging the drone telemetry and motor speed data.
Also out of curiosity, how are the values determined within the brackets in setFormat: file1.setFormat("%f %f %f %f %f %f %f %f\n");
?
Here is my current code with the serial communication and part of the logging added:
#include <Bela.h>
#include <libraries/Pipe/Pipe.h>
#include <libraries/Serial/Serial.h>
#include <libraries/sndfile/sndfile.h>
#include <libraries/WriteFile/WriteFile.h>
#include <cmath>
const char* path = "/mnt/usb/audioCh.wav";
SNDFILE * outfile2;
char originalFilename[] = "/mnt/usb/audioCh.wav";
char* uniqueFilename = WriteFile::generateUniqueFilename(originalFilename);
WriteFile file1;
//Audio
AuxiliaryTask gFillBufferTask;
SNDFILE * outfile;
unsigned int gAudioFrames;
unsigned int gAudioInChannels;
float gAudioSampleRate;
Pipe gPipe;
Serial gSerial;
//Open files audio
void openFile() {
SF_INFO sfinfo;
sfinfo.channels = gAudioInChannels;
sfinfo.samplerate = gAudioSampleRate;
sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
//outfile = sf_open(path, SFM_WRITE, &sfinfo);
outfile2 = sf_open(uniqueFilename, SFM_WRITE, &sfinfo);
}
//close audio files
void closeFile() {
sf_write_sync(outfile2);
sf_close(outfile2);
//sf_write_sync(outfile);
//sf_close(outfile);
printf(".wav file written and closed\n");
}
void writeBuffer(void*) {
unsigned int numItems = gAudioFrames * gAudioInChannels;
float buf[numItems];
int ret;
while((ret = gPipe.readNonRt(buf, numItems) ) > 0)
{
sf_write_float(outfile2, &buf[0], ret);
}
}
void startSerial(void* arg)
{
unsigned int maxLen = 256;
char serialBuffer[maxLen];
// read from the serial port with a timeout of 100ms
int ret2 = gSerial.read(serialBuffer, maxLen, 100);
if (ret2 > 0) {
printf("Received: %.*s\n", ret2, serialBuffer);
} else {
printf("UART4 not available\n");
}
}
bool setup(BelaContext* context, void* arg)
{
gSerial.setup ("/dev/ttyS4", 230400);
AuxiliaryTask serialCommsTask = Bela_createAuxiliaryTask(startSerial, 0, "serial-thread", NULL);
Bela_scheduleAuxiliaryTask(serialCommsTask);
file1.setup("droneData.txt"); //set the file name to write to
file1.setFileType(kText);
//file1.setFileType(kBinary); //this file type reduces the size of the file
file1.setFormat("%f %f %f %f %f %f %f %f\n");
//setup audio frames and channels
gAudioSampleRate = context->audioSampleRate;
gAudioFrames = context->audioFrames;
gAudioInChannels = context->audioInChannels;
gPipe.setup("sndfile-write", 65536, false, false);
openFile();
if((gFillBufferTask = Bela_createAuxiliaryTask(&writeBuffer, 90, "writeBuffer")) == 0) {
return false;
}
return true;
}
void render(BelaContext* context, void* arg)
{
// context->audioIn is the float* that points to all the input samples, stored as interleaved channels)
// audioIn is an array of 4 frames * 2 channels = 8 audio input samples.
gPipe.writeRt(context->audioIn, context->audioFrames * context->audioInChannels);
// analogIn is an array of 2 frames * 8 channels = 16 analog input samples.
// (2 frames because analog I/O runs at half the sample rate of audio I/O.)
//gPipe.writeRt(context->analogIn, context->analogFrames * context->analogInChannels);
Bela_scheduleAuxiliaryTask(gFillBufferTask);
//file1.log();
}
void cleanup(BelaContext* context, void* arg)
{
closeFile();
free(uniqueFilename);
}