• #by-own #raspberrypi #developRobot

Kapitel 15: Datenmodelle

Nun werden noch Klassen benötigt um die Sensor Daten abspeichern zu können, Tastendrücke und dessen Status festzuhalten und ein Datenmodell zum Senden von Websocket-Nachrichten an das Frontend der Webseite.

Die Klasse KeyMap bekommt die Tastendrücke mitgeteilt und setzt entsprechenden Befhel, hier findet also das Mapping von der Taste W zu den Befehl FORWARD statt.

package diy.robot.domain;

import diy.robot.websocketClients.StompWebSocketClient;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
@Slf4j
public class KeyMap {

	private static HashMap<String, Boolean> keyStatus = new HashMap<>();

	private static Direction direction = Direction.STOP;

	private static RobotArmCommand robotArmCommand = RobotArmCommand.STOP;
	private static int robotArmMotor = 0;

	private static LASER laser = LASER.OFF;

	public KeyMap() {
		// RobotMoveMotor
		keyStatus.put("W", false);
		keyStatus.put("A", false);
		keyStatus.put("S", false);
		keyStatus.put("D", false);
		// RobotArm
		keyStatus.put("1", false);
		keyStatus.put("2", false);
		keyStatus.put("3", false);
		keyStatus.put("4", false);
		keyStatus.put("5", false);
		keyStatus.put("6", false);
		keyStatus.put("+", false);
		keyStatus.put("-", false);
		keyStatus.put("I", false);
		// Laser
		keyStatus.put("E", false);
	}

	public static void setKey(String key, Boolean pressed) {
		keyStatus.put(key, pressed);
		calcDirection();
		try {
			StompWebSocketClient.sendStompMessage(new SocketMessage("direction:" + direction.toString()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void calcDirection() {
		if (keyStatus.get("W") && !keyStatus.get("A") && !keyStatus.get("S") && !keyStatus.get("D")) {
			direction = Direction.FORWARD;
		} else if (!keyStatus.get("W") && keyStatus.get("A") && !keyStatus.get("S") && !keyStatus.get("D")) {
			direction = Direction.LEFT;
		} else if (!keyStatus.get("W") && !keyStatus.get("A") && keyStatus.get("S") && !keyStatus.get("D")) {
			direction = Direction.BACKWARD;
		} else if (!keyStatus.get("W") && !keyStatus.get("A") && !keyStatus.get("S") && keyStatus.get("D")) {
			direction = Direction.RIGHT;
		} else if (keyStatus.get("W") && keyStatus.get("A") && !keyStatus.get("S") && !keyStatus.get("D")) {
			direction = Direction.CURVELEFTFORWARD;
		} else if (keyStatus.get("W") && !keyStatus.get("A") && !keyStatus.get("S") && keyStatus.get("D")) {
			direction = Direction.CURVERIGHTFORWARD;
		} else if (!keyStatus.get("W") && keyStatus.get("A") && keyStatus.get("S") && !keyStatus.get("D")) {
			direction = Direction.CURVELEFTBACKWARD;
		} else if (!keyStatus.get("W") && !keyStatus.get("A") && keyStatus.get("S") && keyStatus.get("D")) {
			direction = Direction.CURVERIGHBACKWARD;
		} else if (!keyStatus.get("W") && !keyStatus.get("A") && !keyStatus.get("S") && !keyStatus.get("D")) {
			direction = Direction.STOP;
		} else {
			direction = Direction.STOP;
		}

		if (keyStatus.get("1") && keyStatus.get("+")) {
			robotArmCommand = RobotArmCommand.CLOCKWISE;
			robotArmMotor = 0;
		} else if (keyStatus.get("1") && keyStatus.get("-")) {
			robotArmCommand = RobotArmCommand.ANTICLOCKWISE;
			robotArmMotor = 0;

		} else if (keyStatus.get("2") && keyStatus.get("+")) {
			robotArmCommand = RobotArmCommand.CLOCKWISE;
			robotArmMotor = 1;
		} else if (keyStatus.get("2") && keyStatus.get("-")) {
			robotArmCommand = RobotArmCommand.ANTICLOCKWISE;
			robotArmMotor = 1;

		} else if (keyStatus.get("3") && keyStatus.get("+")) {
			robotArmCommand = RobotArmCommand.CLOCKWISE;
			robotArmMotor = 2;
		} else if (keyStatus.get("3") && keyStatus.get("-")) {
			robotArmCommand = RobotArmCommand.ANTICLOCKWISE;
			robotArmMotor = 2;

		} else if (keyStatus.get("4") && keyStatus.get("+")) {
			robotArmCommand = RobotArmCommand.CLOCKWISE;
			robotArmMotor = 3;
		} else if (keyStatus.get("4") && keyStatus.get("-")) {
			robotArmCommand = RobotArmCommand.ANTICLOCKWISE;
			robotArmMotor = 3;

		} else if (keyStatus.get("5") && keyStatus.get("+")) {
			robotArmCommand = RobotArmCommand.CLOCKWISE;
			robotArmMotor = 4;
		} else if (keyStatus.get("5") && keyStatus.get("-")) {
			robotArmCommand = RobotArmCommand.ANTICLOCKWISE;
			robotArmMotor = 4;

		} else if (keyStatus.get("6") && keyStatus.get("+")) {
			robotArmCommand = RobotArmCommand.CLOCKWISE;
			robotArmMotor = 5;
		} else if (keyStatus.get("6") && keyStatus.get("-")) {
			robotArmCommand = RobotArmCommand.ANTICLOCKWISE;
			robotArmMotor = 5;
		} else {
			robotArmCommand = RobotArmCommand.STOP;
		}

		if (keyStatus.get("I")) {
			robotArmCommand = RobotArmCommand.INIT;
			robotArmMotor = 0;
		}

		if (keyStatus.get("E")) {
			laser = LASER.ON;
		} else {
			laser = LASER.OFF;
		}
	}

	public static Direction getDirection() {
		return direction;
	}

	public static RobotArmCommand getRobotArmCommand() {
		return robotArmCommand;
	}

	public static int getRobotArmMotor() {
		return robotArmMotor;
	}

	public static LASER getLaser() {
		return laser;
	}

	public enum Direction {
		FORWARD, BACKWARD, RIGHT, LEFT, CURVERIGHTFORWARD, CURVERIGHBACKWARD, CURVELEFTBACKWARD, CURVELEFTFORWARD, STOP
	}

	public enum RobotArmCommand {
		// TODO Tatsnekomni 1-6, dann mit + und - motoren kombinieren
		INIT, CLOCKWISE, ANTICLOCKWISE, STOP
	}

	public enum LASER {
		ON, OFF
	}

}

In der Sensor Klasse werden alle Daten von den Sensoren gehalten. Dies ist auch wichtig für die evtl. späteren Autonomen  Funktionen, um so in Java immer auf die aktuellen Sensorwerte zugreifen zu können.

package diy.robot.domain;

import diy.robot.websocketClients.StompWebSocketClient;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Sensors {

	private static double ultrasonicFront = 0.0;

	private static double ultrasonicBack = 0.0;

	private static boolean lightSensor = false;

	private static String rfidSensor = "";

	public static void setSensorValueAutmaticale(String sensor) {
		String[] cursensor = sensor.split(":");
		switch (cursensor[0]) {
		case "ultrasonicFront":
			ultrasonicFront = Double.parseDouble(cursensor[1]);
			try {
				StompWebSocketClient.sendStompMessage(new SocketMessage("ultrasonicFront:" + ultrasonicFront));
			} catch (Exception e) {
				// TODO Auto-generated catch block meine jasdnjkasdhfjk
				log.error(e.getMessage());
			}
			break;
		case "ultrasonicBack":
			ultrasonicBack = Double.parseDouble(cursensor[1]);
			try {
				StompWebSocketClient.sendStompMessage(new SocketMessage("ultrasonicBack:" + ultrasonicBack));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				log.error(e.getMessage());
			}
			break;
		case "lightSensor":
			lightSensor = cursensor[1].equals("0");
			try {
				StompWebSocketClient.sendStompMessage(new SocketMessage("lightSensor:" + lightSensor));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				log.error(e.getMessage());
			}
		case "rfidSensor":
			rfidSensor = cursensor[1];
			try {
				StompWebSocketClient.sendStompMessage(new SocketMessage("rfidSensor:" + rfidSensor));
			} catch (Exception e) {
				log.error(e.getMessage());
			}
			break;
		default:
			log.info("Sensor Value Recieved and seted: " + sensor);
		}
	}

	public static double getUltrasonicFront() {
		return ultrasonicFront;
	}

	public static void setUltrasonicFront(double ultrasonicFront) {
		Sensors.ultrasonicFront = ultrasonicFront;
	}

	public static double getUltrasonicBack() {
		return ultrasonicBack;
	}

	public static void setUltrasonicBack(double ultrasonicBack) {
		Sensors.ultrasonicBack = ultrasonicBack;
	}

	public static boolean isLightSensor() {
		return lightSensor;
	}

	public static void setLightSensor(boolean lightSensor) {
		Sensors.lightSensor = lightSensor;
	}

}

 Als letztes Datenmodell wird noch das Format von den Websocket-Nachrichten definiert, welche später an die Webseite und somit den Clients gesendet wird. Dabei handelt es sich um eine sehr einfache POJO Klasse.

package diy.robot.domain;

public class SocketMessage {

	private String content;

	public SocketMessage() {
	}

	public SocketMessage(String content) {
		this.content = content;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

}

 

© 2019 by-own
Wir benutzen Cookies

Wir nutzen Cookies auf unserer Website. Einige von ihnen sind essenziell für den Betrieb der Seite, während andere uns helfen, diese Website und die Nutzererfahrung zu verbessern (Tracking Cookies). Sie können selbst entscheiden, ob Sie die Cookies zulassen möchten. Bitte beachten Sie, dass bei einer Ablehnung womöglich nicht mehr alle Funktionalitäten der Seite zur Verfügung stehen.