Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

beginning of servo fixes and servo2 #488

Open
wants to merge 15 commits into
base: develop
from
@@ -25,7 +25,7 @@

public transient final static Logger log = LoggerFactory.getLogger(ArduinoMsgGenerator.class);

static final Integer MRLCOMM_VERSION = 59;
static final Integer MRLCOMM_VERSION = 60;

public void generateDefinitions() throws IOException {
generateDefinitions(new File("src/main/resources/resource/Arduino/generate/arduinoMsgs.schema"));
@@ -51,17 +51,8 @@ public BoardInfo(int version) {
* public boolean isUnknown() { return (boardName == null) ||
* boardName.equals("unknown"); }
*/
/*
*
* public void setType(int boardId) { this.boardId = boardId; switch (boardId)
* { case Arduino.BOARD_TYPE_ID_MEGA: boardName = Arduino.BOARD_TYPE_MEGA;
* break; case Arduino.BOARD_TYPE_ID_UNO: boardName = Arduino.BOARD_TYPE_UNO;
* break; case Arduino.BOARD_TYPE_ID_ADK_MEGA: boardName =
* Arduino.BOARD_TYPE_MEGA_ADK; break; case Arduino.BOARD_TYPE_ID_NANO:
* boardName = Arduino.BOARD_TYPE_NANO; break; case
* Arduino.BOARD_TYPE_ID_PRO_MINI: boardName = Arduino.BOARD_TYPE_PRO_MINI;
* break; default: boardName = "unknown"; break; } }
*/



/**
* called on disconnect() so it can re-initalize if connected to a different
@@ -49,6 +49,7 @@ b16 int int (short) 2 bytes -32,768 to 32,767
import org.myrobotlab.service.Arduino;
import org.myrobotlab.service.Runtime;
import org.myrobotlab.service.Servo;
import org.myrobotlab.service.interfaces.PinArrayControl;
import org.myrobotlab.service.interfaces.SerialDevice;
import org.slf4j.Logger;

@@ -63,7 +64,7 @@ b16 int int (short) 2 bytes -32,768 to 32,767

public static final int MAX_MSG_SIZE = 64;
public static final int MAGIC_NUMBER = 170; // 10101010
public static final int MRLCOMM_VERSION = 59;
public static final int MRLCOMM_VERSION = 60;

// send buffer
int sendBufferSize = 0;
@@ -212,8 +213,8 @@ b16 int int (short) 2 bytes -32,768 to 32,767
public final static int ENCODER_ATTACH = 52;
// > setZeroPoint/deviceId
public final static int SET_ZERO_POINT = 53;
// < publishEncoderPosition/deviceId/b16 position
public final static int PUBLISH_ENCODER_POSITION = 54;
// < publishEncoderData/deviceId/b16 position
public final static int PUBLISH_ENCODER_DATA = 54;


/**
@@ -231,7 +232,7 @@ b16 int int (short) 2 bytes -32,768 to 32,767
// public void publishServoEvent(Integer deviceId/*byte*/, Integer eventType/*byte*/, Integer currentPos/*b16*/, Integer targetPos/*b16*/){}
// public void publishSerialData(Integer deviceId/*byte*/, int[] data/*[]*/){}
// public void publishUltrasonicSensorData(Integer deviceId/*byte*/, Integer echoTime/*b16*/){}
// public void publishEncoderPosition(Integer deviceId/*byte*/, Integer position/*b16*/){}
// public void publishEncoderData(Integer deviceId/*byte*/, Integer position/*b16*/){}



@@ -252,6 +253,25 @@ public void begin(SerialDevice serial){
transient private Arduino arduino;

transient private SerialDevice serial;

/**
* want to grab it when SerialDevice is created
*
* @param serial
* @return
*/
/*
static public synchronized Msg getInstance(Arduino arduino, SerialDevice serial) {
if (instance == null) {
instance = new Msg();
}
instance.arduino = arduino;
instance.serial = serial;
return instance;
}
*/

public void setInvoke(boolean b){
invoke = b;
@@ -548,18 +568,18 @@ public void processCommand(int[] ioCmd) {

break;
}
case PUBLISH_ENCODER_POSITION: {
case PUBLISH_ENCODER_DATA: {
Integer deviceId = ioCmd[startPos+1]; // bu8
startPos += 1;
Integer position = b16(ioCmd, startPos+1);
startPos += 2; //b16
if(invoke){
arduino.invoke("publishEncoderPosition", deviceId, position);
arduino.invoke("publishEncoderData", deviceId, position);
} else {
arduino.publishEncoderPosition( deviceId, position);
arduino.publishEncoderData( deviceId, position);
}
if(record != null){
rxBuffer.append("< publishEncoderPosition");
rxBuffer.append("< publishEncoderData");
rxBuffer.append("/");
rxBuffer.append(deviceId);
rxBuffer.append("/");
@@ -2089,8 +2109,8 @@ public static String methodToString(int method) {
case SET_ZERO_POINT:{
return "setZeroPoint";
}
case PUBLISH_ENCODER_POSITION:{
return "publishEncoderPosition";
case PUBLISH_ENCODER_DATA:{
return "publishEncoderData";
}

default: {
@@ -2419,7 +2439,7 @@ public static void main(String[] args) {
virtual.connectVirtualUart(port, port + "UART");
*/

Arduino arduino = (Arduino)Runtime.start("arduino","Arduino");
PinArrayControl arduino = (PinArrayControl)Runtime.start("arduino","Arduino");
Servo servo01 = (Servo)Runtime.start("servo01","Servo");

/*
@@ -63,7 +63,7 @@ b16 int int (short) 2 bytes -32,768 to 32,767

public static final int MAX_MSG_SIZE = 64;
public static final int MAGIC_NUMBER = 170; // 10101010
public static final int MRLCOMM_VERSION = 59;
public static final int MRLCOMM_VERSION = 60;

// send buffer
int sendBufferSize = 0;
@@ -212,8 +212,8 @@ b16 int int (short) 2 bytes -32,768 to 32,767
public final static int ENCODER_ATTACH = 52;
// > setZeroPoint/deviceId
public final static int SET_ZERO_POINT = 53;
// < publishEncoderPosition/deviceId/b16 position
public final static int PUBLISH_ENCODER_POSITION = 54;
// < publishEncoderData/deviceId/b16 position
public final static int PUBLISH_ENCODER_DATA = 54;


/**
@@ -282,6 +282,25 @@ public void begin(SerialDevice serial){
transient private MrlComm arduino;

transient private SerialDevice serial;

/**
* want to grab it when SerialDevice is created
*
* @param serial
* @return
*/
/*
static public synchronized Msg getInstance(MrlComm arduino, SerialDevice serial) {
if (instance == null) {
instance = new Msg();
}
instance.arduino = arduino;
instance.serial = serial;
return instance;
}
*/

public void setInvoke(boolean b){
invoke = b;
@@ -1166,14 +1185,14 @@ public synchronized void publishUltrasonicSensorData(Integer deviceId/*byte*/, I
}
}

public synchronized void publishEncoderPosition(Integer deviceId/*byte*/, Integer position/*b16*/) {
public synchronized void publishEncoderData(Integer deviceId/*byte*/, Integer position/*b16*/) {
try {
if (ackEnabled){
waitForAck();
}
write(MAGIC_NUMBER);
write(1 + 1 + 2); // size
write(PUBLISH_ENCODER_POSITION); // msgType = 54
write(PUBLISH_ENCODER_DATA); // msgType = 54
write(deviceId);
writeb16(position);

@@ -1183,7 +1202,7 @@ public synchronized void publishEncoderPosition(Integer deviceId/*byte*/, Intege
ackRecievedLock.acknowledged = false;
}
if(record != null){
txBuffer.append("> publishEncoderPosition");
txBuffer.append("> publishEncoderData");
txBuffer.append("/");
txBuffer.append(deviceId);
txBuffer.append("/");
@@ -1194,7 +1213,7 @@ public synchronized void publishEncoderPosition(Integer deviceId/*byte*/, Intege
}

} catch (Exception e) {
log.error("publishEncoderPosition threw",e);
log.error("publishEncoderData threw",e);
}
}

@@ -1360,8 +1379,8 @@ public static String methodToString(int method) {
case SET_ZERO_POINT:{
return "setZeroPoint";
}
case PUBLISH_ENCODER_POSITION:{
return "publishEncoderPosition";
case PUBLISH_ENCODER_DATA:{
return "publishEncoderData";
}

default: {
@@ -196,6 +196,8 @@ b16 int int (short)

VirtualArduino virtual;

public Integer boardType;

public MrlComm(VirtualArduino virtual) {
// msg = VirtualMsg.getInstance();
this.virtual = virtual;
@@ -574,7 +576,7 @@ public void publishBoardInfo() {

long now = micros();
int load = (int) ((now - lastBoardInfoUs) / loopCount);
msg.publishBoardInfo(MRLCOMM_VERSION, Arduino.BOARD_TYPE_ID_UNO, load, getFreeRam(), pinList.size(), deviceSummary);
msg.publishBoardInfo(MRLCOMM_VERSION, boardType, load, getFreeRam(), pinList.size(), deviceSummary);
lastBoardInfoUs = now;
loopCount = 0;
}
@@ -22,18 +22,18 @@

static public String encode(Object obj) {
String json = null;
synchronized(obj) {
// synchronized(obj) {
json = mapper.toJson(obj);
}
// }
return json;
}

@Override
public void encode(OutputStream out, Object obj) throws IOException {
String json = null;
synchronized(obj) {
// synchronized(obj) {
json = mapper.toJson(obj);
}
// }
if (json != null)
out.write(json.getBytes());
}
@@ -162,7 +162,7 @@ public Type getOwnerType() {
return byteStream.toByteArray();
}

static public final String getCallBackName(String topicMethod) {
static public final String getCallbackTopicName(String topicMethod) {
// replacements
if (topicMethod.startsWith("publish")) {
return String.format("on%s", capitalize(topicMethod.substring("publish".length())));
@@ -4,26 +4,28 @@
import java.util.Set;

import org.myrobotlab.framework.interfaces.Attachable;
import org.myrobotlab.sensor.EncoderData;
import org.myrobotlab.service.Runtime;
import org.myrobotlab.service.interfaces.EncoderControl;
import org.myrobotlab.service.interfaces.EncoderController;
import org.myrobotlab.service.Runtime;
import org.myrobotlab.service.interfaces.EncoderListener;

public class GenericEncoder implements EncoderControl {


String name;
String pin;
Boolean enabled = true;
transient EncoderController controller;
transient Set<EncoderListener> listeners = new HashSet<>();
String pin;

@Override
public void attach(Attachable attachable) throws Exception {
if (EncoderController.class.isAssignableFrom(attachable.getClass())) {
controller = (EncoderController)controller;
controller = (EncoderController) controller;
name = String.format("%s.encoder", controller.getName());
controller.attach(this);
}
if (EncoderListener.class.isAssignableFrom(attachable.getClass())) {
EncoderListener listener = (EncoderListener)attachable;
EncoderListener listener = (EncoderListener) attachable;
listeners.add(listener);
listener.attach(this);
}
@@ -44,7 +46,7 @@ public void detach(Attachable attachable) {
controller.detach(this);
}
if (EncoderListener.class.isAssignableFrom(attachable.getClass())) {
EncoderListener listener = (EncoderListener)attachable;
EncoderListener listener = (EncoderListener) attachable;
listeners.remove(listener);
listener.detach(this);
}
@@ -57,7 +59,7 @@ public void detach(String serviceName) {

@Override
public void detach() {
detach(this);
detach(this);
}

@Override
@@ -81,8 +83,8 @@ public boolean isAttached(Attachable attachable) {
return controller == attachable;
}
if (EncoderListener.class.isAssignableFrom(attachable.getClass())) {
EncoderListener listener = (EncoderListener)attachable;
return listeners.contains(listener);
EncoderListener listener = (EncoderListener) attachable;
return listeners.contains(listener);
}
return false;
}
@@ -116,27 +118,36 @@ public void setController(EncoderController controller) {
this.controller = controller;
}

@Override
public void onEncoderData(EncoderData data) {
// TODO Auto-generated method stub

}

@Override
public void setPin(String pin) {
this.pin = pin;
}

@Override
public void setPin(int address) {
// TODO Auto-generated method stub

public void setPin(Integer address) {
if (address != null) {
this.pin = address + "";
}
}

@Override
public String getPin() {
// TODO Auto-generated method stub
return null;
return pin;
}

@Override
public void enable() {
enabled = true;
}

@Override
public void disable() {
enabled = false;
}

@Override
public Boolean isEnabled() {
return enabled;
}

}
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.