Skip to content

Commit

Permalink
worky-duino (#1410)
Browse files Browse the repository at this point in the history
  • Loading branch information
supertick authored Feb 16, 2024
1 parent 17dba46 commit c8178e4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 145 deletions.
154 changes: 11 additions & 143 deletions src/main/java/org/myrobotlab/service/Arduino.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ public static class I2CDeviceMap {

transient Mapper motorPowerMapper = new MapperLinear(-1.0, 1.0, -255.0, 255.0);

// make final - if not "connected" log error but don't allow Arduino NPEs
public final transient Msg msg = new Msg(this, null);

Integer nextDeviceId = 0;
Expand All @@ -191,10 +190,6 @@ public static class I2CDeviceMap {

private volatile boolean syncInProgress = false;

/**
* the port the user attempted to connect to
*/
String port;

public Arduino(String n, String id) {
super(n, id);
Expand Down Expand Up @@ -552,6 +547,7 @@ public VirtualArduino getVirtual() {
*/
@Override
public void connect(String port, int rate, int databits, int stopbits, int parity) {
config.connect = true;
connecting = true;
if (port == null) {
warn("%s attempted to connect with a null port", getName());
Expand All @@ -563,7 +559,7 @@ public void connect(String port, int rate, int databits, int stopbits, int parit
serial.addByteListener(this);

// test to see if we've been started. the serial might be null
this.port = port;
config.port = port;

try {

Expand Down Expand Up @@ -811,6 +807,7 @@ public void disablePins() {

@Override
public void disconnect() {
config.connect = false;
// FIXED - all don in 'onDisconnect()'
// enableBoardInfo(false);
// boardInfo is not valid after disconnect
Expand Down Expand Up @@ -2233,7 +2230,7 @@ public Map<String, DeviceMapping> getDeviceList() {

@Override
public void ackTimeout() {
log.warn("{} Ack Timeout seen. TODO: consider resetting the com port {}, reconnecting and re syncing all devices.", getName(), port);
log.warn("{} Ack Timeout seen. TODO: consider resetting the com port {}, reconnecting and re syncing all devices.", getName(), config.port);
}

@Override
Expand Down Expand Up @@ -2325,34 +2322,13 @@ public void neoPixelClear(String neopixel) {
msg.neoPixelClear(getDeviceId(neopixel));
}

@Override
public ArduinoConfig getConfig() {
super.getConfig();

// FIXME "port" shouldn't exist only config.port !
config.port = port;
config.connect = isConnected();

return config;
}

@Override
public ArduinoConfig apply(ArduinoConfig c) {
super.apply(c);

if (msg == null) {
if (config.connect && config.port != null) {
serial = (Serial) startPeer("serial");
if (serial == null) {
log.error("serial is null");
}
msg.setSerial(serial);
serial.addByteListener(this);
} else {
// TODO: figure out why this gets called so often.
log.info("Init serial we already have a msg class.");
}

if (config.connect && config.port != null) {
serial.addByteListener(this);
connect(config.port);
}

Expand All @@ -2372,13 +2348,8 @@ public ArduinoConfig apply(ArduinoConfig c) {
public static void main(String[] args) {
try {

// Platform.setVirtual(true);

LoggingFactory.init(Level.INFO);

Runtime runtime = Runtime.getInstance();
runtime.saveAllDefaults();

Runtime.start("arduino", "Arduino");
Runtime.start("webgui", "WebGui");

Expand All @@ -2387,115 +2358,12 @@ public static void main(String[] args) {
if (isDone) {
return;
}
// Platform.setVirtual(true);

/*
* WebGui webgui = (WebGui) Runtime.create("webgui", "WebGui");
* webgui.autoStartBrowser(false); webgui.setPort(8887);
* webgui.startService();
*/

// Runtime.start("gui", "SwingGui");
Serial.listPorts();

Arduino hub = (Arduino) Runtime.start("controller", "Arduino");
Runtime.start("pir", "Pir");

hub.connect("/dev/ttyACM0");

// hub.enableAck(false);

ServoControl sc = (ServoControl) Runtime.start("s1", "Servo");
sc.setPin(3);
hub.attach(sc);
sc = (ServoControl) Runtime.start("s2", "Servo");
sc.setPin(9);
hub.attach(sc);

hub.detach();

// hub.enableAck(true);
/*
* sc = (ServoControl) Runtime.start("s3", "Servo"); sc.setPin(12);
* hub.attach(sc);
*/

log.info("here");
// hub.connect("COM6"); // uno

// hub.startTcpServer();

VirtualArduino vmega = null;

vmega = (VirtualArduino) Runtime.start("vmega", "VirtualArduino");
vmega.connect("COM7");
Serial sd = vmega.getSerial();
sd.startTcpServer();

// Runtime.start("webgui", "WebGui");

Arduino mega = (Arduino) Runtime.start("mega", "Arduino");

if (mega.isVirtual()) {
vmega = mega.getVirtual();
vmega.setBoardMega();
}

// mega.getBoardTypes();
// mega.setBoardMega();
// mega.setBoardUno();
mega.connect("COM7");

/*
* Arduino uno = (Arduino) Runtime.start("uno", "Arduino");
* uno.connect("COM6");
*/

// log.info("port names {}", mega.getPortNames());

Servo servo = (Servo) Runtime.start("servo", "Servo");
// servo.load();
log.info("rest is {}", servo.getRest());
servo.save();
// servo.setPin(8);
servo.attach(mega);

servo.moveTo(90.0);

/*
* servo.moveTo(3); sleep(300); servo.moveTo(130); sleep(300);
* servo.moveTo(90); sleep(300);
*
*
* // minmax checking
*
* servo.invoke("moveTo", 120);
*/

/*
* mega.attach(servo);
*
* servo.moveTo(3);
*
* servo.moveTo(30);
*
* mega.enablePin("A4");
*
* // arduino.setBoardMega();
*
* Adafruit16CServoDriver adafruit = (Adafruit16CServoDriver)
* Runtime.start("adafruit", "Adafruit16CServoDriver");
* adafruit.attach(mega); mega.attach(adafruit);
*/

// servo.attach(arduino, 8, 90);

// Runtime.start("webgui", "WebGui");
// Service.sleep(3000);

// remote.startListening();

// Runtime.start("webgui", "WebGui");
// Platform.setVirtual(true);
// Serial sd = vmega.getSerial();
// sd.startTcpServer();
// Serial.listPorts();


} catch (Exception e) {
log.error("main threw", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@

public class ArduinoConfig extends ServiceConfig {

/**
* Port (usb or ip:port) to connect)
*/
public String port;

/**
* If you want the arduino to try to connect
* port must not be null.
* This is not a status field.
*/
public boolean connect;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<div class="row">
<div class="form-group">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label for="port">port</label>
<input class="form-control" type="text" ng-readonly="isConnected" id="{{service.name}}-port" ng-model="service.port" uib-typeahead="portName for portName in possiblePorts | filter:$viewValue" typeahead-min-length="0" title="Serial port to connect to - &#10; clicking on this box should show a list of possible ports. &#10; Typically Windows list serial ports as COM1,COM2 etc and Linux or Mac will have /dev/ttyS1 .. etc &#10; You may create a virtual port just by typing some arbitrary name and making sure the service is in virtual mode, &#10; then pressing the connect button"/>
<input class="form-control" type="text" ng-readonly="isConnected" id="{{service.name}}-port" ng-model="service.config.port" uib-typeahead="portName for portName in possiblePorts | filter:$viewValue" typeahead-min-length="0" title="Serial port to connect to - &#10; clicking on this box should show a list of possible ports. &#10; Typically Windows list serial ports as COM1,COM2 etc and Linux or Mac will have /dev/ttyS1 .. etc &#10; You may create a virtual port just by typing some arbitrary name and making sure the service is in virtual mode, &#10; then pressing the connect button"/>
<label for="rate">rate</label>
<input type="text" class="form-control" ng-readonly="isConnected" id="{{service.name}}-rate" ng-model="rate" uib-typeahead="rate for rate in possibleBaud | filter:$viewValue" typeahead-min-length="0"/>
<button class="btn btn-default" ng-show="!isConnected" ng-click="msg.getPortNames()">refresh</button>
<button class="btn btn-default" ng-show="!isConnected" ng-click="msg.connect(service.port, rate)">connect</button>
<button class="btn btn-default" ng-show="!isConnected" ng-click="msg.connect(service.config.port, rate)">connect</button>
<button class="btn btn-default" ng-show="isConnected" ng-click="msg.disconnect()">disconnect</button>
<img ng-src="../../{{isConnectedImage}}.png" tilte="connected light &#10; green is connected &#10; red is disconnected"/>
<button class="btn btn-default" ng-click="msg.getBase64ZippedMrlComm()">Get MrlComm Library</button>
Expand Down

0 comments on commit c8178e4

Please sign in to comment.