Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
slapos
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Xavier Thompson
slapos
Commits
63587a63
Commit
63587a63
authored
Nov 15, 2022
by
Léo-Paul Géneau
👾
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
software/js-drone: Comply with JSLint
parent
8141a390
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
88 additions
and
68 deletions
+88
-68
software/js-drone/buildout.hash.cfg
software/js-drone/buildout.hash.cfg
+3
-3
software/js-drone/main.js
software/js-drone/main.js
+33
-29
software/js-drone/pubsub.js
software/js-drone/pubsub.js
+23
-12
software/js-drone/worker.js
software/js-drone/worker.js
+29
-24
No files found.
software/js-drone/buildout.hash.cfg
View file @
63587a63
...
...
@@ -26,12 +26,12 @@ md5sum = 1ff50063f5a54712a0bc0ff38fa74630
[main]
filename = main.js
md5sum =
c22879bb553a0932dd145a58acdf37e6
md5sum =
2118d7908a909c585e03531147b1d540
[pubsub]
filename = pubsub.js
md5sum =
c732be66f8ec97bd16cd34d06a0c0a0b
md5sum =
1555496ad591a31a845f33488d5c335d
[worker]
filename = worker.js
md5sum =
d919ce35d42561bc38a06273781cb702
md5sum =
bb067644bb1ee2e8eda1324718e317b1
software/js-drone/main.js
View file @
63587a63
/* global console */
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */
/*global arm, console, exit, open, scriptArgs, setTimeout, start, stop,
stopPubsub, takeOffAndWait, Worker*/
import
{
arm
,
start
,
...
...
@@ -9,16 +11,23 @@ import {
import
{
setTimeout
,
Worker
}
from
"
os
"
;
import
{
open
,
exit
}
from
"
std
"
;
(
function
(
console
,
setTimeout
,
Worker
)
{
(
function
(
arm
,
console
,
exit
,
open
,
scriptArgs
,
setTimeout
,
start
,
stop
,
stopPubsub
,
takeOffAndWait
,
Worker
)
{
"
use strict
"
;
const
CONF_PATH
=
{{
json_module
.
dumps
(
configuration
)
}};
var
conf_file
=
open
(
CONF_PATH
,
"
r
"
);
const
configuration
=
JSON
.
parse
(
conf_file
.
readAsString
());
conf_file
.
close
();
var
CONF_PATH
=
{{
json_module
.
dumps
(
configuration
)
}},
conf_file
=
open
(
CONF_PATH
,
"
r
"
),
configuration
=
JSON
.
parse
(
conf_file
.
readAsString
()),
URL
=
"
udp://
"
+
configuration
.
autopilotIp
+
"
:7909
"
,
LOG_FILE
=
"
{{ log_dir }}/mavsdk-log
"
,
pubsubWorker
,
worker
,
user_script
=
scriptArgs
[
1
],
FPS
=
50
,
// Minimum sampling interval for open62541 monitored items
previous_timestamp
,
can_update
=
false
;
const
URL
=
"
udp://
"
+
configuration
.
autopilotIp
+
"
:7909
"
,
LOG_FILE
=
"
{{ log_dir }}/mavsdk-log
"
;
conf_file
.
close
();
// Use a Worker to ensure the user script
// does not block the main script
...
...
@@ -26,17 +35,14 @@ import { open, exit } from "std";
// Create the update loop in the main script
// to prevent it to finish (and so, exit the quickjs process)
var
pubsubWorker
,
worker
=
new
Worker
(
"
{{ worker_script }}
"
),
user_script
=
scriptArgs
[
1
],
// Minimum sampling interval for open62541 monitored items
FPS
=
50
,
previous_timestamp
,
can_update
=
false
;
worker
=
new
Worker
(
"
{{ worker_script }}
"
);
function
connect
()
{
console
.
log
(
"
Will connect to
"
,
URL
);
exitOnFail
(
start
(
URL
,
LOG_FILE
,
60
),
"
Failed to connect to
"
+
URL
);
function
quit
(
is_a_drone
,
exit_code
)
{
stopPubsub
();
if
(
is_a_drone
)
{
stop
();
}
exit
(
exit_code
);
}
function
exitOnFail
(
ret
,
msg
)
{
...
...
@@ -46,12 +52,9 @@ import { open, exit } from "std";
}
}
function
quit
(
is_a_drone
,
exit_code
)
{
stopPubsub
();
if
(
is_a_drone
)
{
stop
();
}
exit
(
exit_code
);
function
connect
()
{
console
.
log
(
"
Will connect to
"
,
URL
);
exitOnFail
(
start
(
URL
,
LOG_FILE
,
60
),
"
Failed to connect to
"
+
URL
);
}
if
(
configuration
.
isADrone
)
{
...
...
@@ -60,11 +63,11 @@ import { open, exit } from "std";
}
pubsubWorker
=
new
Worker
(
"
{{ pubsub_script }}
"
);
pubsubWorker
.
onmessage
=
function
(
e
)
{
pubsubWorker
.
onmessage
=
function
(
e
)
{
if
(
!
e
.
data
.
publishing
)
{
pubsubWorker
.
onmessage
=
null
;
}
}
}
;
worker
.
postMessage
({
type
:
"
initPubsub
"
});
...
...
@@ -91,7 +94,7 @@ import { open, exit } from "std";
}
function
loop
()
{
let
timestamp
=
Date
.
now
(),
var
timestamp
=
Date
.
now
(),
timeout
;
if
(
can_update
)
{
if
(
FPS
<=
(
timestamp
-
previous_timestamp
))
{
...
...
@@ -117,7 +120,7 @@ import { open, exit } from "std";
}
worker
.
onmessage
=
function
(
e
)
{
let
type
=
e
.
data
.
type
;
var
type
=
e
.
data
.
type
;
if
(
type
===
'
initialized
'
)
{
pubsubWorker
.
postMessage
({
action
:
"
run
"
,
...
...
@@ -141,4 +144,5 @@ import { open, exit } from "std";
quit
(
configuration
.
isADrone
,
1
);
}
};
}(
console
,
setTimeout
,
Worker
));
}(
arm
,
console
,
exit
,
open
,
scriptArgs
,
setTimeout
,
start
,
stop
,
stopPubsub
,
takeOffAndWait
,
Worker
));
software/js-drone/pubsub.js
View file @
63587a63
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */
/*global console, open, runPubsub, Worker*/
import
{
runPubsub
}
from
{{
json_module
.
dumps
(
qjs_wrapper
)
}};
import
{
Worker
}
from
"
os
"
;
import
{
open
}
from
"
std
"
;
const
CONF_PATH
=
{{
json_module
.
dumps
(
configuration
)
}},
PORT
=
"
4840
"
;
(
function
(
console
,
open
,
runPubsub
,
Worker
)
{
"
use strict
"
;
let
parent
=
Worker
.
parent
;
var
CONF_PATH
=
{{
json_module
.
dumps
(
configuration
)
}},
PORT
=
"
4840
"
,
parent
=
Worker
.
parent
,
conf_file
=
open
(
CONF_PATH
,
"
r
"
),
configuration
=
JSON
.
parse
(
conf_file
.
readAsString
());
conf_file
.
close
();
var
conf_file
=
open
(
CONF_PATH
,
"
r
"
);
const
configuration
=
JSON
.
parse
(
conf_file
.
readAsString
());
conf_file
.
close
();
function
handle_msg
(
e
)
{
switch
(
e
.
data
.
action
)
{
function
handle_msg
(
e
)
{
switch
(
e
.
data
.
action
)
{
case
"
run
"
:
runPubsub
(
configuration
.
multicastIp
,
PORT
,
configuration
.
netIf
,
e
.
data
.
id
,
e
.
data
.
interval
,
e
.
data
.
publish
);
runPubsub
(
configuration
.
multicastIp
,
PORT
,
configuration
.
netIf
,
e
.
data
.
id
,
e
.
data
.
interval
,
e
.
data
.
publish
);
parent
.
postMessage
({
running
:
false
});
parent
.
onmessage
=
null
;
break
;
default
:
console
.
log
(
"
Undefined action from parent:
"
,
e
.
data
.
action
);
}
}
}
parent
.
onmessage
=
handle_msg
;
parent
.
onmessage
=
handle_msg
;
}(
console
,
open
,
runPubsub
,
Worker
));
\ No newline at end of file
software/js-drone/worker.js
View file @
63587a63
/* global console, std */
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */
/*global console, getAltitude, getAltitudeRel, getInitialAltitude, getLatitude,
getLongitude, getYaw, execUserScript, initPubsub, isInManualMode, landed,
loiter, setAirspeed, setAltitude, setManualControlInput, setMessage,
setTargetCoordinates, std, triggerParachute, Drone, Worker*/
import
{
Drone
,
triggerParachute
,
...
...
@@ -21,17 +25,18 @@ import {
import
*
as
std
from
"
std
"
;
import
{
Worker
}
from
"
os
"
;
(
function
(
console
,
Worker
)
{
(
function
(
console
,
getAltitude
,
getAltitudeRel
,
getInitialAltitude
,
getLatitude
,
getLongitude
,
getYaw
,
initPubsub
,
isInManualMode
,
landed
,
loiter
,
setAirspeed
,
setAltitude
,
setManualControlInput
,
setMessage
,
setTargetCoordinates
,
std
,
triggerParachute
,
Drone
,
Worker
)
{
// Every script is evaluated per drone
"
use strict
"
;
const
CONF_PATH
=
{{
json_module
.
dumps
(
configuration
)
}},
drone_dict
=
{};
var
conf_file
=
std
.
open
(
CONF_PATH
,
"
r
"
);
const
configuration
=
JSON
.
parse
(
conf_file
.
readAsString
());
conf_file
.
close
();
let
parent
=
Worker
.
parent
,
var
CONF_PATH
=
{{
json_module
.
dumps
(
configuration
)
}},
conf_file
=
std
.
open
(
CONF_PATH
,
"
r
"
),
configuration
=
JSON
.
parse
(
conf_file
.
readAsString
()),
parent
=
Worker
.
parent
,
user_me
=
{
//for debugging purpose
fdopen
:
std
.
fdopen
,
...
...
@@ -39,12 +44,12 @@ import { Worker } from "os";
//required to fly
triggerParachute
:
triggerParachute
,
drone_dict
:
{},
exit
:
function
(
exit_code
)
{
exit
:
function
(
exit_code
)
{
parent
.
postMessage
({
type
:
"
exited
"
,
exit
:
exit_code
});
parent
.
onmessage
=
null
;
},
getAltitudeAbs
:
getAltitude
,
getCurrentPosition
:
function
()
{
getCurrentPosition
:
function
()
{
return
{
x
:
getLatitude
(),
y
:
getLongitude
(),
...
...
@@ -56,25 +61,25 @@ import { Worker } from "os";
id
:
configuration
.
id
,
landed
:
landed
,
loiter
:
loiter
,
sendMsg
:
function
(
msg
,
id
=
-
1
)
{
sendMsg
:
function
(
msg
,
id
)
{
if
(
id
===
undefined
)
{
id
=
-
1
;
}
setMessage
(
JSON
.
stringify
({
content
:
msg
,
dest_id
:
id
}));
},
setAirspeed
:
setAirspeed
,
setAltitude
:
setAltitude
,
setTargetCoordinates
:
setTargetCoordinates
};
conf_file
.
close
();
function
loadUserScript
(
path
)
{
let
script_content
=
std
.
loadFile
(
path
);
var
script_content
=
std
.
loadFile
(
path
);
if
(
script_content
===
null
)
{
console
.
log
(
"
Failed to load user script
"
+
path
);
std
.
exit
(
1
);
}
try
{
std
.
evalScript
(
"
function execUserScript(from, me) {
"
+
script_content
+
"
};
"
"
function execUserScript(from, me) {
"
+
script_content
+
"
};
"
);
}
catch
(
e
)
{
console
.
log
(
"
Failed to evaluate user script
"
,
e
);
...
...
@@ -102,19 +107,16 @@ import { Worker } from "os";
loadUserScript
(
evt
.
data
.
path
);
parent
.
postMessage
({
type
:
"
loaded
"
});
}
else
if
(
type
===
"
update
"
)
{
for
(
const
[
id
,
drone
]
of
Object
.
entries
(
user_me
.
drone_dict
)
)
{
message
=
drone
.
message
if
(
message
.
length
>
0
)
{
Object
.
entries
(
user_me
.
drone_dict
).
forEach
(
function
([
id
,
drone
]
)
{
message
=
drone
.
message
;
if
(
user_me
.
id
!==
Number
(
id
)
&&
message
.
length
>
0
)
{
message
=
JSON
.
parse
(
message
);
if
(
user_me
.
id
===
id
)
{
continue
;
}
if
(
user_me
.
hasOwnProperty
(
"
onGetMsg
"
)
&&
[
-
1
,
user_me
.
id
].
includes
(
message
.
dest_id
))
{
user_me
.
onGetMsg
(
message
.
content
);
}
}
}
}
);
// Call the drone onStart function
if
(
user_me
.
hasOwnProperty
(
"
onUpdate
"
))
{
if
(
configuration
.
isADrone
&&
isInManualMode
())
{
...
...
@@ -138,4 +140,7 @@ import { Worker } from "os";
std
.
exit
(
1
);
}
};
}(
console
,
Worker
));
}(
console
,
getAltitude
,
getAltitudeRel
,
getInitialAltitude
,
getLatitude
,
getLongitude
,
getYaw
,
initPubsub
,
isInManualMode
,
landed
,
loiter
,
setAirspeed
,
setAltitude
,
setManualControlInput
,
setMessage
,
setTargetCoordinates
,
std
,
triggerParachute
,
Drone
,
Worker
));
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment