File: nexstar_pec.c

package info (click to toggle)
libnexstar 0.15-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, trixie
  • size: 1,484 kB
  • ctags: 308
  • sloc: sh: 11,250; ansic: 1,552; makefile: 24
file content (226 lines) | stat: -rw-r--r-- 4,579 bytes parent folder | download | duplicates (2)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**************************************************************
	Celestron NexStar compatible telescope control library

	(C)2013-2016 by Rumen G.Bogdanovski
***************************************************************/
#include <math.h>
#include <nexstar.h>
#include <nexstar_pec.h>


int pec_index_found(int dev) {
	char res[2];

	REQUIRE_VER(VER_AUX);
	REQUIRE_VENDOR(VNDR_CELESTRON);

	if (tc_pass_through_cmd(dev, 1, _TC_AXIS_RA_AZM, 0x18, 0, 0, 0, 1, res) < 0) {
		return RC_FAILED;
	}

	if (res[0] == 0) return 0;
	else return 1;
}


int pec_seek_index(int dev) {
	char res;

	REQUIRE_VER(VER_AUX);
	REQUIRE_VENDOR(VNDR_CELESTRON);

	if (tc_pass_through_cmd(dev, 1, _TC_AXIS_RA_AZM, 0x19, 0, 0, 0, 0, &res) < 0) {
		return RC_FAILED;
	}

	return RC_OK;
}


int pec_record(int dev, char action) {
	char res, cmd_id;

	REQUIRE_VER(VER_AUX);
	REQUIRE_VENDOR(VNDR_CELESTRON);

	if (action == PEC_START) {
		cmd_id = 0x0c;  /* Start PEC record */
	} else if (action == PEC_STOP) {
		cmd_id = 0x16;  /* Stop PEC record */
	} else {
		return RC_PARAMS;
	}

	if (tc_pass_through_cmd(dev, 1, _TC_AXIS_RA_AZM, cmd_id, 0, 0, 0, 0, &res) < 0) {
		return RC_FAILED;
	}

	return RC_OK;
}


int pec_record_complete(int dev) {
	char res[2];

	REQUIRE_VER(VER_AUX);
	REQUIRE_VENDOR(VNDR_CELESTRON);

	if (tc_pass_through_cmd(dev, 1, _TC_AXIS_RA_AZM, 0x15, 0, 0, 0, 1, res) < 0) {
		return RC_FAILED;
	}

	if (res[0] == 0) return 0;
	else return 1;
}


int pec_playback(int dev, char action) {
	char res;

	REQUIRE_VER(VER_AUX);
	REQUIRE_VENDOR(VNDR_CELESTRON);

	if ((action != PEC_START) && (action != PEC_STOP)) {
		return RC_PARAMS;
	}

	if (tc_pass_through_cmd(dev, 2, _TC_AXIS_RA_AZM, 0x0d, action, 0, 0, 0, &res) < 0) {
		return RC_FAILED;
	}

	return RC_OK;
}


int pec_get_playback_index(int dev) {
	char res[2];

	REQUIRE_VER(VER_AUX);
	REQUIRE_VENDOR(VNDR_CELESTRON);

	if (tc_pass_through_cmd(dev, 1, _TC_AXIS_RA_AZM, 0x0e, 0, 0, 0, 1, res) < 0) {
		return RC_FAILED;
	}

	return (unsigned char) res[0];
}


int pec_get_data_len(int dev) {
	char res[2];

	REQUIRE_VER(VER_AUX);
	REQUIRE_VENDOR(VNDR_CELESTRON);

	if (tc_pass_through_cmd(dev, 2, _TC_AXIS_RA_AZM, 0x30, 0x3f, 0, 0, 1, res) < 0) {
		return RC_FAILED;
	}

	return (unsigned char) res[0];
}


int _pec_set_data(int dev, unsigned char index, char data) {
	char res;

	REQUIRE_VER(VER_AUX);
	REQUIRE_VENDOR(VNDR_CELESTRON);

	if (tc_pass_through_cmd(dev, 4, _TC_AXIS_RA_AZM, 0x31, 0x40+index, data, 0, 0, &res) < 0) {
		return RC_FAILED;
	}

	return RC_OK;
}


/* on success the returned value should be threated as a signed char */
int _pec_get_data(int dev, unsigned char index) {
	char res[2];

	REQUIRE_VER(VER_AUX);
	REQUIRE_VENDOR(VNDR_CELESTRON);

	if (tc_pass_through_cmd(dev, 2, _TC_AXIS_RA_AZM, 0x30, 0x40+index, 0, 0, 1, res) < 0) {
		return RC_FAILED;
	}

	return (unsigned char) res[0];
}


int pec_set_data(int dev, float *data, int len) {
	int pec_len, i, rdiff;
	float diff, current = 0;

	pec_len = pec_get_data_len(dev);
	if (pec_len < 0) {
		return RC_FAILED;
	}

	if (pec_len != len) {
		return RC_PARAMS;
	}

	for( i=0; i<pec_len; i++) {
		diff = data[i] - current;
		/* I have no idea why the values are different for positive and negative numbers
		   I thought the coefficient should be 0.0772 arcsec/unit as the resolution of
		   24bit number for 360 degrees. I tried to mach as best as I could the values
		   returned by Celestron's PECTool and I came up with this numbers... */
		if (diff < 0) {
			rdiff = (int)roundl(data[i] / 0.0845) - (int)roundl(current / 0.0845);
		} else {
			rdiff = (int)roundl(data[i] / 0.0774) - (int)roundl(current / 0.0774);
		}

		if ((rdiff > 127) || (rdiff < -127)) {
			return RC_DATA;
		}

		if (_pec_set_data(dev, i, (char)rdiff) < 0) {
			return RC_FAILED;
		}
		current = data[i];
	}

	if (pec_record(dev, PEC_STOP) < 0) {
		return RC_FAILED;
	}
	return RC_OK;
}


int pec_get_data(int dev, float *data, const int max_len) {
	int pec_len, i, diff;
	float current = 0;
	char rdata;

	pec_len = pec_get_data_len(dev);
	if (pec_len < 0) {
		return RC_FAILED;
	}

	if (pec_len > max_len) {
		return RC_PARAMS;
	}

	for (i = 0; i < pec_len; i++) {
		diff = _pec_get_data(dev, i);
		if (diff < 0) {
			return RC_FAILED;
		}
		/* we need thedata as a signed char */
		rdata = (char)diff;

		/* I have no idea why the values are different for positive and negative numbers.
		   Pease se the note in pec_set_data()! */
		if (rdata > 0) {
			current += rdata * 0.0774;
		} else {
			current += rdata * 0.0845;
		}
		data[i] = current;
	}
	return pec_len;
}