-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
292 lines (249 loc) · 8.6 KB
/
main.cpp
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* Revision: 2.8.7 */
/******************************************************************************
* Copyright 1998-2018 NetBurner, Inc. ALL RIGHTS RESERVED
*
* Permission is hereby granted to purchasers of NetBurner Hardware to use or
* modify this computer program for any use as long as the resultant program
* is only executed on NetBurner provided hardware.
*
* No other rights to use this program or its derivatives in part or in
* whole are granted.
*
* It may be possible to license this or other NetBurner software for use on
* non-NetBurner Hardware. Contact sales@Netburner.com for more information.
*
* NetBurner makes no representation or warranties with respect to the
* performance of this computer program, and specifically disclaims any
* responsibility for any damages, special or consequential, connected with
* the use of this program.
*
* NetBurner
* 5405 Morehouse Dr.
* San Diego, CA 92121
* www.netburner.com
******************************************************************************/
/**
* @file main.cpp
* @brief WebGL Example
*
* In this example, we look at how to take advantage some of the more recent web technologies available
* by building a page that utilizes WebGL and a really nice wrapper library, Three.js, to render
* a simple textured model. We then update the position and rotation of that model using a WebSocket
* connection. The main points of this example will show how to incorporate an SD card in order to hold
* all of the model and texture data, as well as the JavaScript libraries, how to use the NetBurner as
* an FTP server to be able to remotely update your assets, and how to get your scene up and running.
*/
// NB Constants
#include <constants.h>
// NB Libs
#include <buffers.h>
#include <http.h>
#include <init.h>
#include <iosys.h>
#include <stdlib.h>
#include <webclient/json_lexer.h>
#include <multichanneli2c.h>
#include <pins.h>
#include <ucosmcfc.h>
// NB FTP
#include <ftpd.h>
#if (defined(USE_MMC) && defined(MOD5441X))
#define MULTI_MMC TRUE // For modules with onboard flash sockets, even if you are using external flash cards
#include <effs_fat/multi_drive_mmc_mcf.h>
#elif (defined(USE_MMC))
#include <effs_fat/mmc_mcf.h>
#elif (defined(USE_CFC))
#include <effs_fat/cfc_mcf.h>
#endif
#include "FileSystemUtils.h"
#include "cardtype.h"
#include "web.h"
#include "IMUSetupAndSample.h"
#include "MPU9250.h"
#include "Boot_Settings.h"
const char *AppName = "WebGL Example with IMU";
MPU9250 imu; //Default constructor does nothing
NV_SettingsStruct NV_Settings; //Saved flash memory
// The FTP task priority
#define FTP_PRIO (MAIN_PRIO - 2)
#define IMU_TIMER 3
// Our WebSocket file descriptor
extern int ws_fd;
// These are buffers that we use to send JSON data to clients
const int ReportBufSize = 512;
char ReportBuffer[ReportBufSize];
// These are the global values that we will use managing the position and rotation
// of the model.
float Pos[3] = {0.0, 0.0, 0.0};
float GoalPos[3] = {0.0, 0.0, 0.0};
float Rot[3] = {0.0, 0.0, 0.0}; // Rotations are stored/sent as radians
float GoalRot[3] = {0.0, 0.0, 0.0}; // Rotations are stored/sent as radians
extern float q[];
extern "C"
{
void UserMain(void *pd);
}
/**
* @brief Determines how much to change a position or rotation value for one
* step.
*/
inline float GetMovVal(float dif)
{
float difSign = dif < 0.0 ? -1 : 1;
return ((dif * difSign) >= 0.025) ? 0.025 * difSign : dif;
}
/**
* @brief This function manages the goal positions and rotations for our model to move to.
* Once we get within a certain distance of the goal value, we pick new ones.
*
* When using an actual sensor, this is the function that should get updated to use real values.
* The rotation values are expected to be in radians.
*/
void UpdatePosAndRot()
{
// Find the distance between the two
float xDif = GoalPos[0] - Pos[0];
float yDif = GoalPos[1] - Pos[1];
float zDif = GoalPos[2] - Pos[2];
float dist = sqrt((xDif * xDif) + (yDif * yDif) + (zDif * zDif));
// Close enough, pick new points
if (dist < 0.01)
{
for (int i = 0; i < 3; i++)
{
float posSign = ((rand() % 2) == 0) ? -1 : 1;
GoalPos[i] = (rand() % 2) * posSign; // The 2 here is an arbitrary value to keep the model on the screen
}
}
else
{
Pos[0] += GetMovVal(xDif);
Pos[1] += GetMovVal(yDif);
Pos[2] += GetMovVal(zDif);
}
// Pick new goal rotation
// Find the distance between the two
xDif = GoalRot[0] - Rot[0];
yDif = GoalRot[1] - Rot[1];
zDif = GoalRot[2] - Rot[2];
dist = sqrt((xDif * xDif) + (yDif * yDif) + (zDif * zDif));
// Close enough, pick new rotation values
if (dist < 0.01)
{
for (int i = 0; i < 3; i++)
{
float rotSign = ((rand() % 2) == 0) ? -1 : 1;
GoalRot[i] = ((rand() % 7855) / 10000.0) * rotSign; // .7855 is a hair over 45 degrees
}
}
else
{
Rot[0] += GetMovVal(xDif);
Rot[1] += GetMovVal(yDif);
Rot[2] += GetMovVal(zDif);
}
}
/**
* @brief This function packages up our rotation and position data, and sends it out the
* file descriptor used by the WebSocket connection.
*/
void SendWebSocketData()
{
// Prevent the values from changing in the middle of trying to assign them later by storing them
// in a local variable.
USER_ENTER_CRITICAL();
float q0 = q[0];
float q1 = q[1];
float q2 = q[2];
float q3 = q[3];
USER_EXIT_CRITICAL();
// Our JSON blob that we will send
ParsedJsonDataSet jsonOutObj;
// Build the JSON blob
jsonOutObj.StartBuilding();
jsonOutObj.AddObjectStart("PosUpdate");
jsonOutObj.Add("x", 0.0);
jsonOutObj.Add("y", 0.0);
jsonOutObj.Add("z", 0.0);
jsonOutObj.EndObject();
jsonOutObj.AddObjectStart("RotUpdate");
jsonOutObj.Add("w", q0);
jsonOutObj.Add("x", -q2);
jsonOutObj.Add("y", -q3);
jsonOutObj.Add("z", q1);
jsonOutObj.EndObject();
jsonOutObj.DoneBuilding();
// If you would like to print the JSON object to serial to see the format, uncomment the next line
// JsonOutObject.PrintObject(true);
// Print JSON object to a buffer and write the buffer to the WebSocket file descriptor
int dataLen = jsonOutObj.PrintObjectToBuffer(ReportBuffer, ReportBufSize);
writeall(ws_fd, ReportBuffer, dataLen);
}
/**
* @brief This is where our main application begins.
*/
void UserMain(void *pd)
{
init();
OSChangePrio(MAIN_PRIO);
/**
* The following call to f_enterFS() must be called in every task that accesses
* the file system. This must only be called once in each task and must be done before
* any other file system functions are used. Up to 10 tasks can be assigned to use
* the file system. Any task may also be removed from accessing the file system with a
* call to the function f_releaseFS().
*/
f_enterFS();
// We must also enter the file system for the HTTP tasks
OSChangePrio(HTTP_PRIO);
f_enterFS();
// We must also enter the file system for the FTP tasks
OSChangePrio(FTP_PRIO);
f_enterFS();
OSChangePrio(MAIN_PRIO);
// Initialize the CFC or SD/MMC external flash drive
InitExtFlash();
// Initialize the stack, set up the web server, etc.
StartHTTP();
RegisterWebFuncs();
// Start FTP server with a task priority higher than UserMain()
int status = FTPDStart(21, FTP_PRIO);
if (status == FTPD_OK)
{
iprintf("Started FTP Server\r\n");
if (F_LONGFILENAME == 1) { iprintf("Long file names are supported\r\n"); }
else
{
iprintf("Long file names are not supported- only 8.3 format\r\n");
}
}
else
{
iprintf("** Error: %d. Could not start FTP Server\r\n", status);
}
//I2C (communication used for IMU)
MultiChannel_I2CInit();
Pins[27].function(PIN_27_I2C0_SCL); //I2C Pins: change accordingly
Pins[29].function(PIN_29_I2C0_SDA);
//Re-construct MPU9250 object
//passing in -1 assigns the next free HiResTimer to the imu
imu = MPU9250(IMU_TIMER); //ignore warning that 'imu' set and not used
//Initialize and calibrate IMU
IMUSetup();
//Create RTOS task that updates the IMU at a specified update rate
IMURun();
iprintf("Starting WebGL Example\r\n");
DumpDir();
while (1)
{
// Update the simulations position and rotation values
//UpdatePosAndRot();
// If we have a valid WebSocket file descriptor
if (ws_fd > 0)
{
// Send our data
SendWebSocketData();
}
OSTimeDly(1);
}
}