/[zanavi_public1]/navit/navit/vehicle/demo/vehicle_demo.c
ZANavi

Contents of /navit/navit/vehicle/demo/vehicle_demo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 30 - (show annotations) (download)
Wed Aug 22 17:01:27 2012 UTC (11 years, 7 months ago) by zoff99
File MIME type: text/plain
File size: 7048 byte(s)
ZANavi 2.0, lots of changes, everything in its own thread, more performance
1 /**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2011-2012 Zoff <zoff@zoff.cc>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * Navit, a modular navigation system.
22 * Copyright (C) 2005-2008 Navit Team
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * version 2 as published by the Free Software Foundation.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the
35 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
36 * Boston, MA 02110-1301, USA.
37 */
38
39 #include <glib.h>
40 #include <string.h>
41 #include "config.h"
42 #include "debug.h"
43 #include "coord.h"
44 #include "item.h"
45 #include "navit.h"
46 #include "map.h"
47 #include "route.h"
48 #include "callback.h"
49 #include "transform.h"
50 #include "plugin.h"
51 #include "vehicle.h"
52 #include "event.h"
53 #include "util.h"
54
55 struct vehicle_priv
56 {
57 int interval;
58 int position_set;
59 struct callback_list *cbl;
60 struct navit *navit;
61 struct coord_geo geo;
62 struct coord last;
63 double config_speed;
64 double speed;
65 double direction;
66 struct callback *timer_callback;
67 struct event_timeout *timer;
68 char *timep;
69
70 };
71
72 static void vehicle_demo_destroy(struct vehicle_priv *priv)
73 {
74 g_free(priv->timep);
75 g_free(priv);
76 }
77
78 static int vehicle_demo_position_attr_get(struct vehicle_priv *priv, enum attr_type type, struct attr *attr)
79 {
80 switch (type)
81 {
82 case attr_position_speed:
83 attr->u.numd = &priv->speed;
84 break;
85 case attr_position_direction:
86 attr->u.numd = &priv->direction;
87 break;
88 case attr_position_coord_geo:
89 attr->u.coord_geo = &priv->geo;
90 break;
91 case attr_position_time_iso8601:
92 g_free(priv->timep);
93 priv->timep = current_to_iso8601();
94 attr->u.str = priv->timep;
95 break;
96 default:
97 return 0;
98 }
99 attr->type = type;
100 return 1;
101 }
102
103 static int vehicle_demo_set_attr(struct vehicle_priv *priv, struct attr *attr)
104 {
105 if (attr->type == attr_navit)
106 {
107 priv->navit = attr->u.navit;
108 }
109 return 1;
110 }
111
112 struct vehicle_methods vehicle_demo_methods =
113 { vehicle_demo_destroy, vehicle_demo_position_attr_get, vehicle_demo_set_attr, NULL, };
114
115 static void vehicle_demo_timer(struct vehicle_priv *priv)
116 {
117 struct coord c, c2, pos, ci;
118 int slen, len, dx, dy;
119 struct route *route = NULL;
120 struct map *route_map = NULL;
121 struct map_rect *mr = NULL;
122 struct item *item = NULL;
123
124 //dbg(0,"stop demo vehicle=%d\n", global_stop_demo_vehicle);
125 if (global_stop_demo_vehicle == 1)
126 {
127 // demo vehicle should stand still!
128 return;
129 }
130
131 len = (priv->config_speed * priv->interval / 1000) / 3.6;
132 //dbg(0, "###### Entering simulation loop\n");
133 if (priv->navit)
134 {
135 route = navit_get_route(priv->navit);
136 }
137 //DBG dbg(0,"rr 1\n");
138 if (route)
139 {
140 route_map = route_get_map(route);
141 }
142 //DBG dbg(0,"rr 2\n");
143 if (route_map)
144 {
145 mr = map_rect_new(route_map, NULL);
146 }
147 //DBG dbg(0,"rr 3\n");
148 if (mr)
149 {
150 item = map_rect_get_item(mr);
151 }
152 //DBG dbg(0,"rr 4\n");
153 if (item && item->type == type_route_start)
154 {
155 item = map_rect_get_item(mr);
156 }
157 //dbg(0,"rr 5\n");
158 if (item && item_coord_get(item, &pos, 1))
159 {
160 priv->position_set = 0;
161 //dbg(0, "current pos=0x%x,0x%x\n", pos.x, pos.y);
162 ////DBG dbg(0, "last pos=0x%x,0x%x\n", priv->last.x, priv->last.y);
163 //if (priv->last.x == pos.x && priv->last.y == pos.y)
164 //{
165 // //dbg(1, "endless loop\n");
166 //}
167
168 priv->last = pos;
169 while (item && priv->config_speed)
170 {
171 if (!item_coord_get(item, &c, 1))
172 {
173 item = map_rect_get_item(mr);
174 continue;
175 }
176 //dbg(0, "next pos=0x%x,0x%x\n", c.x, c.y);
177 slen = transform_distance(projection_mg, &pos, &c);
178 ////DBG dbg(0, "len=%d slen=%d\n", len, slen);
179 if (slen < len)
180 {
181 len -= slen;
182 pos = c;
183 }
184 else
185 {
186 if (item_coord_get(item, &c2, 1) || map_rect_get_item(mr))
187 {
188 dx = c.x - pos.x;
189 dy = c.y - pos.y;
190 ci.x = pos.x + dx * len / slen;
191 ci.y = pos.y + dy * len / slen;
192 priv->direction = transform_get_angle_delta(&pos, &c, 0);
193 priv->speed = priv->config_speed;
194 }
195 else
196 {
197 ci.x = pos.x;
198 ci.y = pos.y;
199 priv->speed = 0;
200 ////DBG dbg(0, "destination reached\n");
201 }
202 //dbg(1, "ci=0x%x,0x%x\n", ci.x, ci.y);
203 transform_to_geo(projection_mg, &ci, &priv->geo);
204 callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
205 break;
206 }
207 }
208 }
209 else
210 {
211 if (priv->position_set)
212 {
213 callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
214 }
215 }
216 //DBG dbg(0,"rr 6\n");
217 if (mr)
218 {
219 map_rect_destroy(mr);
220 }
221 // dbg(0,"rr F\n");
222 }
223
224 static struct vehicle_priv *
225 vehicle_demo_new(struct vehicle_methods *meth, struct callback_list *cbl, struct attr **attrs)
226 {
227 struct vehicle_priv *ret;
228 struct attr *interval, *speed, *position_coord_geo;
229
230 //DBG dbg(0, "enter\n");
231 ret = g_new0(struct vehicle_priv, 1);
232 ret->cbl = cbl;
233 ret->interval = 1200;
234 ret->config_speed = 41;
235
236 //dbg(0, "vd 3.1 %d %d\n", ret->interval, ret->config_speed);
237
238 //DBG dbg(0, "vd 1\n");
239 if ((speed = attr_search(attrs, NULL, attr_speed)))
240 {
241 ret->config_speed = speed->u.num;
242 }
243
244 //DBG dbg(0, "vd 2\n");
245 if ((interval = attr_search(attrs, NULL, attr_interval)))
246 {
247 ret->interval = interval->u.num;
248 }
249
250 // ret->geo.lat = 0;
251 // ret->geo.lng = 0;
252 // //DBG dbg(0, "position_default %f %f\n", ret->geo.lat, ret->geo.lng);
253
254 //dbg(0, "vd 3.2 %d %d\n", ret->interval, ret->config_speed);
255 if ((position_coord_geo = attr_search(attrs, NULL, attr_position_coord_geo)))
256 {
257 ret->geo = *(position_coord_geo->u.coord_geo);
258 ret->position_set = 1;
259 //DBG dbg(0, "position_set %f %f\n", ret->geo.lat, ret->geo.lng);
260 }
261
262 //DBG dbg(0, "vd 4\n");
263 *meth = vehicle_demo_methods;
264 ret->timer_callback = callback_new_1(callback_cast(vehicle_demo_timer), ret);
265 callback_add_names(ret->timer_callback, "vehicle_demo_new", "vehicle_demo_timer");
266 //DBG dbg(0, "vd 5\n");
267 dbg(0, "event_add_timeout %d,%d,%p", ret->interval, 1, ret->timer_callback);
268 ret->timer = event_add_timeout(ret->interval, 1, ret->timer_callback);
269 //DBG dbg(0, "leave\n");
270 return ret;
271 }
272
273 void plugin_init(void)
274 {
275 //DBG dbg(0, "enter\n");
276 plugin_register_vehicle_type("demo", vehicle_demo_new);
277 }

   
Visit the ZANavi Wiki