xxxxxxxxxx
246
var wifiName = "fablab-droner";
var wifiPassword = "39nerdro";
let colour // Instructions: Attach a PS3 controller and press the play button.
// PS3 Gamepad Demo.
let controllers = []
var speedX = 0;
var speedY = 0;
var speedZ = 0;
function setup() {
frameRate(60);
colour=color('green')
createCanvas(8000, 512)
background(120,0,0)
noStroke()
window.addEventListener("gamepadconnected", function(e) {
gamepadHandler(e, true);
console.log("Gamepad connected at index %d: %s. %d buttons, %d axes.",
e.gamepad.index, e.gamepad.id,
e.gamepad.buttons.length, e.gamepad.axes.length);
});
window.addEventListener("gamepaddisconnected", function(e) {
console.log("Gamepad disconnected from index %d: %s",
e.gamepad.index, e.gamepad.id);
colour=color(120,0,0)
gamepadHandler(e, false);
});
}
frameRate(30);
const canvas = createCanvas(windowWidth, windowHeight);
textFont('Share Tech Mono');
WebSocketStart("ws://localhost:9000");
drones.push(new Drone("192.168.0.151"));
function draw() {
// droneCmd("rc 0 speedX 0 speedY")
background(colour)
drawGamepad()
status()
}
function status()
{
color('cyan')
ellipse(10,300,10,10)
}
function gamepadHandler(event, connecting) {
let gamepad = event.gamepad;
if (connecting) {
print("Connecting to controller "+gamepad.index)
controllers[gamepad.index] = gamepad
} else {
delete controllers[gamepad.index]
}
}
function drawGamepad()
{
let dx=20
let dy=100
let dj=40
var gamepads = navigator.getGamepads()
for (let i in controllers)
{
translate(0,dy,0)
push()
let controller=gamepads[i]//controllers[i]
if (controller.buttons)
{
for (let btn=0;btn<controller.buttons.length;btn++)
{
let val=controller.buttons[btn]
strokeWeight(2)
stroke('white')
if (buttonPressed(val))
{
fill('green')
stroke('white')
}
else
{
fill('red')
stroke('grey')
}
ellipse(btn*dx+dx,i*dx+dx,dx*0.8)
}
}
if (controller.axes)
{
let axes=controller.axes
for (let axis=0;axis<axes.length;axis++)
{
let val=controller.axes[axis]
translate(dj,0)
fill('grey')
stroke('white')
rect(0,dx*2,dx,dx*4)
noStroke()
fill('yellow')
rect(0,(val*dx*2)+1+dx*4, dx,dx/4)
fill('white')
text(nf(val,0,2),0,dx*8)
}
}
pop()
}
}
function buttonPressed(b) {
if (typeof(b) == "object") {
return b.pressed; // binary
}
return b > 0.9; // analog value
background(0);
drawUI();
if (uiButton("Do drone show", color(255, 255, 255), 200, 30, 40, 30).clicked) {
droneCmd("takeoff", true);
droneCmd("mon", true);
droneCmd("go 0 0 100 50 m-1", true);
droneCmd("forward 100", true);
droneCmd("go 0 0 0 50 m-7", true);
droneCmd("back 100", true);
droneCmd("go 0 0 0 50 m-1", true);
droneCmd("land", true);
/* droneCmd("takeoff", true);
droneCmd("right 30", true);
droneCmd("left 30", true);
droneCmd("land", true);*/
}
/* if (uiButton(">>", color(255, 255, 255), 200, 30, 300, 300).clicked) {
droneCmd("right 30", true);
}*/
/*if (keyIsPressed == true && key == 'b') {
droneCmd("left 30", true);
}*/
//fill(255,255,255);
//ellipse(200,100,200,200);
/* if (uiButton("go left", color(0, 0, 0,0), 200, 200, 200, 30).clicked) {
droneCmd("left 30", true);
}*/
customUI();
} // afslutnign på draw funktionen
function customUI() {
uiContainerStart(600, 100, 400);
uiText("Custom UI");
uiText("Y: " + speedY);
uiText("X: " + speedX);
uiContainerEnd();
// kald metoden:
listenToKeyboard();
}
function listenToKeyboard() {
speedX = 0;
speedY = 0;
speedZ = 0;
if (keyIsPressed === true && key === 'w') {
speedX = 100;
}
if (keyIsPressed === true && key === 'a') {
speedY = -100;
}
if (keyIsPressed === true && key === 's') {
speedX = -100;
}
if (keyIsPressed === true && key === 'd') {
speedY = 100;
}
/* if (keyIsPressed === true && key === 'j') {
speedZ = 100;
}
*/
//position
//var command = "rc " + speedY + " " + speedX + " " + 0 + " " + 0;
// droneCmd(command, false)
//drejning
var command = "rc " + 0 + " " + speedX + " " + 0 + " " + speedY + " ";
if (drones[0].queue.length < 1) {
droneCmd(command, false)
}
}
function mousePressed() {
}
function keyReleased() {
if (key == 'q') {
droneCmd("left 30", true);
}
if (key == 'f') {
var fs = fullscreen();
fullscreen(!fs);
}
}