Commit bf1c5708 authored by PE Hladik's avatar PE Hladik
Browse files

upatde tutrial readme

parent 7c6d3eed
dino_p3:
gcc -c -D POSIX -D_TICK_UNIT_=3000000 -O2 -I ../lib/hippo/ -I ./lib_part3 -fmessage-length=0 -I ./dino_p3.hippo ../lib/hippo//hippo.c dino_p3.hippo/*.c ./main.c ./lib_part3/*.c
gcc -o dino_p3 ./*.o -lpthread -lrt
dino_p4:
gcc -c -D POSIX -D_TICK_UNIT_=5000000 -O2 -I ../lib/hippo/ -I ./lib_part4 -fmessage-length=0 -I ./dino_p4.hippo ../lib/hippo//hippo.c dino_p4.hippo/*.c ./main.c ./lib_part4/*.c
gcc -o dino_p4 ./*.o -lpthread -lrt
rm *.o
clean:
rm dino_p3 | true
rm dino_p4 | true
new: clean
$(MAKE)
This diff is collapsed.
File mode changed from 100644 to 100755
......@@ -567,7 +567,7 @@
update: function () {
// <start hack @hippo>
if (websocket.readyState != 0){
console.log("Let's go");
//console.log("Let's go");
if(this.horizon.obstacles[0]){
console.log("obstacle: " + this.horizon.obstacles[0].xPos);
console.log("obstacle: " + this.horizon.obstacles[0].typeConfig.type);
......@@ -575,11 +575,14 @@
st = (""+this.horizon.obstacles[0].xPos).padStart(5, '0')+";";
st += (""+this.horizon.obstacles[0].yPos).padStart(5, '0')+";";
st += (""+this.currentSpeed.toFixed(2)).padStart(5, '0')+";";
st += ""+0+";";
} else {
st = ("00300;");
st = ("00900;");
st += (""+this.horizon.obstacles[0].yPos).padStart(5, '0')+";";
st += (""+this.currentSpeed.toFixed(2)).padStart(5, '0')+";";
st += ""+0+";";
}
console.log(st);
websocket.send(st);
}
......@@ -861,6 +864,22 @@
// Reset the time clock.
this.time = getTimeStamp();
// <start hack @hippo>
var param = this.distanceMeter.getActualDistance(Math.ceil(this.distanceRan));//Math.ceil(this.distanceRan);
console.log(param);
setTimeout(function(){/*YourCode*/
if (websocket.readyState != 0){
console.log("Crashed");
st = "00300;";
st += "00000;";
st += "00000;";
st += (""+param).padStart(5, '0')+";";
websocket.send(st);
console.log(st);
}
},200);
// <end hack @hippo>
},
stop: function () {
......
task t_init () : int is f_init_mg
task t_ww () : int is f_wait_websocket
event e_keyboard : int is f_keyboard
event e_keyboard : 1..3 is f_keyboard
process manager is
states s1, s2, s3, s4, s5, s6
var key : int := 0,
t : int := 1
var key : 1..3 := 3,
t : int := 1
from s1
start t_init();
......@@ -24,9 +24,9 @@ process manager is
to s6
from s6
case key of
1 -> to s3
| 2 -> to s3
| any -> to s3
1 -> to s5
| 2 -> to s5
| 3 -> to s5
end
......
type msg_id is union msg_start | msg_restart end
task t_send (msg_id): int is f_send
task t_init () : int is f_init_mg
task t_ww () : int is f_wait_websocket
event e_keyboard : int is f_keyboard
task t_init () : int is f_init_mg
task t_ww () : int is f_wait_websocket
event e_keyboard : 1..3 is f_keyboard
process manager is
states s1, s2, s3, s4, s5, s6, s7, s8
var key : int := 0,
var key : 1..3 := 3,
t : int := 1,
msg : msg_id := msg_stop
msg : msg_id := msg_restart
from s1
start t_init();
......@@ -28,9 +28,9 @@ process manager is
to s6
from s6
case key of
1 -> msg := msg_start; to s7
1 -> msg := msg_start; to s7
| 2 -> msg := msg_restart; to s7
| any -> to s6
| 3 -> to s5
end
from s7
start t_send(msg);
......
type msg_id is union msg_start | msg_restart | msg_jump | msg_duck | msg_stop_ducking end
type wsData is record xObstacle, yObstacle, speed : int end
type msg_id is union msg_start | msg_restart end
type wsData is record xObstacle, yObstacle, speed : int, isCrashed : bool end
task t_read () : wsData is f_read
task t_send (msg_id): int is f_send
task t_init () : int is f_init_mg
task t_ww () : int is f_wait_websocket
event e_keyboard : int is f_keyboard
task t_init () : int is f_init_mg
task t_ww () : int is f_wait_websocket
event e_keyboard : 1..3 is f_keyboard
process manager (&isPeriodic: bool) is
process manager (&isPeriodic: bool, &isCrashed : bool) is
states s1, s2, s3, s4, s5, s6, s7, s8
var key : int := 0,
var key : 1..3 := 3,
t : int := 1,
msg : msg_id := msg_restart
......@@ -30,10 +30,9 @@ process manager (&isPeriodic: bool) is
to s6
from s6
case key of
1 -> msg := msg_start; isPeriodic := true; to s7
| 2 -> msg := msg_restart; isPeriodic := true; to s7
| 3 -> isPeriodic := false; to s5
| any -> to s5
1 -> msg := msg_start; isPeriodic := true; isCrashed := false; to s7
| 2 -> msg := msg_restart; isPeriodic := true; isCrashed := false; to s7
| 3 -> isCrashed := true; to s5
end
from s7
start t_send(msg);
......@@ -42,74 +41,52 @@ process manager (&isPeriodic: bool) is
sync t_send t;
to s5
process periodic_clock [next_period : sync](&isPeriodic: bool) is
process periodic_clock (&isPeriodic: bool, &isCrashed : bool, &periodReader: bool) is
states s1, s2, s3
from s1
on isPeriodic;
on isPeriodic;
wait[0,0];
isPeriodic := false;
to s2
from s2
wait[10,10];
to s3
from s3
if isPeriodic then
next_period; to s2
if (not isCrashed) then
periodReader := true; to s2
else
isCrashed := false;
to s1
end
process trainer [next_period : sync] is
states s1, s2, s3, s4, s5, s6, s7
var d : wsData,
duck : bool := false,
ret : int := 0,
msg : msg_id := msg_jump
from s1
next_period;
process reader (&isCrashed : bool, &periodReader: bool, &d : wsData) is
states s1, s2, s3
from s1
on periodReader;
wait[0,0];
periodReader := false;
to s2
from s2
start t_read();
to s3
from s3
sync t_read d;
if (d.xObstacle < ((130*(100 + d.speed/20))/100) ) then
to s4
else
to s5
end
from s4
if ((d.yObstacle < 76) and (not duck)) then
duck := true;
msg := msg_duck;
to s6
else
msg := msg_jump;
to s6
end
from s5
if (duck) then
duck := false;
msg := msg_stop_ducking;
to s6
else
to s1
end
from s6
start t_send(msg);
to s7
from s7
sync t_send ret;
to s1
isCrashed := d.isCrashed;
to s1
component Hossi is
var isPeriodic : bool := false
port next_period : sync in [0,0]
var isPeriodic : bool := false,
isCrashed : bool := false,
periodReader: bool := false,
d : wsData := {xObstacle=0, yObstacle=0, speed=0, isCrashed=false}
par * in
manager (&isPeriodic)
|| periodic_clock [next_period](&isPeriodic)
|| trainer [next_period]
manager (&isPeriodic, &isCrashed)
|| periodic_clock (&isPeriodic, &isCrashed, &periodReader)
|| reader (&isCrashed, &periodReader, &d)
end
Hossi
\ No newline at end of file
type wsData is record xObstacle, yObstacle, speed : int end
event e_keyboard : int is f_keyboard
task t_start () : int is f_start
task t_stop () : int is f_stop
task t_jump () : int is f_jump
task t_duck () : int is f_duck
task t_sduck () : int is f_sduck
task t_read () : wsData is f_read
task t_init () : int is f_init_mg
task t_ww () : int is f_wait_websocket
process manager (&isPeriodic: bool) is
states a, b, c, d, e, f, w, x, y, z
var key : int,
t : int := 1
from w
start t_init();
to x
from x
sync t_init t;
to y
from y
start t_ww();
to z
from z
sync t_ww t;
to a
from a
e_keyboard?key;
to b
from b
case key of
1 -> isPeriodic := true; to c
| 2 -> isPeriodic := false; to e
| any -> to a
end
from c
start t_start();
to d
from d
sync t_start t;
to a
from e
start t_stop();
to f
from f
sync t_stop t;
to y
process periodic_clock [next_period : sync](&isPeriodic: bool) is
states a, b, c
from a
on isPeriodic;
to b
from b
wait[10,10];
to c
from c
if isPeriodic then
next_period; to b
else
to a
end
process dino [next_period : sync] is
states a, b, c, d, e, s_ducking, e_ducking, s_jumping, e_jumping, s_stop_ducking, e_stop_ducking
var d : wsData,
l : int := 0,
duck : bool := false,
previous : int := 0,
ret : int := 0
from a
next_period;
to b
from b
start t_read();
to c
from c
sync t_read d;
l := (130*(100 + d.speed/20))/100;
if (d.xObstacle < l) then
to d
else
to e
end
from d
previous := l;
if ((d.yObstacle < 76) and (not duck)) then // need to duck
duck := true;
to s_ducking
else // need to jump
to s_jumping
end
from e
//if ((duck) and ((d.xObstacle < 15) or (d.xObstacle > previous))) then // need to stop ducking
if (duck) then // need to stop ducking
duck := false;
previous := 0;
to s_stop_ducking
else
to a
end
from s_ducking
start t_duck();
to e_ducking
from e_ducking
sync t_duck ret;
to a
from s_jumping
start t_jump();
to e_jumping
from e_jumping
sync t_jump ret;
to a
from s_stop_ducking
start t_sduck();
to e_stop_ducking
from e_stop_ducking
sync t_sduck ret;
to a
component Hossi is
var isPeriodic : bool := false
port next_period : sync in [0,0]
par * in
manager (&isPeriodic)
|| periodic_clock [next_period](&isPeriodic)
|| dino [next_period]
end
Hossi
......@@ -26,16 +26,24 @@ int f_wait_websocket(){
return 0;
}
int f_keyboard(){
printf("DINO >> keyboard (1: start, 2: restart)\n");
char c;
c = getchar();
getchar();
int i = c - 48 ;
return i;
char f_keyboard(){
char c[2];
int i;
do {
printf("DINO >> enter action (1: start, 2: restart, 3:stop)\n");
fgets(c, 2, stdin);
int a = 0;
while (a != '\n' && a != EOF) {
a = getchar();
}
i = atoi(c);
if ((i < 0) || (i > 3) ){
printf("<--- The value must be 1, 2 or 3 --->\n");
}
} while ((i < 0) || (i > 3) );
return i ;
}
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
......@@ -49,4 +57,4 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
mg_iobuf_delete(&c->recv, c->recv.len);
}
(void) fn_data;
}
\ No newline at end of file
}
......@@ -6,6 +6,6 @@
int f_init_mg();
int f_wait_websocket();
int f_keyboard();
char f_keyboard();
#endif
\ No newline at end of file
#endif
......@@ -14,7 +14,7 @@ int isWebsocketOpen = 0;
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data);
int f_send(msg_id_c msg_id){
mg_ws_send(mgr.conns, msg_map[msg_id], MSG_SIZE, WEBSOCKET_OP_TEXT);
mg_ws_send(mgr.conns, msg_map[msg_id], MSG_SIZE, WEBSOCKET_OP_TEXT);
mg_mgr_poll(&mgr, 1);
return 0;
}
......@@ -37,13 +37,22 @@ int f_wait_websocket(){
return 0;
}
int f_keyboard(){
printf("DINO >> keyboard (1: start, 2: restart)\n");
char c;
c = getchar();
getchar();
int i = c - 48 ;
return i;
char f_keyboard(){
char c[2];
int i;
do {
printf("DINO >> enter action (1: start, 2: restart, 3:stop)\n");
fgets(c, 2, stdin);
int a = 0;
while (a != '\n' && a != EOF) {
a = getchar();
}
i = atoi(c);
if ((i < 0) || (i > 3) ){
printf("<--- The value must be 1, 2 or 3 --->\n");
}
} while ((i < 0) || (i > 3) );
return i ;
}
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
......@@ -56,14 +65,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
}
} else if (ev == MG_EV_WS_MSG) {
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
char str[5];
strncpy(str, wm->data.ptr, 5);
int xpos = atoi(str);
strncpy(str, (wm->data.ptr)+6, 5);
int ypos = atoi(str);
strncpy(str, wm->data.ptr+12, 5);
mg_iobuf_delete(&c->recv, c->recv.len);
}
(void) fn_data;
}
\ No newline at end of file
}
......@@ -5,11 +5,8 @@
#include "hippo_app_functions.h"
int f_init_mg();
int f_wait_websocket();
int f_keyboard();
char f_keyboard();
int f_send(msg_id_c);
#endif
\ No newline at end of file
#endif
#include "hippo_functions.h"
#define MSG_SIZE 4
#define NB_MSG_ID 5
#define NB_MSG_ID 2
//msg_start | msg_restart | msg_jump | msg_duck | msg_stop_ducking
char msg_map[NB_MSG_ID][MSG_SIZE] = {"duck", "jump", "rsta", "star", "sdck"};
char msg_map[NB_MSG_ID][MSG_SIZE] = {"rsta", "star"};
static const char *s_listen_on = "http://localhost:8000";
static const char *s_web_directory = ".";
......@@ -12,18 +12,27 @@ struct mg_mgr mgr;
int isWebsocketOpen = 0;
wsData_c data = {0,250,0};
int val;
pthread_mutex_t mutex;
wsData_c data = {0,0,900,0};
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data);
wsData_c f_read(){
wsData_c f_read(){
//printf("==> start read\n");
data.xObstacle = 900;
data.yObstacle = 0;
data.speed = (int)(7*100);
data.isCrashed = 0;
mg_mgr_poll(&mgr, 1);
//printf("data {s=%d, x=%d, y=%d}\n", data.speed, data.xObstacle, data.yObstacle);
printf("data {speed=%d, xObs=%d, yObs=%d, crashed=%d}\n", data.speed, data.xObstacle, data.yObstacle, data.isCrashed);
//printf("==> end read\n");
val = 1;
return data;
}
int f_send(msg_id_c msg_id){
printf("send: %d\n", msg_id);
mg_ws_send(mgr.conns, msg_map[msg_id], MSG_SIZE, WEBSOCKET_OP_TEXT);
mg_mgr_poll(&mgr, 1);
return 0;
......@@ -47,13 +56,22 @@ int f_wait_websocket(){
return 0;
}
int f_keyboard(){
printf("DINO >> keyboard (1: start, 2: restart)\n");
char c;
c = getchar();
getchar();
int i = c - 48 ;
return i;
char f_keyboard(){
char c[2];
int i;
do {
printf("DINO >> enter action (1: start, 2: restart, 3:stop)\n");
fgets(c, 2, stdin);
int a = 0;
while (a != '\n' && a != EOF) {
a = getchar();
}
i = atoi(c);
if ((i < 0) || (i > 3) ){
printf("<--- The value must be 1, 2 or 3 --->\n");
}
} while ((i < 0) || (i > 3) );
return i ;
}
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
......@@ -65,19 +83,23 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
mg_ws_upgrade(c, hm, NULL);
}
} else if (ev == MG_EV_WS_MSG) {
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
char str[5];
strncpy(str, wm->data.ptr, 5);
int xpos = atoi(str);
strncpy(str, (wm->data.ptr)+6, 5);
int ypos = atoi(str);
strncpy(str, wm->data.ptr+12, 5);
float speed = atof(str);
data.xObstacle = xpos;
data.yObstacle = ypos;
data.speed = (int)speed*100;
mg_iobuf_delete(&c->recv, c->recv.len);
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
char str[5];
strncpy(str, wm->data.ptr, 5);
int xpos = atoi(str);
strncpy(str, (wm->data.ptr)+6, 5);
int ypos = atoi(str);
strncpy(str, wm->data.ptr+12, 5);
float speed = atof(str);
strncpy(str, wm->data.ptr+18, 5);