forked from scanse/sweep-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.c
87 lines (68 loc) · 2.72 KB
/
example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Make use of the CMake build system or compile manually, e.g. with:
// gcc -std=c99 example.c -lsweep
#include <assert.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sweep/sweep.h>
// Utility functions for error handling: we simply shut down here; you should do a better job
static void die(sweep_error_s error) {
assert(error);
fprintf(stderr, "Error: %s\n", sweep_error_message(error));
sweep_error_destruct(error);
exit(EXIT_FAILURE);
}
static void check(sweep_error_s error) {
if (error)
die(error);
}
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stdout, "Usage: ./example-c device\n");
return EXIT_FAILURE;
}
// Makes sure the installed library is compatible with the interface
assert(sweep_is_abi_compatible());
// Grab the port name from the input argument
const char* port = argv[1];
// All functions which can potentially fail write into an error object
sweep_error_s error = NULL;
// Create a Sweep device from the specified USB serial port; there is a second constructor for advanced usage
sweep_device_s sweep = sweep_device_construct_simple(port, &error);
check(error);
// The Sweep's rotating speed in Hz
int32_t speed = sweep_device_get_motor_speed(sweep, &error);
check(error);
fprintf(stdout, "Motor Speed Setting: %" PRId32 " Hz\n", speed);
// The Sweep's sample rate in Hz
int32_t rate = sweep_device_get_sample_rate(sweep, &error);
check(error);
fprintf(stdout, "Sample Rate Setting: %" PRId32 " Hz\n", rate);
// Capture scans
fprintf(stdout, "Beginning data acquisition as soon as motor speed stabilizes...\n");
sweep_device_start_scanning(sweep, &error);
check(error);
// Let's do 10 full 360 degree scans
for (int32_t num_scans = 0; num_scans < 10; ++num_scans) {
// This blocks until a full 360 degree scan is available
sweep_scan_s scan = sweep_device_get_scan(sweep, &error);
check(error);
// For each sample in a full 360 degree scan print angle and distance.
// In case you're doing expensive work here consider using a decoupled producer / consumer pattern.
for (int32_t n = 0; n < sweep_scan_get_number_of_samples(scan); ++n) {
int32_t angle = sweep_scan_get_angle(scan, n);
int32_t distance = sweep_scan_get_distance(scan, n);
int32_t signal = sweep_scan_get_signal_strength(scan, n);
fprintf(stdout, "Angle %" PRId32 ", Distance %" PRId32 ", Signal Strength: %" PRId32 "\n", angle, distance, signal);
}
// Cleanup scan response
sweep_scan_destruct(scan);
}
// Stop capturing scans
sweep_device_stop_scanning(sweep, &error);
check(error);
// Shut down and cleanup Sweep device
sweep_device_destruct(sweep);
return EXIT_SUCCESS;
}