Skip to content

Commit

Permalink
[69] more removal
Browse files Browse the repository at this point in the history
  • Loading branch information
mirage22 committed Oct 9, 2024
1 parent 2a965c0 commit 24d7e21
Show file tree
Hide file tree
Showing 9 changed files with 557 additions and 562 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
import com.robo4j.hw.lego.enums.SensorTypeEnum;
import com.robo4j.hw.lego.provider.SensorProvider;
import com.robo4j.hw.lego.wrapper.SensorWrapper;
import com.robo4j.logging.SimpleLoggingUtil;
import com.robo4j.units.lego.infra.InfraSensorEnum;
import com.robo4j.units.lego.infra.InfraSensorMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -40,13 +41,13 @@
* @author Miroslav Wengner (@miragemiko)
*/
public class InfraSensorUnit extends RoboUnit<String> {

public static final String PROPERTY_SENSOR_PORT = "sensorPort";
public static final String PROPERTY_TARGET = "target";
public static final String PROPERTY_SCAN_INIT_DELAY = "scanInitDelay";
public static final String PROPERTY_SCAN_PERIOD = "scanPeriod";
public static final int VALUE_SCAN_INIT_DELAY = 1000;
public static final int VALUE_SCAN_PERIOD = 800;
private static final Logger LOGGER = LoggerFactory.getLogger(InfraSensorUnit.class);
private volatile AtomicBoolean active = new AtomicBoolean();
private ILegoSensor sensor;
private String target;
Expand Down Expand Up @@ -93,7 +94,7 @@ private void processMessage(String message){
stopMeasurement();
break;
default:
SimpleLoggingUtil.error(getClass(), String.format("not supported value: %s", message));
LOGGER.warn("not supported value: {}", message);
}
}

Expand Down
268 changes: 132 additions & 136 deletions robo4j-units-lego/src/main/java/com/robo4j/units/lego/SimpleTankUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
import com.robo4j.hw.lego.enums.MotorTypeEnum;
import com.robo4j.hw.lego.provider.MotorProvider;
import com.robo4j.hw.lego.wrapper.MotorWrapper;
import com.robo4j.logging.SimpleLoggingUtil;
import com.robo4j.units.lego.enums.MotorRotationEnum;
import com.robo4j.units.lego.platform.LegoPlatformMessage;
import com.robo4j.units.lego.utils.LegoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
Expand All @@ -42,139 +43,134 @@
*/
public class SimpleTankUnit extends AbstractMotorUnit<LegoPlatformMessage> implements RoboReference<LegoPlatformMessage> {

public static final String PROPERTY_SPEED = "speed";
public static final String PROPERTY_LEFT_MOTOR_PORT = "leftMotorPort";
public static final String PROPERTY_LEFT_MOTOR_TYPE = "leftMotorType";
public static final String PROPERTY_RIGHT_MOTOR_PORT = "rightMotorPort";
public static final String PROPERTY_RIGHT_MOTOR_TYPE = "rightMotorType";
/* test visible */
protected volatile ILegoMotor rightMotor;
protected volatile ILegoMotor leftMotor;
private volatile int speed;


public SimpleTankUnit(RoboContext context, String id) {
super(LegoPlatformMessage.class, context, id);
}

/**
*
* @param message
* the message received by this unit.
*/
@Override
public void onMessage(LegoPlatformMessage message) {
processPlatformMessage(message);
}

@Override
public void shutdown() {
setState(LifecycleState.SHUTTING_DOWN);
rightMotor.close();
leftMotor.close();
setState(LifecycleState.SHUTDOWN);
}

/**
*
* @param configuration
* the {@link Configuration} provided.
* @throws ConfigurationException
* exception
*/
@Override
protected void onInitialization(Configuration configuration) throws ConfigurationException {
setState(LifecycleState.UNINITIALIZED);
speed = configuration.getInteger(PROPERTY_SPEED, LegoPlatformMessage.DEFAULT_SPEED);
String leftMotorPort = configuration.getString(PROPERTY_LEFT_MOTOR_PORT, AnalogPortEnum.B.getType());
Character leftMotorType = configuration.getCharacter(PROPERTY_LEFT_MOTOR_TYPE, MotorTypeEnum.NXT.getType());
String rightMotorPort = configuration.getString(PROPERTY_RIGHT_MOTOR_PORT, AnalogPortEnum.C.getType());
Character rightMotorType = configuration.getCharacter(PROPERTY_RIGHT_MOTOR_TYPE, MotorTypeEnum.NXT.getType());

MotorProvider motorProvider = new MotorProvider();
rightMotor = new MotorWrapper<>(motorProvider, AnalogPortEnum.getByType(rightMotorPort),
MotorTypeEnum.getByType(rightMotorType));
rightMotor.setSpeed(speed);
leftMotor = new MotorWrapper<>(motorProvider, AnalogPortEnum.getByType(leftMotorPort),
MotorTypeEnum.getByType(leftMotorType));
leftMotor.setSpeed(speed);

setState(LifecycleState.INITIALIZED);
}



// Private Methods
private void processPlatformMessage(LegoPlatformMessage message) {
switch (message.getType()) {
case STOP:
executeBothEnginesStop(rightMotor, leftMotor);
break;
case MOVE:
executeBothEngines(MotorRotationEnum.FORWARD, rightMotor, leftMotor);
break;
case BACK:
executeBothEngines(MotorRotationEnum.BACKWARD, rightMotor, leftMotor);
break;
case LEFT:
executeTurn(leftMotor, rightMotor);
break;
case RIGHT:
executeTurn(rightMotor, leftMotor);
break;
case SPEED:
checkUpdateSpeed(message);
default:
SimpleLoggingUtil.error(getClass(), message.getType() + " not supported!");
throw new LegoUnitException("PLATFORM COMMAND: " + message);
}
}

private void checkUpdateSpeed(LegoPlatformMessage message) {
if(message.getSpeed() != speed){
speed = message.getSpeed();
rightMotor.setSpeed(speed);
leftMotor.setSpeed(speed);
}
}

private boolean executeTurn(ILegoMotor... motors) {
ILegoMotor rOne = motors[LegoUtils.DEFAULT_0];
ILegoMotor rTwo = motors[LegoUtils.DEFAULT_1];
Future<Boolean> first = runEngine(rOne, MotorRotationEnum.BACKWARD);
Future<Boolean> second = runEngine(rTwo, MotorRotationEnum.FORWARD);
try {
return first.get() && second.get();
} catch (InterruptedException | ExecutionException e) {
throw new LegoUnitException("executeTurnByCycles error: ", e);
}
}

private boolean executeBothEngines(MotorRotationEnum rotation, ILegoMotor... motors) {
Future<Boolean> motorLeft = runEngine(motors[LegoUtils.DEFAULT_0], rotation);
Future<Boolean> motorRight = runEngine(motors[LegoUtils.DEFAULT_1], rotation);

try {
return motorLeft.get() && motorRight.get();
} catch (InterruptedException | ExecutionException e) {
throw new LegoUnitException("BothEnginesByCycles error: ", e);
}
}

private boolean executeBothEnginesStop(ILegoMotor... motors) {
Future<Boolean> motorLeft = executeEngineStop(motors[LegoUtils.DEFAULT_0]);
Future<Boolean> motorRight = executeEngineStop(motors[LegoUtils.DEFAULT_1]);
try {
return motorLeft.get() && motorRight.get();
} catch (InterruptedException | ExecutionException e) {
throw new LegoUnitException("executeBothEnginesStop error: ", e);
}
}

private Future<Boolean> executeEngineStop(ILegoMotor motor) {
return getContext().getScheduler().submit(() -> {
motor.stop();
return motor.isMoving();
});
}
public static final String PROPERTY_SPEED = "speed";
public static final String PROPERTY_LEFT_MOTOR_PORT = "leftMotorPort";
public static final String PROPERTY_LEFT_MOTOR_TYPE = "leftMotorType";
public static final String PROPERTY_RIGHT_MOTOR_PORT = "rightMotorPort";
public static final String PROPERTY_RIGHT_MOTOR_TYPE = "rightMotorType";
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleTankUnit.class);
/* test visible */
protected volatile ILegoMotor rightMotor;
protected volatile ILegoMotor leftMotor;
private volatile int speed;


public SimpleTankUnit(RoboContext context, String id) {
super(LegoPlatformMessage.class, context, id);
}

/**
* @param message the message received by this unit.
*/
@Override
public void onMessage(LegoPlatformMessage message) {
processPlatformMessage(message);
}

@Override
public void shutdown() {
setState(LifecycleState.SHUTTING_DOWN);
rightMotor.close();
leftMotor.close();
setState(LifecycleState.SHUTDOWN);
}

/**
* @param configuration the {@link Configuration} provided.
* @throws ConfigurationException exception
*/
@Override
protected void onInitialization(Configuration configuration) throws ConfigurationException {
setState(LifecycleState.UNINITIALIZED);
speed = configuration.getInteger(PROPERTY_SPEED, LegoPlatformMessage.DEFAULT_SPEED);
String leftMotorPort = configuration.getString(PROPERTY_LEFT_MOTOR_PORT, AnalogPortEnum.B.getType());
Character leftMotorType = configuration.getCharacter(PROPERTY_LEFT_MOTOR_TYPE, MotorTypeEnum.NXT.getType());
String rightMotorPort = configuration.getString(PROPERTY_RIGHT_MOTOR_PORT, AnalogPortEnum.C.getType());
Character rightMotorType = configuration.getCharacter(PROPERTY_RIGHT_MOTOR_TYPE, MotorTypeEnum.NXT.getType());

MotorProvider motorProvider = new MotorProvider();
rightMotor = new MotorWrapper<>(motorProvider, AnalogPortEnum.getByType(rightMotorPort),
MotorTypeEnum.getByType(rightMotorType));
rightMotor.setSpeed(speed);
leftMotor = new MotorWrapper<>(motorProvider, AnalogPortEnum.getByType(leftMotorPort),
MotorTypeEnum.getByType(leftMotorType));
leftMotor.setSpeed(speed);

setState(LifecycleState.INITIALIZED);
}


// Private Methods
private void processPlatformMessage(LegoPlatformMessage message) {
switch (message.getType()) {
case STOP:
executeBothEnginesStop(rightMotor, leftMotor);
break;
case MOVE:
executeBothEngines(MotorRotationEnum.FORWARD, rightMotor, leftMotor);
break;
case BACK:
executeBothEngines(MotorRotationEnum.BACKWARD, rightMotor, leftMotor);
break;
case LEFT:
executeTurn(leftMotor, rightMotor);
break;
case RIGHT:
executeTurn(rightMotor, leftMotor);
break;
case SPEED:
checkUpdateSpeed(message);
default:
LOGGER.error("not supported!:{}", message.getType());
throw new LegoUnitException("PLATFORM COMMAND: " + message);
}
}

private void checkUpdateSpeed(LegoPlatformMessage message) {
if (message.getSpeed() != speed) {
speed = message.getSpeed();
rightMotor.setSpeed(speed);
leftMotor.setSpeed(speed);
}
}

private boolean executeTurn(ILegoMotor... motors) {
ILegoMotor rOne = motors[LegoUtils.DEFAULT_0];
ILegoMotor rTwo = motors[LegoUtils.DEFAULT_1];
Future<Boolean> first = runEngine(rOne, MotorRotationEnum.BACKWARD);
Future<Boolean> second = runEngine(rTwo, MotorRotationEnum.FORWARD);
try {
return first.get() && second.get();
} catch (InterruptedException | ExecutionException e) {
throw new LegoUnitException("executeTurnByCycles error: ", e);
}
}

private boolean executeBothEngines(MotorRotationEnum rotation, ILegoMotor... motors) {
Future<Boolean> motorLeft = runEngine(motors[LegoUtils.DEFAULT_0], rotation);
Future<Boolean> motorRight = runEngine(motors[LegoUtils.DEFAULT_1], rotation);

try {
return motorLeft.get() && motorRight.get();
} catch (InterruptedException | ExecutionException e) {
throw new LegoUnitException("BothEnginesByCycles error: ", e);
}
}

private boolean executeBothEnginesStop(ILegoMotor... motors) {
Future<Boolean> motorLeft = executeEngineStop(motors[LegoUtils.DEFAULT_0]);
Future<Boolean> motorRight = executeEngineStop(motors[LegoUtils.DEFAULT_1]);
try {
return motorLeft.get() && motorRight.get();
} catch (InterruptedException | ExecutionException e) {
throw new LegoUnitException("executeBothEnginesStop error: ", e);
}
}

private Future<Boolean> executeEngineStop(ILegoMotor motor) {
return getContext().getScheduler().submit(() -> {
motor.stop();
return motor.isMoving();
});
}
}
Loading

0 comments on commit 24d7e21

Please sign in to comment.