Search in sources :

Example 1 with InvalidResponseException

use of com.neuronrobotics.sdk.common.InvalidResponseException in project java-bowler by NeuronRobotics.

the class DyIO method flushCache.

/**
 * This method will flush the DyIO cache for all channels. All channel values as stored by setting the value from code, or the value
 * stored at the time that the cache/flush mode was enabled. THis method will flush all 24 channel values in one packet allowing for
 * co-ordinated motion.
 * @param time in seconds
 */
public void flushCache(double seconds) {
    // System.out.println("Updating all channels");
    Integer[] values = new Integer[getInternalChannels().size()];
    int i = 0;
    for (DyIOChannel d : getInternalChannels()) {
        values[i++] = d.getCachedValue();
    // System.out.println("Flushing chan "+d+" to "+d.getCachedValue());
    }
    if (isLegacyParser()) {
        for (int j = 0; j < 5; j++) {
            try {
                send(new SetAllChannelValuesCommand(seconds, values));
                return;
            } catch (InvalidResponseException e1) {
                System.err.println("Failed to update all, retrying");
            }
        }
    } else {
        // for(DyIOChannel d:getInternalChannels()) {
        // d.setCachedTime((float) seconds);
        // if(d.getMode()==DyIOChannelMode.SERVO_OUT)
        // d.flush();
        // }
        send("bcs.io.*;0.3;;", BowlerMethod.POST, "sacv", new Object[] { new Integer((int) (seconds * 1000)), values });
    }
}
Also used : SetAllChannelValuesCommand(com.neuronrobotics.sdk.commands.bcs.io.SetAllChannelValuesCommand) InvalidResponseException(com.neuronrobotics.sdk.common.InvalidResponseException)

Example 2 with InvalidResponseException

use of com.neuronrobotics.sdk.common.InvalidResponseException in project java-bowler by NeuronRobotics.

the class DyIO method getDyIOChannelCount.

public Integer getDyIOChannelCount() {
    if (dyioChanCount == null) {
        if (isLegacyParser()) {
            try {
                BowlerDatagram dg = send(new GetDyIOChannelCountCommand());
                dyioChanCount = ByteList.convertToInt(dg.getData().getBytes(0, 4));
            } catch (InvalidResponseException ex) {
                ex.printStackTrace();
            }
        } else {
            Object[] args = send("bcs.io.*;0.3;;", BowlerMethod.GET, "gchc", new Object[] {});
            dyioChanCount = (Integer) args[0];
        }
    }
    return dyioChanCount;
}
Also used : BowlerDatagram(com.neuronrobotics.sdk.common.BowlerDatagram) GetDyIOChannelCountCommand(com.neuronrobotics.sdk.commands.bcs.io.GetDyIOChannelCountCommand) InvalidResponseException(com.neuronrobotics.sdk.common.InvalidResponseException)

Example 3 with InvalidResponseException

use of com.neuronrobotics.sdk.common.InvalidResponseException in project java-bowler by NeuronRobotics.

the class DyIOChannel method getValue.

/* (non-Javadoc)
	 * @see com.neuronrobotics.sdk.dyio.IDyIOChannel#getValue()
	 */
public int getValue() {
    int val = 0;
    if (getDevice().isLegacyParser()) {
        BowlerDatagram response = null;
        try {
            response = getDevice().send(new GetValueCommand(number));
        } catch (InvalidResponseException e) {
            response = getDevice().send(new GetValueCommand(number));
        }
        ByteList bl = response.getData();
        Byte b = bl.pop();
        if (b == null || b.intValue() != number) {
            Log.error("Failed to get value " + response);
            return 0;
        }
        val = new DyIOChannelEvent(this, bl).getValue();
        setCachedValue(val);
        setPreviousValue(val);
    } else {
        // Object [] args =getDevice().send("bcs.io.*;0.3;;",
        // BowlerMethod.GET,
        // "gchv",
        // new Object[]{number});
        // val=(Integer)args[1];
        // Log.debug("Got Value: "+val);
        // For the new API the channel values should come in through the asynchronous path.
        val = getPreviousValue();
    }
    return val;
}
Also used : ByteList(com.neuronrobotics.sdk.common.ByteList) BowlerDatagram(com.neuronrobotics.sdk.common.BowlerDatagram) GetValueCommand(com.neuronrobotics.sdk.commands.bcs.io.GetValueCommand) InvalidResponseException(com.neuronrobotics.sdk.common.InvalidResponseException)

Example 4 with InvalidResponseException

use of com.neuronrobotics.sdk.common.InvalidResponseException 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)

Example 5 with InvalidResponseException

use of com.neuronrobotics.sdk.common.InvalidResponseException in project java-bowler by NeuronRobotics.

the class DyIOAbstractPeripheral method SavePosition.

/**
 * This method sets the value of the output of the giver peripheral, and also stores this value as the "default"
 * value in non volatile memory to use at startup of the peripheral.
 *
 * @param pos the position to set as the new starting point for the channel
 * @return if the save worked or not.
 */
public boolean SavePosition(int pos) {
    try {
        DyIOChannelMode mode = getChannel().getMode();
        switch(mode) {
            case SERVO_OUT:
            case PWM_OUT:
                configuration = pos;
                if (getChannel().getDevice().isLegacyParser()) {
                    getChannel().send(new SetChannelValueCommand(getChannel().getChannelNumber(), pos, getMode(), true));
                } else {
                    getChannel().getDevice().send("bcs.io.*;0.3;;", BowlerMethod.CRITICAL, "cchn", new Object[] { getChannel().getChannelNumber(), true, new Integer[] { pos } });
                    getChannel().setValue(pos);
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                return true;
            default:
                return false;
        }
    } catch (InvalidResponseException e) {
        return false;
    }
}
Also used : SetChannelValueCommand(com.neuronrobotics.sdk.commands.bcs.io.SetChannelValueCommand) DyIOChannelMode(com.neuronrobotics.sdk.dyio.DyIOChannelMode) InvalidResponseException(com.neuronrobotics.sdk.common.InvalidResponseException)

Aggregations

InvalidResponseException (com.neuronrobotics.sdk.common.InvalidResponseException)5 BowlerDatagram (com.neuronrobotics.sdk.common.BowlerDatagram)2 ByteList (com.neuronrobotics.sdk.common.ByteList)2 GetDyIOChannelCountCommand (com.neuronrobotics.sdk.commands.bcs.io.GetDyIOChannelCountCommand)1 GetValueCommand (com.neuronrobotics.sdk.commands.bcs.io.GetValueCommand)1 SetAllChannelValuesCommand (com.neuronrobotics.sdk.commands.bcs.io.SetAllChannelValuesCommand)1 SetChannelValueCommand (com.neuronrobotics.sdk.commands.bcs.io.SetChannelValueCommand)1 SetChannelModeCommand (com.neuronrobotics.sdk.commands.bcs.io.setmode.SetChannelModeCommand)1 DyIOChannelMode (com.neuronrobotics.sdk.dyio.DyIOChannelMode)1