package zeppelin;_x000D_
import server.SendToClient;_x000D_
import transfer.Transfer;_x000D_
_x000D_
import com.pi4j.io.gpio.GpioController;_x000D_
import com.pi4j.io.gpio.GpioPinDigitalOutput;_x000D_
import com.pi4j.io.gpio.GpioPinPwmOutput;_x000D_
import com.pi4j.io.gpio.Pin;_x000D_
import com.pi4j.io.gpio.PinState;_x000D_
_x000D_
_x000D_
public class Motor {_x000D_
	private GpioPinDigitalOutput forwardPin;_x000D_
	private GpioPinDigitalOutput reversePin;_x000D_
	private GpioPinPwmOutput pwmPin;_x000D_
_x000D_
	//PWM: value tussen 0 en 1024 (volgens WiringPi)_x000D_
	//of mss tussen 0 en 100 (dit is bij SoftPWM)_x000D_
	//evt een aparte klasse pwm control die gedeeld tussen alle motoren_x000D_
	private boolean pwmEnabled;_x000D_
_x000D_
	private GpioController gpiocontroller;_x000D_
	private Propellor id;_x000D_
_x000D_
	//previous mode and direction ==> avoids sending the same transfer twice_x000D_
	private Propellor.Mode prevmode = Propellor.Mode.OFF;_x000D_
	private Propellor.Direction prevdirection;_x000D_
_x000D_
	private SendToClient sender;_x000D_
_x000D_
	public Motor(Pin fwPin, Pin rvPin, GpioController gpio,Propellor id, GpioPinPwmOutput pwmPin, SendToClient sender) {_x000D_
		gpiocontroller = gpio;_x000D_
		forwardPin = gpiocontroller.provisionDigitalOutputPin(fwPin,"forward");_x000D_
		reversePin = gpiocontroller.provisionDigitalOutputPin(rvPin,"backward");_x000D_
		this.pwmPin = pwmPin; _x000D_
		this.id = id;_x000D_
		this.sender = sender;_x000D_
_x000D_
		//status wanneer de app wordt afgesloten_x000D_
		forwardPin.setShutdownOptions(true,PinState.LOW);_x000D_
		reversePin.setShutdownOptions(true,PinState.LOW);_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Laat deze motor naar voor draaien._x000D_
	 */_x000D_
	public void setForward() {_x000D_
		reversePin.setState(PinState.LOW);_x000D_
		forwardPin.setState(PinState.HIGH);_x000D_
_x000D_
		if(prevdirection != Propellor.Direction.FORWARD || prevmode != Propellor.Mode.ON) {_x000D_
			Transfer transfer = new Transfer();_x000D_
			transfer.setPropellor(id, Propellor.Mode.ON, Propellor.Direction.FORWARD, 0);_x000D_
			sender.sendTransfer(transfer);_x000D_
_x000D_
			prevmode = Propellor.Mode.ON;_x000D_
			prevdirection = Propellor.Direction.FORWARD;_x000D_
		}_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Laat deze motor naar voor draaien._x000D_
	 * Stuurt geen update_x000D_
	 */_x000D_
	private void fw() {_x000D_
		reversePin.setState(PinState.LOW);_x000D_
		forwardPin.setState(PinState.HIGH);_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Laat deze motor naar achter draaien._x000D_
	 */_x000D_
	public void setReverse() {_x000D_
		forwardPin.setState(PinState.LOW);_x000D_
		reversePin.setState(PinState.HIGH);_x000D_
		_x000D_
		if(prevdirection != Propellor.Direction.REVERSE || prevmode != Propellor.Mode.ON) {_x000D_
			Transfer transfer = new Transfer();_x000D_
			transfer.setPropellor(id, Propellor.Mode.ON, Propellor.Direction.REVERSE, 0);_x000D_
			sender.sendTransfer(transfer);_x000D_
_x000D_
			prevmode = Propellor.Mode.ON;_x000D_
			prevdirection = Propellor.Direction.REVERSE;_x000D_
		}_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Laat deze motor naar achter draaien._x000D_
	 * Stuurt geen update_x000D_
	 */_x000D_
	private void rv() {_x000D_
		forwardPin.setState(PinState.LOW);_x000D_
		reversePin.setState(PinState.HIGH);_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Zet deze motor uit._x000D_
	 */_x000D_
	public void setOff() {_x000D_
		reversePin.setState(PinState.LOW);_x000D_
		forwardPin.setState(PinState.LOW);_x000D_
		_x000D_
		if(prevmode != Propellor.Mode.OFF) {_x000D_
			Transfer transfer = new Transfer();_x000D_
			transfer.setPropellor(id, Propellor.Mode.OFF, null, 0);_x000D_
			sender.sendTransfer(transfer);_x000D_
_x000D_
			prevmode = Propellor.Mode.OFF;_x000D_
			prevdirection = null;_x000D_
		}_x000D_
	}_x000D_
	_x000D_
	/**_x000D_
	 * Zet deze motor uit._x000D_
	 * Stuurt geen update_x000D_
	 */_x000D_
	public void off() {_x000D_
		reversePin.setState(PinState.LOW);_x000D_
		forwardPin.setState(PinState.LOW);_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Activeert pwm op deze motor_x000D_
	 */_x000D_
	public void PwmOn() {_x000D_
		pwmEnabled = true;_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Deactiveert pwm op deze motor_x000D_
	 * @param set0_x000D_
	 * 		Om aan te geven of de pwm pin op 0 moet worden gezet_x000D_
	 * 		Dus indien geen andere motoren hier nog gebruik van aan het maken zijn._x000D_
	 */_x000D_
	public void PwmOff(boolean set0) {_x000D_
		pwmEnabled = false;_x000D_
		if(set0)_x000D_
			pwmPin.setPwm(0);_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Zet de pwm value en laat de motor in de juiste richting draaien_x000D_
	 * indien pwm geactiveerd is op deze motor_x000D_
	 * @param value_x000D_
	 * 			Getal tussen -1024 (min) en 1024 (max)_x000D_
	 */_x000D_
	public void setPwmValue(int value) {_x000D_
		if(pwmEnabled) {_x000D_
			if(value > 0){_x000D_
				pwmPin.setPwm(1024);_x000D_
				fw();_x000D_
				off();_x000D_
				pwmPin.setPwm(value);_x000D_
				fw();_x000D_
			}_x000D_
			else{_x000D_
				pwmPin.setPwm(1024);_x000D_
				rv();_x000D_
				off();_x000D_
				pwmPin.setPwm(Math.abs(value));_x000D_
				rv();_x000D_
			}_x000D_
			_x000D_
			_x000D_
//			Transfer transfer = new Transfer();_x000D_
//			transfer.setPropellor(id, Propellor.Mode.PWM, null, value);_x000D_
//			sender.sendTransfer(transfer);_x000D_
			prevmode = Propellor.Mode.PWM;_x000D_
			_x000D_
		}_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Geeft aan of pwm geactiveerd is voor deze motor._x000D_
	 * @return_x000D_
	 */_x000D_
	public boolean getPwmStatus() {_x000D_
		return pwmEnabled;_x000D_
	}_x000D_
}_x000D_

/**_x000D_
	 * Laat deze motor naar voor draaien._x000D_
	 * Stuurt geen update_x000D_
	 */
package zeppelin;_x000D_
import server.SendToClient;_x000D_
import transfer.Transfer;_x000D_
_x000D_
import com.pi4j.io.gpio.GpioController;_x000D_
import com.pi4j.io.gpio.GpioPinDigitalOutput;_x000D_
import com.pi4j.io.gpio.GpioPinPwmOutput;_x000D_
import com.pi4j.io.gpio.Pin;_x000D_
import com.pi4j.io.gpio.PinState;_x000D_
_x000D_
_x000D_
public class Motor {_x000D_
	private GpioPinDigitalOutput forwardPin;_x000D_
	private GpioPinDigitalOutput reversePin;_x000D_
	private GpioPinPwmOutput pwmPin;_x000D_
_x000D_
	//PWM: value tussen 0 en 1024 (volgens WiringPi)_x000D_
	//of mss tussen 0 en 100 (dit is bij SoftPWM)_x000D_
	//evt een aparte klasse pwm control die gedeeld tussen alle motoren_x000D_
	private boolean pwmEnabled;_x000D_
_x000D_
	private GpioController gpiocontroller;_x000D_
	private Propellor id;_x000D_
_x000D_
	//previous mode and direction ==> avoids sending the same transfer twice_x000D_
	private Propellor.Mode prevmode = Propellor.Mode.OFF;_x000D_
	private Propellor.Direction prevdirection;_x000D_
_x000D_
	private SendToClient sender;_x000D_
_x000D_
	public Motor(Pin fwPin, Pin rvPin, GpioController gpio,Propellor id, GpioPinPwmOutput pwmPin, SendToClient sender) {_x000D_
		gpiocontroller = gpio;_x000D_
		forwardPin = gpiocontroller.provisionDigitalOutputPin(fwPin,"forward");_x000D_
		reversePin = gpiocontroller.provisionDigitalOutputPin(rvPin,"backward");_x000D_
		this.pwmPin = pwmPin; _x000D_
		this.id = id;_x000D_
		this.sender = sender;_x000D_
_x000D_
		//status wanneer de app wordt afgesloten_x000D_
		forwardPin.setShutdownOptions(true,PinState.LOW);_x000D_
		reversePin.setShutdownOptions(true,PinState.LOW);_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Laat deze motor naar voor draaien._x000D_
	 */_x000D_
	public void setForward() {_x000D_
		reversePin.setState(PinState.LOW);_x000D_
		forwardPin.setState(PinState.HIGH);_x000D_
_x000D_
		if(prevdirection != Propellor.Direction.FORWARD || prevmode != Propellor.Mode.ON) {_x000D_
			Transfer transfer = new Transfer();_x000D_
			transfer.setPropellor(id, Propellor.Mode.ON, Propellor.Direction.FORWARD, 0);_x000D_
			sender.sendTransfer(transfer);_x000D_
_x000D_
			prevmode = Propellor.Mode.ON;_x000D_
			prevdirection = Propellor.Direction.FORWARD;_x000D_
		}_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Laat deze motor<SUF>*/_x000D_
	private void fw() {_x000D_
		reversePin.setState(PinState.LOW);_x000D_
		forwardPin.setState(PinState.HIGH);_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Laat deze motor naar achter draaien._x000D_
	 */_x000D_
	public void setReverse() {_x000D_
		forwardPin.setState(PinState.LOW);_x000D_
		reversePin.setState(PinState.HIGH);_x000D_
		_x000D_
		if(prevdirection != Propellor.Direction.REVERSE || prevmode != Propellor.Mode.ON) {_x000D_
			Transfer transfer = new Transfer();_x000D_
			transfer.setPropellor(id, Propellor.Mode.ON, Propellor.Direction.REVERSE, 0);_x000D_
			sender.sendTransfer(transfer);_x000D_
_x000D_
			prevmode = Propellor.Mode.ON;_x000D_
			prevdirection = Propellor.Direction.REVERSE;_x000D_
		}_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Laat deze motor naar achter draaien._x000D_
	 * Stuurt geen update_x000D_
	 */_x000D_
	private void rv() {_x000D_
		forwardPin.setState(PinState.LOW);_x000D_
		reversePin.setState(PinState.HIGH);_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Zet deze motor uit._x000D_
	 */_x000D_
	public void setOff() {_x000D_
		reversePin.setState(PinState.LOW);_x000D_
		forwardPin.setState(PinState.LOW);_x000D_
		_x000D_
		if(prevmode != Propellor.Mode.OFF) {_x000D_
			Transfer transfer = new Transfer();_x000D_
			transfer.setPropellor(id, Propellor.Mode.OFF, null, 0);_x000D_
			sender.sendTransfer(transfer);_x000D_
_x000D_
			prevmode = Propellor.Mode.OFF;_x000D_
			prevdirection = null;_x000D_
		}_x000D_
	}_x000D_
	_x000D_
	/**_x000D_
	 * Zet deze motor uit._x000D_
	 * Stuurt geen update_x000D_
	 */_x000D_
	public void off() {_x000D_
		reversePin.setState(PinState.LOW);_x000D_
		forwardPin.setState(PinState.LOW);_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Activeert pwm op deze motor_x000D_
	 */_x000D_
	public void PwmOn() {_x000D_
		pwmEnabled = true;_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Deactiveert pwm op deze motor_x000D_
	 * @param set0_x000D_
	 * 		Om aan te geven of de pwm pin op 0 moet worden gezet_x000D_
	 * 		Dus indien geen andere motoren hier nog gebruik van aan het maken zijn._x000D_
	 */_x000D_
	public void PwmOff(boolean set0) {_x000D_
		pwmEnabled = false;_x000D_
		if(set0)_x000D_
			pwmPin.setPwm(0);_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Zet de pwm value en laat de motor in de juiste richting draaien_x000D_
	 * indien pwm geactiveerd is op deze motor_x000D_
	 * @param value_x000D_
	 * 			Getal tussen -1024 (min) en 1024 (max)_x000D_
	 */_x000D_
	public void setPwmValue(int value) {_x000D_
		if(pwmEnabled) {_x000D_
			if(value > 0){_x000D_
				pwmPin.setPwm(1024);_x000D_
				fw();_x000D_
				off();_x000D_
				pwmPin.setPwm(value);_x000D_
				fw();_x000D_
			}_x000D_
			else{_x000D_
				pwmPin.setPwm(1024);_x000D_
				rv();_x000D_
				off();_x000D_
				pwmPin.setPwm(Math.abs(value));_x000D_
				rv();_x000D_
			}_x000D_
			_x000D_
			_x000D_
//			Transfer transfer = new Transfer();_x000D_
//			transfer.setPropellor(id, Propellor.Mode.PWM, null, value);_x000D_
//			sender.sendTransfer(transfer);_x000D_
			prevmode = Propellor.Mode.PWM;_x000D_
			_x000D_
		}_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Geeft aan of pwm geactiveerd is voor deze motor._x000D_
	 * @return_x000D_
	 */_x000D_
	public boolean getPwmStatus() {_x000D_
		return pwmEnabled;_x000D_
	}_x000D_
}_x000D_
