Added square, triangle and sawtooth convenience waveforms.

This commit is contained in:
Chris Esposito 2018-04-20 11:11:33 +10:00
parent d5754f4b2e
commit 183b38cfb4
5 changed files with 88 additions and 8 deletions

View File

@ -72,8 +72,8 @@ void MainWindow::on_pushButton_4_clicked()
void MainWindow::on_pushButton_5_clicked()
{
std::vector<double> *from_librador = (librador_get_analog_data(current_channel, 1, 375000, 0.1, 0));
double sampleRate = 375000;
std::vector<double> *from_librador = (librador_get_analog_data(current_channel, 1, sampleRate, 0.1, 0));
if(from_librador == NULL){
qDebug() << "from_librador NULL!";
return;
@ -90,14 +90,14 @@ void MainWindow::on_pushButton_5_clicked()
QVector<double> xaxis;
for (int i=0; i<yaxis.length(); i++){
xaxis.append(i);
xaxis.append(-i/sampleRate);
}
qDebug() << yaxis.length();
qDebug() << xaxis.length();
ui->widget->yAxis->setRange(ymin, ymax);
ui->widget->xAxis->setRange(0, yaxis.length());
ui->widget->xAxis->setRange(-yaxis.length()/sampleRate, 0);
ui->widget->graph(0)->setData(xaxis, yaxis);
ui->widget->replot();
@ -185,6 +185,15 @@ void MainWindow::signal_gen_convenience(int channel)
case 0:
librador_send_sin_wave(channel, frequency_hz, amplitude_v, offset_v);
break;
case 1:
librador_send_square_wave(channel, frequency_hz, amplitude_v, offset_v);
break;
case 2:
librador_send_triangle_wave(channel, frequency_hz, amplitude_v, offset_v);
break;
case 3:
librador_send_sawtooth_wave(channel, frequency_hz, amplitude_v, offset_v);
break;
default:
qDebug() << "INVALID WAVEFORM";
}

View File

@ -356,6 +356,21 @@
<string>Sin</string>
</property>
</item>
<item>
<property name="text">
<string>Square</string>
</property>
</item>
<item>
<property name="text">
<string>Triangle</string>
</property>
</item>
<item>
<property name="text">
<string>Sawtooth</string>
</property>
</item>
</widget>
</item>
</layout>

View File

@ -330,6 +330,9 @@ public:
comboBox_signal_gen_type = new QComboBox(centralWidget);
comboBox_signal_gen_type->addItem(QString());
comboBox_signal_gen_type->addItem(QString());
comboBox_signal_gen_type->addItem(QString());
comboBox_signal_gen_type->addItem(QString());
comboBox_signal_gen_type->setObjectName(QStringLiteral("comboBox_signal_gen_type"));
verticalLayout_4->addWidget(comboBox_signal_gen_type);
@ -429,6 +432,9 @@ public:
label_7->setText(QApplication::translate("MainWindow", "Offset(V)", nullptr));
label_5->setText(QApplication::translate("MainWindow", "Type", nullptr));
comboBox_signal_gen_type->setItemText(0, QApplication::translate("MainWindow", "Sin", nullptr));
comboBox_signal_gen_type->setItemText(1, QApplication::translate("MainWindow", "Square", nullptr));
comboBox_signal_gen_type->setItemText(2, QApplication::translate("MainWindow", "Triangle", nullptr));
comboBox_signal_gen_type->setItemText(3, QApplication::translate("MainWindow", "Sawtooth", nullptr));
pushButton_12->setText(QApplication::translate("MainWindow", "Send CH1", nullptr));
pushButton_13->setText(QApplication::translate("MainWindow", "Send CH2", nullptr));

View File

@ -142,9 +142,33 @@ int round_to_log2(double in){
return round(pow(2, floor(log2(in))));
}
int librador_send_sin_wave(int channel, double frequency_Hz, double amplitude_v, double offset_v){
CHECK_API_INITIALISED
unsigned char generator_sin(double x)
{
//Offset of 1 and divided by 2 shifts range from -1:1 to 0:1. We've got to return an unsigned char, after all!
return (unsigned char)round(255.0 * ((sin(x)+1)/2));
}
unsigned char generator_square(double x)
{
return (x > M_PI) ? 255 : 0;
}
unsigned char generator_sawtooth(double x)
{
return round(255.0 * (x/(2.0*M_PI)));
}
unsigned char generator_triangle(double x)
{
if(x <= M_PI){
return round(255.0 * (x/M_PI));
} else {
return round(255.0 * (1 -((x - M_PI)/M_PI)));
}
}
int send_convenience_waveform(int channel, double frequency_Hz, double amplitude_v, double offset_v, unsigned char (*sample_generator)(double))
{
if((amplitude_v + offset_v) > 9.6){
return -1;
//Voltage range too high
@ -161,6 +185,8 @@ int librador_send_sin_wave(int channel, double frequency_Hz, double amplitude_v,
int num_samples = fmin(1000000.0/frequency_Hz, 512);
//The maximum number of samples that Labrador's buffer holds is 512.
//The minimum time between samples is 1us. Using T=1/f, this gives a maximum sample number of 10^6/f.
num_samples = 2*(num_samples / 2);
//Square waves need an even number. Others don't care.
double usecs_between_samples = 1000000.0/((double)num_samples * frequency_Hz);
//Again, from T=1/f.
unsigned char* sampleBuffer = (unsigned char*)malloc(num_samples);
@ -170,8 +196,7 @@ int librador_send_sin_wave(int channel, double frequency_Hz, double amplitude_v,
for(i=0; i< num_samples; i++){
x_temp = (double)i * (2.0*M_PI/(double)num_samples);
//Generate points at interval 2*pi/num_samples.
sampleBuffer[i] = (unsigned char)round(255 * ((sin(x_temp)+1)/2));
//Offset of 1 and divided by 2 shifts range from -1:1 to 0:1. Helpful for unsigned.
sampleBuffer[i] = sample_generator(x_temp);
}
librador_update_signal_gen_settings(channel, sampleBuffer, num_samples, usecs_between_samples, amplitude_v, offset_v);
@ -179,3 +204,25 @@ int librador_send_sin_wave(int channel, double frequency_Hz, double amplitude_v,
free(sampleBuffer);
return 0;
}
int librador_send_sin_wave(int channel, double frequency_Hz, double amplitude_v, double offset_v){
CHECK_API_INITIALISED
return send_convenience_waveform(channel, frequency_Hz, amplitude_v, offset_v, generator_sin);
}
int librador_send_square_wave(int channel, double frequency_Hz, double amplitude_v, double offset_v){
CHECK_API_INITIALISED
return send_convenience_waveform(channel, frequency_Hz, amplitude_v, offset_v, generator_square);
}
int librador_send_triangle_wave(int channel, double frequency_Hz, double amplitude_v, double offset_v){
CHECK_API_INITIALISED
return send_convenience_waveform(channel, frequency_Hz, amplitude_v, offset_v, generator_triangle);
}
int librador_send_sawtooth_wave(int channel, double frequency_Hz, double amplitude_v, double offset_v){
CHECK_API_INITIALISED
return send_convenience_waveform(channel, frequency_Hz, amplitude_v, offset_v, generator_sawtooth);
}

View File

@ -13,6 +13,9 @@ int LIBRADORSHARED_EXPORT librador_avr_debug();
//a1
int LIBRADORSHARED_EXPORT librador_update_signal_gen_settings(int channel, unsigned char* sampleBuffer, int numSamples, double usecs_between_samples, double amplitude_v, double offset_v);
int LIBRADORSHARED_EXPORT librador_send_sin_wave(int channel, double frequency_Hz, double amplitude_v, double offset_v);
int LIBRADORSHARED_EXPORT librador_send_square_wave(int channel, double frequency_Hz, double amplitude_v, double offset_v);
int LIBRADORSHARED_EXPORT librador_send_sawtooth_wave(int channel, double frequency_Hz, double amplitude_v, double offset_v);
int LIBRADORSHARED_EXPORT librador_send_triangle_wave(int channel, double frequency_Hz, double amplitude_v, double offset_v);
//a2
////As above
//a3