-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
126 lines (117 loc) · 3.1 KB
/
main.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* @file main.cpp
* @author Mit Bailey (mitbailey99@gmail.com)
* @brief
* @version See Git tags for version information.
* @date 2021.09.02
*
* @copyright Copyright (c) 2021
*
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <signal.h>
#define lower_Az 40
#define upper_Az 110
#define lower_El 10
#define upper_El 35
volatile sig_atomic_t done = 0;
void sighandler(int sig)
{
done = 1;
}
// Generates AzEl data for a fake satellite pass.
int main()
{
signal(SIGINT, sighandler);
int AzEl[2] = {0};
bool upward = true;
bool iteratedLast = false;
int fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd < 3)
{
printf("Error opening /dev/ttyUSB0, check wiring and sudo\n");
return 0;
}
struct termios options[1];
tcgetattr(fd, options);
options->c_cflag = B2400 | CS8 | CLOCAL | CREAD;
options->c_iflag = IGNPAR;
options->c_oflag = 0;
options->c_lflag = 0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, options);
// FILE *fp = fopen("data.txt", "w");
char cmd[50];
ssize_t sz;
sz = sprintf(cmd, "PB %d\r", lower_Az);
if (write(fd, cmd, sz) < sz)
{
printf("Error writing azimuth command\n");
}
sleep(1);
sz = sprintf(cmd, "PA %d\r", lower_El);
if (write(fd, cmd, sz) < sz)
{
printf("Error writing elevation command\n");
}
sleep(30);
while (!done)
{
for (int az = lower_Az; (az <= upper_Az) && (!done); az++)
{
double idx = (az - lower_Az) * M_PI / 70; // 0 -- pi
int el = (upper_El - lower_El) * sin(idx) + lower_El;
printf("Az: %d deg, El: %d deg\n", az, el);
sz = snprintf(cmd, sizeof(cmd), "PB %d\r", az);
if (write(fd, cmd, sz) < sz)
{
printf("Error writing azimuth command\n");
}
sleep(1);
sz = snprintf(cmd, sizeof(cmd), "PA %d\r", el);
if (write(fd, cmd, sz) < sz)
{
printf("Error writing elevation command\n");
}
// if (!iteratedLast)
// fprintf(fp, "%d %d\n", az, el);
sleep(1);
}
sz = sprintf(cmd, "PB %d\r", lower_Az);
if (write(fd, cmd, sz) < sz)
{
printf("Error writing azimuth command\n");
}
sz = sprintf(cmd, "PA %d\r", lower_El);
if (write(fd, cmd, sz) < sz)
{
printf("Error writing elevation command\n");
}
// if (iteratedLast == false)
// {
// iteratedLast = true;
// fclose(fp);
// }
for (int i = 0; (i < 60) && (!done); i++)
sleep(1);
}
sleep(1);
sz = sprintf(cmd, "PB %d\r", 0);
if (write(fd, cmd, sz) < sz)
{
printf("Error writing azimuth command\n");
}
sz = sprintf(cmd, "PA %d\r", 90);
sleep(1);
if (write(fd, cmd, sz) < sz)
{
printf("Error writing elevation command\n");
}
close(fd);
return 0;
}