winrotor_ctrl.c 10.1 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007
	Copyright Stichting C.A. Muller Radioastronomiestation, 2007

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>

#include "controller_block.h"
#include "controller_trace.h"
#include "controller_setpoint_command.h"
#include "controller_sample.h"
#include "block_subtract.h"
#include "block_setpoint_generator.h"
#include "block_quantize.h"
#include "block_filter_lp.h"
#include "winrotor-usb.h"

#define FREQ 20.0

#define PI 3.14159265358979323846

struct block {
	char *name;
	struct controller_block *(*create)(char *);
};

struct param_misc {
	char *blockname;
	char *paramname;
	void *value;
};

struct block_link {
	char *outblock;
	char *outterm;
	char *inblock;
	char *interm;
	int chain;
};

static struct block controller_blocks[] = {
	{ "winrotor",                         block_winrotor_usb_create       },

	{ "azimuth_spg",                      block_setpoint_generator_create },
	{ "azimuth_setpoint_error",           block_subtract_create           },
	{ "azimuth_error",                    block_subtract_create           },
	{ "azimuth_quantize",                 block_quantize_create           },
	{ "azimuth_lp",                       block_filter_lp_create          },
	
	{ "elevation_spg",                    block_setpoint_generator_create },
	{ "elevation_setpoint_error",         block_subtract_create           },
	{ "elevation_error",                  block_subtract_create           },
	{ "elevation_quantize",               block_quantize_create           },
	{ "elevation_lp",                     block_filter_lp_create          },
	
	{ NULL, NULL }
};

static struct param_misc controller_param[] = {
	{ "winrotor",              "az_gain",             &(float){ 7.9        }},
	{ "winrotor",              "el_gain",             &(float){ 3.18       }},
	{ "winrotor",              "az_offset",           &(float){ 0.0        }},
	{ "winrotor",              "el_offset",           &(float){ 0.0        }},
	{ "winrotor",              "enabled",             &(int){ 0            }},

	{ "azimuth_spg",           "setpoint",            &(float){ 0.0        }},
	{ "azimuth_spg",           "t",                   &(float){ 1.0 / FREQ }},
	{ "azimuth_spg",           "max",                 &(float){ 2.01 * PI  }},
	{ "azimuth_spg",           "min",                 &(float){ 0.0        }},
	{ "azimuth_spg",           "max1d",               &(float){ 0.0922     }},
	{ "azimuth_spg",           "min1d",               &(float){-0.0922     }},
	{ "azimuth_spg",           "max2d",               &(float){ 1.0        }},
	{ "azimuth_spg",           "min2d",               &(float){-1.0        }},
	{ "azimuth_spg",           "max3d",               &(float){ 1.0        }},
	{ "azimuth_spg",           "min3d",               &(float){-1.0        }},
	{ "azimuth_lp",            "tau",                 &(float){ 0.4        }},
	{ "azimuth_lp",            "t",                   &(float){ 1.0 / FREQ }},
	{ "azimuth_quantize",      "quantum",             &(float){ 0.05       }},

	{ "elevation_spg",         "setpoint",            &(float){ 0.0        }},
	{ "elevation_spg",         "t",                   &(float){ 1.0 / FREQ }},
	{ "elevation_spg",         "max",                 &(float){ 0.6 * PI   }},
	{ "elevation_spg",         "min",                 &(float){ 0.0        }},
	{ "elevation_spg",         "max1d",               &(float){ 0.043      }},
	{ "elevation_spg",         "min1d",               &(float){-0.043      }},
	{ "elevation_spg",         "max2d",               &(float){ 1.0        }},
	{ "elevation_spg",         "min2d",               &(float){-1.0        }},
	{ "elevation_spg",         "max3d",               &(float){ 1.0        }},
	{ "elevation_spg",         "min3d",               &(float){-1.0        }},
	{ "elevation_lp",          "tau",                 &(float){ 0.4        }},
	{ "elevation_lp",          "t",                   &(float){ 1.0 / FREQ }},
	{ "elevation_quantize",    "quantum",             &(float){ 0.03       }},

	{ NULL, NULL, NULL }
};

static struct block_link controller_links[] = {
	{ "winrotor",                 "az_out",     "azimuth_lp",               "in",       1 },
	{ "azimuth_lp",               "out",        "azimuth_setpoint_error",   "negative", 1 },
	{ "azimuth_lp",               "out",        "azimuth_error",            "negative", 1 },
	{ "winrotor",                 "el_out",     "elevation_lp",             "in",       1 },
	{ "elevation_lp",             "out",        "elevation_setpoint_error", "negative", 1 },
	{ "elevation_lp",             "out",        "elevation_error",          "negative", 1 },
	{ "azimuth_spg",              "spg0",       "azimuth_error",            "positive", 1 },
	{ "azimuth_spg",              "setpoint",   "azimuth_setpoint_error",   "positive", 1 },
	{ "elevation_spg",            "spg0",       "elevation_error",          "positive", 1 },
	{ "elevation_spg",            "setpoint",   "elevation_setpoint_error", "positive", 1 },
	{ "azimuth_setpoint_error",   "difference", "azimuth_quantize",         "in",       1 },
	{ "elevation_setpoint_error", "difference", "elevation_quantize",       "in",       1 },
	{ "azimuth_quantize",         "out",        "winrotor",                 "az_in",    0 },
	{ "elevation_quantize",       "out",        "winrotor",                 "el_in",    0 },

	{ NULL, NULL, NULL, NULL }
};

static struct controller_trace_name dt_trace_list[] = {
	{ "Azimuth_Spg0",                     "rad",       "azimuth_spg",                      "spg0"      },
	{ "Azimuth_Spg1",                     "rad/s",     "azimuth_spg",                      "spg1"      },
	{ "Azimuth_Spg2",                     "rad/s/s",   "azimuth_spg",                      "spg2"      },
	{ "Azimuth_Spg3",                     "rad/s/s/s", "azimuth_spg",                      "spg3"      },
	{ "Azimuth_Setpoint",                 "rad",       "azimuth_spg",                      "setpoint",  },
	{ "Azimuth_Error",                    "rad",       "azimuth_error",                    "difference" },
	{ "Azimuth_Setpoint_Error",           "rad",       "azimuth_setpoint_error",           "difference" },
	{ "Azimuth_Position",                 "rad",       "winrotor",                         "az_out"    },
	
	{ "Elevation_Position",               "rad",       "elevation_lp",                     "out"       },
	{ "Elevation_Position_Raw",           "rad",       "winrotor",                         "el_out"    },
	{ "Elevation_Setpoint",               "rad",       "elevation_spg",                    "setpoint"  },
	{ "Elevation_Spg0",                   "rad",       "elevation_spg",                    "spg0"      },
	{ "Elevation_Spg1",                   "rad/s",     "elevation_spg",                    "spg1"      },
	{ "Elevation_Spg2",                   "rad/s",     "elevation_spg",                    "spg2"      },
	{ "Elevation_Spg3",                   "rad/s",     "elevation_spg",                    "spg3"      },
	{ "Elevation_Error",                  "rad",       "elevation_error",                  "difference" },
	{ "Elevation_Setpoint_Error",         "rad",       "elevation_setpoint_error",         "difference" },
	
	{ NULL, NULL, NULL, NULL, 0.0 }
};

static struct controller_spg_name dt_spg_list[] = {
	{ "Azimuth_Setpoint",           "rad", "azimuth_spg"           },

	{ "Elevation_Setpoint",         "rad", "elevation_spg"         },

	{ NULL, NULL, NULL },
};


int main(int argc, char **argv)
{
	struct controller_block *block;
	int i;
	float val;
	
	controller_block_param_init();
	
	/* Create blocks */

	for (i = 0; controller_blocks[i].name; i++) {
		block = controller_blocks[i].create(controller_blocks[i].name);
		if (!block) {
			printf("Could not create block %s\n",
			    controller_blocks[i].name);
			return 1;
		}
	}

	/* Set params */
	
	for (i = 0; controller_param[i].blockname; i++) {
		if (controller_block_param_set(
		    controller_param[i].blockname,
		    controller_param[i].paramname,
		    controller_param[i].value) < 0) {
			printf("Could not set %s.%s\n",
			    controller_param[i].blockname,
			    controller_param[i].paramname);
			return 1;
		}
	}

	/* Link blocks */

	for (i = 0; controller_links[i].outblock; i++) {
		if (controller_block_connect(
		    controller_links[i].outblock,
		    controller_links[i].outterm,
		    controller_links[i].inblock,
		    controller_links[i].interm,
		    controller_links[i].chain) < 0) {
			printf("Could not link %s.%s to %s.%s\n",
			    controller_links[i].outblock,
			    controller_links[i].outterm,
			    controller_links[i].inblock,
			    controller_links[i].interm);
			return 1;
		}
	}


	
	/* Add traces to the trace interface */

	for (i = 0; dt_trace_list[i].name; i++)
		controller_trace_list_add(&dt_trace_list[i]);

	controller_trace_server_start(10000, 100);

	/* Add spg command handlers */

	for (i = 0; dt_spg_list[i].name; i++)
		controller_setpoint_command_server_add(&dt_spg_list[i]);

	controller_setpoint_command_server_start(11000, 100);

	/* Start 'sample' */

	controller_block_sample_init();

	controller_sample_frequency_set(FREQ);

	printf("Starting sample thread\n");
	controller_sample_start();

	do {
		sleep(1);
	} while (controller_samplenr < FREQ * 5);
	
	printf("Setting setpoint\n");
	controller_block_param_get("winrotor", "az_out", &val);
	controller_block_param_set("azimuth_spg", "setpoint", &val);
	controller_block_param_get("winrotor", "el_out", &val);
	controller_block_param_set("elevation_spg", "setpoint", &val);
	printf("Enabling rotor interface\n");
	i = 1;
	controller_block_param_set("winrotor", "enabled", &i);

	while (1) {
		sleep(60);
	}

	return 0;
}