Search in sources :

Example 1 with SetChannelModeCommand

use of com.neuronrobotics.sdk.commands.bcs.io.setmode.SetChannelModeCommand in project java-bowler by NeuronRobotics.

the class DyIO method resync.

/**
 * This method re-synchronizes the entire state of the DyIO object with the DyIO module.
 * The Firmware version is checked
 * The info string is checked
 * All channel modes are updated
 *
 * @return true if success
 */
public boolean resync() {
    if (getConnection() == null) {
        Log.info("Not connected");
        return false;
    }
    if (!getConnection().isConnected()) {
        Log.info("Not connected");
        return false;
    }
    if (isResyncing()) {
        Log.info("Already resyncing");
        return true;
    }
    if (hasNamespace(NEURONROBOTICS_DYIO_1_0)) {
        setLegacyParser(false);
    }
    if (hasNamespace("neuronrobotics.dyio.*;0.3;;")) {
        setLegacyParser(true);
    }
    setResyncing(true);
    setMuteResyncOnModeChange(true);
    Log.info("Re-syncing...");
    getBatteryVoltage(true);
    BowlerDatagram response;
    try {
        if (!haveFirmware()) {
            firmware = getRevisions().get(0).getBytes();
        }
        checkFirmwareRev();
    // if(info.contains("Unknown")){
    // response = send(new InfoCommand());
    // if (response != null) {
    // info = response.getData().asString();
    // }
    // }
    } catch (Exception e) {
        e.printStackTrace();
        checkFirmwareRev();
    }
    ArrayList<DyIOChannelMode> modes = getAllChannelModes();
    for (int i = 0; i < modes.size(); i++) {
        DyIOChannelMode cm = modes.get(i);
        boolean editable = true;
        if (cm == null) {
            cm = DyIOChannelMode.DIGITAL_OUT;
            editable = false;
            try {
                Log.error("Mode get failed, setting to default");
                send(new SetChannelModeCommand(i, cm));
            } catch (Exception e) {
                setMuteResyncOnModeChange(false);
                throw new DyIOCommunicationException("Setting a pin to Digital In failed");
            }
        }
        try {
            Log.info("\n\nUpdating channel: " + i);
            getInternalChannels().get(i).update(this, i, cm, editable);
        } catch (IndexOutOfBoundsException e) {
            // System.out.println("New channel "+i);
            getInternalChannels().add(new DyIOChannel(this, i, cm, editable));
            DyIOChannel dc = getInternalChannels().get(i);
            dc.fireModeChangeEvent(dc.getCurrentMode());
        }
    }
    setMuteResyncOnModeChange(false);
    if (getInternalChannels().size() == 0)
        throw new DyIOCommunicationException("DyIO failed to report during initialization");
    haveBeenSynced = true;
    setResyncing(false);
    return true;
}
Also used : SetChannelModeCommand(com.neuronrobotics.sdk.commands.bcs.io.setmode.SetChannelModeCommand) BowlerDatagram(com.neuronrobotics.sdk.common.BowlerDatagram) PIDCommandException(com.neuronrobotics.sdk.pid.PIDCommandException) InvalidConnectionException(com.neuronrobotics.sdk.common.InvalidConnectionException) InvalidResponseException(com.neuronrobotics.sdk.common.InvalidResponseException)

Example 2 with SetChannelModeCommand

use of com.neuronrobotics.sdk.commands.bcs.io.setmode.SetChannelModeCommand in project java-bowler by NeuronRobotics.

the class DyIOChannel method setMode.

/* (non-Javadoc)
	 * @see com.neuronrobotics.sdk.dyio.IDyIOChannel#setMode(com.neuronrobotics.sdk.dyio.DyIOChannelMode, boolean)
	 */
public boolean setMode(DyIOChannelMode mode, boolean async) {
    if (settingMode)
        return true;
    // resyncIfNotSynced();
    if (mode == null) {
        throw new RuntimeException("Mode can not be set to null, must be set to a mode");
    }
    if (getMode() == null) {
        Log.info(this.getClass() + " First time setting mode.");
        fireModeChangeEvent(mode);
        isAsync = isDefaultAsync(mode);
        haveSetMode = false;
    } else if ((getMode() == mode) && (async == isAsync)) {
        Log.debug(this.getClass() + "Channel: " + getChannelNumber() + " is already " + getMode());
        return true;
    }
    if (!canBeMode(mode)) {
        if (mode == DyIOChannelMode.SERVO_OUT)
            new RuntimeException("\nChannel: " + getChannelNumber() + " can not be mode '" + mode + "' in current configuration. \nCheck the power switch settings and availible modes.").printStackTrace();
        else
            new RuntimeException("\nChannel: " + getChannelNumber() + " can not be mode '" + mode + "'.").printStackTrace();
        mode = getMode();
    }
    settingMode = true;
    for (int i = 0; i < MAXATTEMPTS; i++) {
        try {
            isAsync = async;
            haveSetMode = true;
            /**
             * Legacy
             */
            if (getDevice().isLegacyParser()) {
                getDevice().send(new SetChannelModeCommand(number, mode, async));
                if (!getDevice().isMuteResyncOnModeChange()) {
                    try {
                        getDevice().resync();
                    } catch (RuntimeException e) {
                        e.printStackTrace();
                        getDevice().setMuteResyncOnModeChange(true);
                    }
                } else {
                    Log.info("Not resyncing from channel: " + getChannelNumber());
                    fireModeChangeEvent(mode);
                }
            } else {
                // int printlevel = Log.getMinimumPrintLevel();
                // Log.enableInfoPrint();
                Object[] args = getDevice().send("bcs.io.setmode.*;0.3;;", BowlerMethod.POST, "schm", new Object[] { getChannelNumber(), mode.getValue(), async ? 1 : 0 });
                ByteList currentModes = (ByteList) args[0];
                // System.out.println("Setting # "+getChannelNumber()+" to "+mode);
                for (int j = 0; j < getDevice().getChannels().size(); j++) {
                    DyIOChannelMode cm = DyIOChannelMode.get(currentModes.getByte(j));
                    if (getDevice().getChannel(j).getCurrentMode() != cm) {
                        // System.err.println("Setting # "+j+" to "+cm);
                        getDevice().getChannel(j).fireModeChangeEvent(cm);
                    }
                }
            // Log.setMinimumPrintLevel(printlevel);
            }
            settingMode = false;
            // Defaultuing the advanced async to on
            if (isAsync)
                getDevice().configAdvancedAsyncNotEqual(number, 10);
            return true;
        } catch (InvalidResponseException e) {
            Log.error(e.getMessage());
            try {
                Thread.sleep(100);
            } catch (InterruptedException e1) {
                settingMode = false;
                return false;
            }
        }
    }
    settingMode = false;
    return false;
}
Also used : SetChannelModeCommand(com.neuronrobotics.sdk.commands.bcs.io.setmode.SetChannelModeCommand) ByteList(com.neuronrobotics.sdk.common.ByteList) InvalidResponseException(com.neuronrobotics.sdk.common.InvalidResponseException)

Aggregations

SetChannelModeCommand (com.neuronrobotics.sdk.commands.bcs.io.setmode.SetChannelModeCommand)2 InvalidResponseException (com.neuronrobotics.sdk.common.InvalidResponseException)2 BowlerDatagram (com.neuronrobotics.sdk.common.BowlerDatagram)1 ByteList (com.neuronrobotics.sdk.common.ByteList)1 InvalidConnectionException (com.neuronrobotics.sdk.common.InvalidConnectionException)1 PIDCommandException (com.neuronrobotics.sdk.pid.PIDCommandException)1