-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.c
108 lines (86 loc) · 3.3 KB
/
input.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* File: input.c
* By: Alex Tong, Date: Tue Mar 11
* Last Updated: Sun May 11 21:03:04
*
* file for reading in input as specified in state_rep.h
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include "state_rep.h"
#include "input.h"
#include "polar.h"
#include "helper.h"
#ifdef DATA_GEN
extern double ideal_speed(Env_data env, Boat_data boat);
static void update_pos(Boat_data boat, double boat_speed)
{
/* convert velocity vectors to angular velocity */
double boat_x = boat_speed * cosf(degrees_to_radians(boat->heading)),
boat_y = boat_speed * sinf(degrees_to_radians(boat->heading));
/* convert tangential velocity vectors to angular velocity */
double ang_vel_y = boat_y / EARTH_R;
double ang_vel_x = boat_x / EARTH_R;
/* convert position to radians */
double long_rad = degrees_to_radians(boat->pos.lon);
double lat_rad = degrees_to_radians(boat->pos.lat);
/* add angular velocity times time to current position
* and update boat's position and convert back
* NOTE: f = 1 / t, so t = 1 / f, hence division
*/
boat->pos.lat = radians_to_degrees(lat_rad + ang_vel_y * RATE);
boat->pos.lon = radians_to_degrees(long_rad + ang_vel_x * RATE);
}
void output_state(FILE *fp, Env_data env, Boat_data boat) {
fprintf(fp, OUTPUT_FORMAT, OUTPUT_ARGS);
}
#endif
/* reads a data string into boat and enviroment data */
int update_state(char *data, Env_data env, Boat_data boat)
{
/* read env */
int re = sscanf(data, DATA_FORMAT_STRING, DATA_ARGS);
/* Skip badly formed lines */
if (re != NUM_MEMS) {
fprintf(stderr, "Error reading data, read in: %d / %d necessary "
"members\n", re, NUM_MEMS);
return 1;
}
#ifdef DATA_GEN
double boat_angle = (M_PI * boat->heading) / 180,
/* boat_speed = 10, */
boat_speed = ideal_speed(env, boat),
wind_angle = (M_PI * env->wind_dir) / 180,
/* wind_angle = degrees_to_radians(env->wind_dir),*/
wind_speed = env->wind_speed;
update_pos(boat, boat_speed);
boat->boat_speed = boat_speed;
/* resolve boat vectors */
double boat_x = boat->boat_speed * cosf(boat_angle),
boat_y = boat->boat_speed * sinf(boat_angle);
/* resolve wind vectors */
double wind_x = wind_speed * cosf(wind_angle),
wind_y = wind_speed * sinf(wind_angle);
/* calculate vector product */
double app_wind_x = boat_x + wind_x, app_wind_y = boat_y + wind_y;
/* determine vector components */
double temp = (180 * atan(app_wind_y / app_wind_x)) / M_PI;
env->app_wind_dir = (temp < 0) ? temp + 360 : temp;
env->app_wind_speed = sqrt(app_wind_x * app_wind_x + app_wind_y * app_wind_y);
#endif
return 0;
}
unsigned read_waypts(FILE *fp, Position *waypts, unsigned size) {
char *line = malloc(16); /* TODO 16 I believe is arbitrary and needs to change */
unsigned num_waypoints, i;
assert(fscanf(fp, "%u", &num_waypoints) == 1);
assert(num_waypoints >= 0 && num_waypoints <= size);
/* read in each waypt */
for (i = 0; i < num_waypoints; ++i) {
assert(fscanf(fp, "%lf,%lf;", &(waypts[i].lat), &(waypts[i].lon)) == 2);
}
fgetc(fp); /* discard the newline */
free(line);
return num_waypoints;
}