Serial simple acquisition#
[1]:
import os
import sys
sys.path.insert(0, os.path.realpath('../'))
[2]:
import LDAQ
[14]:
# Create serial acquisition source:
acq_serial = LDAQ.serial_communication.SerialAcquisitionSimple(port="COM3", baudrate=9600,
acquisition_name="arduino",
delimiter=",",
start_character= "",
end_character = "\r\n",
sample_rate=None,
channel_names=None)
# Here the arduino sends out characters over serial where a line would look like this:
# b"1,300,3.0\r\n
# Since no channels were defined, the acquisition will run a pretest to determine number of channels
# Addiitonally sample rate is estimated, since not specified
No channel names were given. Running pretest to determine number of channels and estimate sample rate...
Running pretest for 10.0 seconds...
[15]:
# create core object and add acquisition source:
ldaq = LDAQ.Core(acquisitions=[acq_serial])
# set trigger:
ldaq.set_trigger(
source="arduino",
channel=0,
level=100,
duration=5.0)
# run acquisition:
ldaq.run()
+--------+-------------------------------------------------+
| HOTKEY | DESCRIPTION |
+--------+-------------------------------------------------+
| s | Start the measurement manually (ignore trigger) |
+--------+-------------------------------------------------+
| q | Stop the measurement |
+--------+-------------------------------------------------+
Waiting for trigger...
triggered.
Recording...Measurement finished.
sys:1: ResourceWarning: Unclosed socket <zmq.Socket(zmq.PUSH) at 0x19e55db7520>
ResourceWarning: Enable tracemalloc to get the object allocation traceback
[16]:
# Retrieve the measurement data:
measurement = ldaq.get_measurement_dict()
measurement["arduino"]["data"]
[16]:
array([[ 990., 1007., 787.],
[ 997., 1001., 741.],
[1004., 991., 687.],
[1008., 980., 636.],
[1010., 966., 580.],
[1011., 950., 527.],
[1011., 929., 468.],
[1009., 908., 415.],
[1006., 886., 363.],
[1000., 858., 307.],
[ 994., 832., 260.],
[ 987., 804., 215.],
[ 978., 776., 176.],
[ 968., 745., 139.],
[ 956., 713., 105.],
[ 944., 682., 78.],
[ 931., 650., 55.],
[ 917., 618., 37.],
[ 903., 587., 24.],
[ 887., 553., 15.],
[ 871., 522., 12.],
[ 854., 491., 12.],
[ 834., 455., 19.],
[ 815., 424., 29.],
[ 796., 393., 43.],
[ 775., 361., 63.],
[ 754., 329., 88.],
[ 731., 299., 116.],
[ 709., 269., 149.],
[ 685., 240., 186.],
[ 659., 209., 230.],
[ 634., 183., 273.],
[ 610., 159., 319.],
[ 584., 134., 369.],
[ 557., 112., 421.],
[ 530., 92., 474.],
[ 507., 75., 521.],
[ 483., 61., 568.],
[ 457., 47., 621.],
[ 433., 36., 666.],
[ 410., 27., 710.],
[ 386., 20., 755.],
[ 361., 15., 798.],
[ 338., 12., 837.],
[ 316., 12., 872.],
[ 294., 13., 903.],
[ 272., 17., 932.],
[ 250., 22., 957.],
[ 230., 30., 976.],
[ 210., 40., 993.],
[ 192., 51., 1003.],
[ 170., 67., 1010.],
[ 152., 84., 1011.],
[ 135., 102., 1007.],
[ 117., 125., 997.],
[ 102., 148., 981.],
[ 87., 174., 960.],
[ 75., 198., 937.],
[ 64., 223., 910.],
[ 53., 252., 876.],
[ 44., 280., 842.],
[ 36., 308., 805.],
[ 29., 339., 763.],
[ 23., 371., 719.],
[ 18., 401., 675.],
[ 15., 434., 627.],
[ 13., 465., 580.],
[ 12., 499., 530.],
[ 12., 532., 480.],
[ 13., 564., 433.],
[ 16., 597., 384.],
[ 19., 628., 339.],
[ 24., 660., 293.],
[ 30., 692., 249.],
[ 37., 721., 210.],
[ 45., 749., 174.],
[ 56., 779., 136.],
[ 66., 805., 107.],
[ 78., 832., 80.],
[ 89., 854., 59.],
[ 102., 875., 42.],
[ 117., 898., 26.],
[ 132., 917., 17.],
[ 148., 935., 12.],
[ 166., 952., 12.],
[ 184., 967., 17.],
[ 203., 979., 26.],
[ 223., 990., 40.],
[ 242., 998., 58.],
[ 265., 1005., 83.],
[ 287., 1009., 111.],
[ 311., 1011., 145.],
[ 338., 1011., 186.],
[ 364., 1008., 230.],
[ 392., 1001., 279.],
[ 418., 993., 327.],
[ 443., 983., 375.],
[ 471., 969., 430.],
[ 496., 955., 480.],
[ 522., 937., 533.],
[ 548., 918., 584.],
[ 574., 897., 636.],
[ 601., 873., 687.],
[ 625., 849., 733.],
[ 649., 824., 777.],
[ 676., 793., 823.],
[ 700., 765., 861.],
[ 724., 734., 897.],
[ 747., 703., 927.],
[ 770., 670., 954.],
[ 793., 636., 976.],
[ 813., 603., 993.],
[ 834., 568., 1004.],
[ 855., 530., 1011.],
[ 874., 495., 1011.],
[ 893., 457., 1005.],
[ 909., 422., 993.],
[ 926., 385., 975.],
[ 939., 353., 954.],
[ 952., 322., 929.],
[ 965., 287., 895.],
[ 975., 258., 861.],
[ 984., 228., 820.],
[ 992., 201., 779.],
[ 998., 175., 736.],
[1004., 148., 684.],
[1008., 125., 633.],
[1010., 102., 577.],
[1011., 82., 521.],
[1011., 66., 471.],
[1009., 51., 418.],
[1006., 39., 369.],
[1001., 28., 316.],
[ 996., 20., 271.],
[ 989., 15., 230.],
[ 981., 12., 190.],
[ 973., 12., 156.],
[ 963., 13., 122.],
[ 951., 16., 93.],
[ 940., 21., 70.],
[ 929., 28., 51.],
[ 914., 37., 34.],
[ 901., 47., 23.],
[ 886., 60., 15.],
[ 871., 73., 12.],
[ 854., 89., 12.],
[ 836., 107., 18.],
[ 818., 126., 27.],
[ 799., 147., 41.],
[ 778., 171., 60.],
[ 756., 196., 84.],
[ 736., 221., 111.],
[ 712., 251., 145.],
[ 688., 280., 181.],
[ 662., 314., 225.],
[ 637., 345., 268.],
[ 613., 377., 313.],
[ 587., 412., 363.],
[ 560., 447., 415.],
[ 535., 480., 464.],
[ 508., 516., 518.],
[ 483., 549., 568.],
[ 455., 587., 624.],
[ 430., 620., 672.],
[ 405., 652., 719.],
[ 380., 686., 766.],
[ 354., 719., 810.],
[ 329., 751., 851.],
[ 306., 779., 887.],
[ 283., 807., 918.],
[ 258., 837., 948.],
[ 237., 861., 970.],
[ 215., 886., 989.],
[ 195., 907., 1001.],
[ 174., 929., 1010.],
[ 154., 949., 1011.],
[ 135., 965., 1007.],
[ 117., 979., 997.],
[ 102., 991., 981.],
[ 88., 999., 961.],
[ 75., 1006., 937.],
[ 63., 1010., 908.],
[ 51., 1011., 872.],
[ 42., 1011., 835.],
[ 34., 1008., 795.],
[ 27., 1002., 750.],
[ 21., 994., 701.],
[ 17., 985., 657.],
[ 14., 973., 608.],
[ 12., 960., 562.],
[ 12., 943., 508.],
[ 12., 927., 461.],
[ 14., 908., 415.],
[ 17., 887., 366.],
[ 21., 864., 319.],
[ 26., 841., 276.],
[ 33., 815., 233.],
[ 41., 790., 195.],
[ 49., 763., 160.],
[ 60., 732., 124.],
[ 70., 703., 96.],
[ 82., 674., 72.],
[ 94., 646., 53.],
[ 107., 615., 36.],
[ 122., 582., 23.],
[ 137., 551., 15.],
[ 154., 520., 12.],
[ 173., 484., 13.],
[ 190., 453., 19.],
[ 209., 422., 30.],
[ 229., 389., 45.],
[ 249., 359., 65.],
[ 272., 325., 91.],
[ 293., 297., 118.],
[ 316., 267., 151.],
[ 341., 237., 190.],
[ 366., 208., 233.],
[ 390., 182., 276.],
[ 416., 156., 325.],
[ 441., 133., 372.],
[ 469., 109., 427.],
[ 494., 90., 477.],
[ 518., 74., 524.],
[ 543., 59., 574.],
[ 568., 46., 624.],
[ 591., 35., 669.],
[ 616., 26., 716.],
[ 639., 20., 758.],
[ 665., 14., 803.],
[ 687., 12., 840.],
[ 709., 12., 874.],
[ 731., 13., 907.],
[ 754., 17., 935.],
[ 774., 23., 958.],
[ 795., 31., 979.],
[ 814., 41., 993.],
[ 833., 52., 1004.],
[ 854., 68., 1011.],
[ 872., 85., 1011.],
[ 890., 105., 1006.],
[ 907., 126., 996.],
[ 922., 150., 980.],
[ 937., 175., 958.],
[ 950., 201., 934.],
[ 963., 231., 901.],
[ 973., 260., 867.],
[ 982., 289., 830.],
[ 990., 322., 787.],
[ 997., 353., 744.],
[1003., 389., 693.],
[1007., 424., 642.],
[1010., 459., 590.],
[1011., 499., 530.],
[1011., 535., 477.],
[1009., 572., 421.],
[1006., 607., 369.],
[1001., 644., 316.],
[ 995., 680., 265.],
[ 988., 711., 223.],
[ 979., 743., 181.],
[ 969., 774., 143.],
[ 958., 802., 111.],
[ 946., 830., 81.],
[ 934., 854., 59.],
[ 919., 879., 39.],
[ 905., 899., 26.],
[ 890., 918., 17.],
[ 873., 937., 12.],
[ 855., 954., 12.],
[ 837., 967., 17.],
[ 818., 980., 27.],
[ 799., 990., 41.],
[ 779., 998., 59.]])
C code uploaded to the Arduino:#
#define PI 3.1415926535897932384626433832795
const int amplitude = 500; // Amplitude of the sine wave
const int offset = 512; // Offset to shift the sine wave above 0
// Periods of the sine waves in milliseconds
const unsigned long period1 = 2000;
const unsigned long period2 = 1500;
const unsigned long period3 = 1000;
void setup() {
Serial.begin(9600); // Starts the serial communication
}
void loop() {
unsigned long time = millis(); // Get the current time
// Calculate the output values for the three sine waves
int outputValue1 = calculateOutputValue(time, period1);
int outputValue2 = calculateOutputValue(time, period2);
int outputValue3 = calculateOutputValue(time, period3);
// Send the output values to the serial port
Serial.print(outputValue1);
Serial.print(",");
Serial.print(outputValue2);
Serial.print(",");
Serial.println((double)outputValue3);
delay(1); // Short delay so the plotter can keep up
}
int calculateOutputValue(unsigned long time, unsigned long period) {
double fraction = (time % period) / (double)period; // Calculate the fraction of the period that has passed
double angle = fraction * 2 * PI; // Convert the fraction to an angle in radians
double sineValue = sin(angle); // Calculate the sine of the angle
return offset + amplitude * sineValue; // Scale and shift the sine value
}