Skip to content

Instantly share code, notes, and snippets.

@NanneHuiges
Created June 1, 2017 06:29
Show Gist options
  • Save NanneHuiges/e856f8f26c22c19025aadcc314ad5117 to your computer and use it in GitHub Desktop.
Save NanneHuiges/e856f8f26c22c19025aadcc314ad5117 to your computer and use it in GitHub Desktop.
Piters versie van de Controller, waar de default-loop logica uitgecomment is
/*
* Copyright (C) 2017 Ordina
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.ordina.jtech.hackadrone.io;
import nl.ordina.jtech.hackadrone.models.Command;
import nl.ordina.jtech.hackadrone.net.CommandConnection;
import java.io.IOException;
/**
* Class representing the controller for a drone.
*
* @author Nils Berlijn
* @version 1.0
* @since 1.0
*/
public final class Controller extends Thread implements CommandListener {
/**
* The device to control the drone with.
*/
private final Device device;
/**
* The command connection with the drone.
*/
private final CommandConnection commandConnection;
/**
* The command.
*/
private Command command = new Command();
private Command prevCommand = null;
/**
* A controller constructor
*
* @param device the device to control the drone with
* @param commandConnection the command connection with the drone
*/
public Controller(Device device, CommandConnection commandConnection) {
this.device = device;
this.commandConnection = commandConnection;
}
/**
* Interrupts the controller.
*/
@Override
public void interrupt() {
device.setListener(null);
device.stop();
super.interrupt();
}
/**
* Starts running the controller.
*/
@Override
public void run() {
device.setListener(this);
device.start();
try {
Command myCommand = new Command();
myCommand.setTakeOff(true);
commandConnection.sendCommand(myCommand);
myCommand.setTakeOff(false);
for (int i=0; i<50;i++) {
myCommand.setYaw(i*50);
commandConnection.sendCommand(myCommand);
try {
Thread.sleep(100);
} catch (Exception e) {
}
myCommand.setPitch(75);
commandConnection.sendCommand(myCommand);
// myCommand.setThrottle(75);
// commandConnection.sendCommand(myCommand);
}
for (int i=0; i<50;i++) {
myCommand.setYaw(-i*50);
commandConnection.sendCommand(myCommand);
try {
Thread.sleep(100);
} catch (Exception e) {
}
myCommand.setPitch(75);
commandConnection.sendCommand(myCommand);
// myCommand.setThrottle(75);
// commandConnection.sendCommand(myCommand);
}
myCommand.setThrottle(0);
myCommand.setPitch(0);
myCommand.setLand(true);
commandConnection.sendCommand(myCommand);
} catch (Exception e) {
}
// while (!isInterrupted()) {
// try {
// if (command != prevCommand) {
// System.out.println("New command: " + command);
// commandConnection.sendCommand(command);
// prevCommand = command;
// }
// Thread.sleep(50);
// } catch (IOException e) {
// System.err.println("Unable to send command");
// } catch (InterruptedException e) {
// System.err.println("Command interrupted");
// }
// }
}
/**
* Handles the received command.
*
* @param command the command to handle
*/
@Override
public void onCommandReceived(Command command) {
if (command == null) {
this.command = new Command();
} else {
this.command = command;
}
}
}
@NanneHuiges
Copy link
Author

Dit is dus de versie die Piter (Hoekstra) geschreven heeft, maar door ruzie met newlines en github was het even handiger dat ik hem zou plaatsen. Hierbij dus :)

@nberlijn
Copy link

nberlijn commented Jun 1, 2017

Bedankt voor het delen van de code! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment