This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEx4.ino
212 lines (173 loc) · 5.73 KB
/
Ex4.ino
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
// -----------------------------------------------------------------
// Exercise 4
// -----------------------------------------------------------------
#include <FrameStream.h>
#include <Frameiterator.h>
#include <avr/io.h>
#include "Distance.h"
#include "Odometry.h"
#include "PID.h"
#include "everytime.h"
#include "MotorControl.h"
#include "WallFollow.h"
#include <math.h>
volatile int32_t sumOfTicks=0;
uint32_t distances[100];
short i=0;
#define OUTPUT__BAUD_RATE 57600
FrameStream frm(Serial1);
// Forward declarations
void InitGUI();
// hierarchical runnerlist, that connects the gui elements with
// callback methods
declarerunnerlist(GUI);
// First level will be called by frm.run (if a frame is recived)
beginrunnerlist();
fwdrunner(!g, GUIrunnerlist); //forward !g to the second level (GUI)
callrunner(!!, InitGUI);
fwdrunner(ST, stickdata);
endrunnerlist();
// GUI level
beginrunnerlist(GUI);
callrunner(es, CallbackSTOP);
callrunner(ms, CallbackSTART);
endrunnerlist();
void stickdata(char* str, size_t length)
{
int left = (int) atof(str);
int i; int right=0;
for(i=0; i<length; i++){
if (*(str+i)==','){
right=(int) atof(str+i+1);
break;
}
}
setMotors(left,right);
}
void CallbackSTOP()
{
deactivateMotors();
}
void CallbackSTART()
{
activateMotors();
}
/*
* @brief initialises the GUI of ArduinoView
*
* In this function, the GUI, is configured. For this, Arduinoview shorthand,
* HTML as well as embedded JavaScript can be used.
*/
void InitGUI() {
delay(500);
frm.print(F("!SbesvSTOP"));
frm.end();
frm.print(F("!SbmsvSTART"));
frm.end();
//this implements a joystick field using HTML SVG and JS
//and some info divs next to it
frm.print(F("!H"
"<div><style>svg * { pointer-events: none; }</style>\n"
"<div style='display:inline-block'> <div id='state'> </div>\n"
"<svg id='stick' width='300' height='300' viewBox='-150 -150 300 300' style='background:rgb(200,200,255)' >\n"
"<line id='pxy' x1='0' y1='0' x2='100' y2='100' style='stroke:rgb(255,0,0);stroke-width:3' />\n"
"<circle id='cc' cx='0' cy='0' r='3' style='stroke:rgb(0,0,0);stroke-width:3' />\n"
"</svg></div>"
"<div style='display:inline-block'>"
"<div id='info0'></div>"
"<div id='info1'></div>"
"<div id='info2'></div>"
"</div></div>\n"
"<script>\n"
"var getEl=function(x){return document.getElementById(x)};\n"
"var setEl=function(el,attr,val){(el).setAttributeNS(null,attr,val)};\n"
"var stick=getEl('stick');\n"
"function sticktransform(x,y){\n"
"x = x-150;\n"
"y = -(y-150);\n"
"setstick(x,y);\n"
"}\n"
"function setstick(x,y){\n"
"setpointer(x,y);\n"
"l = Math.floor(Math.min(127,Math.max(-127,y + x/2)));\n"
"r = Math.floor(Math.min(127,Math.max(-127,y - x/2)));\n"
"setStateDisplay(x,y,l,r);\n"
"sendframe(\"ST\"+l +\",\"+r);\n"
"}\n"
"function setStateDisplay(x,y,l,r){\n"
"msg=getEl('state');\n"
"msg.innerHTML= 'x= '+ x +' y= '+ y +' l= '+ l +' r= '+ r ;\n"
"}\n"
"function setpointer(x,y){\n"
"pxy=getEl('pxy');\n"
"setEl(pxy,'x2',x);\n"
"setEl(pxy,'y2',-y);\n"
"}\n"
"stick.onmousemove=function(e){\n"
"if( e.buttons == 1 ){\n"
"sticktransform(e.offsetX,e.offsetY);\n"
"}};\n"
"stick.onmousedown=function(e){\n"
"if( e.buttons == 1 ){\n"
"sticktransform(e.offsetX,e.offsetY);\n"
"}};\n"
"stick.onmouseup=function(e){\n"
"setstick(0,0);\n"
"};\n"
"stick.onmouseleave=function(e){\n"
"setstick(0,0);\n"
"};\n"
"function sendframe(msg){\n"
"ArduinoView.sendMessage(ArduinoView._createSerialFrame(msg))\n"
"}\n"
"setstick(0,0);\n"
"</script>\n"));
frm.end();
}
/*
* @brief Initialisation
*
* Implement basic initialisation of the program.
*/
void setup()
{
//prepare Serial interfaces
Serial.begin(OUTPUT__BAUD_RATE);
Serial1.begin(OUTPUT__BAUD_RATE);
Serial.println(F("Willkommen zur PKeS Übung"));
Serial.println(F("PKes4"));
//request reset of GUI
frm.print("!!");
frm.end();
delay(500);
// TODO initialize Odometry, Sensors, Motors, etc.
initOdom();
configADC();
//initSensors();
initMotors();
deactivateMotors();
for(int i=0;i<100;i++)
{
distances[i]=1000;
}
}
/*
* @brief Main loop
*
* This function will be called repeatedly and shall therefore implement the
* main behavior of the program.
*/
void loop()
{
every(10){
i++;
if(i==100)
i=0;
distances[i]=getDistance();
}
every(50){
motorMain();
}
// read & run ArduinoView Frames
while(frm.run());
}