Search in sources :

Example 11 with ByteList

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

the class DyIOOutputStream method write.

/**
 * Writes <code>bl</code> to this output stream as a single packet verses
 * individual writes for each byte.
 *
 * @param bl
 *            - the data
 * @throws IOException
 *             - if an I/O error occurs.
 */
public void write(ByteList bl) throws IOException {
    if (chan.getMode() != DyIOChannelMode.USART_TX) {
        throw new IOException("The DyIO is not configured with a UART Tx mode");
    }
    // System.out.println("Sending ByteList: "+bl.asString());
    while (bl.size() > 0) {
        ByteList b;
        if (bl.size() > 20) {
            b = new ByteList(bl.popList(20));
        } else {
            b = new ByteList(bl.popList(bl.size()));
        }
        // System.out.println("Sending ByteList: "+b.asString());
        chan.setValue(b);
    }
}
Also used : ByteList(com.neuronrobotics.sdk.common.ByteList) IOException(java.io.IOException)

Example 12 with ByteList

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

the class CreateSensors method toString.

/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
public String toString() {
    String s = "Raw: " + new ByteList(data);
    s += "\nDistance: " + distance;
    s += "\nAngle: " + angle;
    s += "\nTempreture: " + tempreture;
    s += "\nCharge: " + charge;
    s += "\nCapacity: " + capacity;
    s += "\nVoltage: " + voltage;
    s += "\nBump right: " + rightBump;
    s += "\nBump left: " + leftBump;
    s += "\nDrop right: " + rightDrop;
    s += "\nDrop left: " + leftDrop;
    s += "\nDrop center: " + centerDrop;
    return s;
}
Also used : ByteList(com.neuronrobotics.sdk.common.ByteList)

Example 13 with ByteList

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

the class DyIO method getAllChannelModes.

public ArrayList<DyIOChannelMode> getAllChannelModes() {
    ArrayList<DyIOChannelMode> modes = new ArrayList<DyIOChannelMode>();
    BowlerDatagram response;
    ByteList bl;
    if (isLegacyParser()) {
        try {
            response = send(new GetChannelModeCommand());
        } catch (Exception e) {
            if (getInternalChannels().size() == 0) {
                Log.error("Initilization failed once, retrying");
                try {
                    response = send(new GetChannelModeCommand());
                } catch (Exception e2) {
                    e2.printStackTrace();
                    setMuteResyncOnModeChange(false);
                    throw new DyIOCommunicationException("DyIO failed to report during initialization. Could not determine DyIO configuration: " + e2.getMessage());
                }
            } else {
                setMuteResyncOnModeChange(false);
                return null;
            }
        }
        if (response == null)
            checkFirmwareRev();
        // if(getAddress().equals(new MACAddress(MACAddress.BROADCAST))) {
        setAddress(response.getAddress());
        // }
        @SuppressWarnings("unused") int count = 0;
        if (getDyIOChannelCount() != null) {
            count = getDyIOChannelCount();
        }
        bl = response.getData();
        Log.error("Using old parsing model");
    } else {
        Object[] args = send("bcs.io.*;0.3;;", BowlerMethod.GET, "gacm", new Object[] {});
        bl = (ByteList) args[0];
    }
    for (int i = 0; i < bl.size(); i++) {
        DyIOChannelMode cm = DyIOChannelMode.get(bl.getByte(i));
        modes.add(cm);
    }
    return modes;
}
Also used : GetChannelModeCommand(com.neuronrobotics.sdk.commands.bcs.io.GetChannelModeCommand) ByteList(com.neuronrobotics.sdk.common.ByteList) BowlerDatagram(com.neuronrobotics.sdk.common.BowlerDatagram) ArrayList(java.util.ArrayList) PIDCommandException(com.neuronrobotics.sdk.pid.PIDCommandException) InvalidConnectionException(com.neuronrobotics.sdk.common.InvalidConnectionException) InvalidResponseException(com.neuronrobotics.sdk.common.InvalidResponseException)

Example 14 with ByteList

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

the class DyIO method onAsyncResponse.

/* (non-Javadoc)
	 * @see com.neuronrobotics.sdk.common.IBowlerDatagramListener#onAsyncResponse
	 */
public void onAsyncResponse(BowlerDatagram data) {
    if (!haveBeenSynced) {
        return;
    }
    Log.info("<< Async\n" + data.toString());
    if (data.getRPC().equals("_pwr")) {
        powerEvent(data);
    }
    if (data.getRPC().equals("gchv")) {
        ByteList bl = data.getData();
        Byte b = bl.pop();
        if (b == null) {
            return;
        }
        DyIOChannel c = getChannel(b);
        c.fireChannelEvent(new DyIOChannelEvent(c, bl));
        return;
    }
    if (data.getRPC().equals("gacv")) {
        if (isLegacyParser()) {
            Log.error("All channel values\n" + data.toString());
            ByteList bl = data.getData();
            for (DyIOChannel c : getChannels()) {
                ByteList val = new ByteList(bl.popList(4));
                Log.error("DyIO event " + c + " value: " + val);
                if (!c.isStreamChannel())
                    c.fireChannelEvent(new DyIOChannelEvent(c, val));
            }
        } else {
            Log.info("All channel values\n" + data.toString());
            ByteList bl = data.getData();
            int numChan = bl.pop();
            if (numChan != getChannels().size()) {
                Log.error("Bad packet, wrong number of values");
            }
            for (int i = 0; i < getChannels().size(); i++) {
                DyIOChannel c = getChannels().get(i);
                ByteList val = new ByteList(bl.popList(4));
                Log.info("DyIO event " + c + " value: " + val);
                if (!c.isStreamChannel())
                    c.fireChannelEvent(new DyIOChannelEvent(c, val));
            }
        }
    }
    if (data.getRPC().equals("strm")) {
        Log.warning("STREAM Packet\n" + data.toString());
        ByteList bl = data.getData();
        // channel value
        Byte b = bl.pop();
        if (b == null) {
            return;
        }
        // size of array
        bl.pop();
        DyIOChannel c = getChannel(b);
        c.fireChannelEvent(new DyIOChannelEvent(c, bl));
    } else {
        IDyIOEvent e = new DyIOAsyncEvent(data);
        fireDyIOEvent(e);
    }
    getPid().onAsyncResponse(data);
}
Also used : ByteList(com.neuronrobotics.sdk.common.ByteList)

Example 15 with ByteList

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

ByteList (com.neuronrobotics.sdk.common.ByteList)25 BowlerDatagram (com.neuronrobotics.sdk.common.BowlerDatagram)9 IOException (java.io.IOException)5 InvalidResponseException (com.neuronrobotics.sdk.common.InvalidResponseException)3 GetValueCommand (com.neuronrobotics.sdk.commands.bcs.io.GetValueCommand)2 SetChannelValueCommand (com.neuronrobotics.sdk.commands.bcs.io.SetChannelValueCommand)2 DyIO (com.neuronrobotics.sdk.dyio.DyIO)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 PingCommand (com.neuronrobotics.sdk.commands.bcs.core.PingCommand)1 GetChannelModeCommand (com.neuronrobotics.sdk.commands.bcs.io.GetChannelModeCommand)1 GetChannelModeListCommand (com.neuronrobotics.sdk.commands.bcs.io.GetChannelModeListCommand)1 SetChannelModeCommand (com.neuronrobotics.sdk.commands.bcs.io.setmode.SetChannelModeCommand)1 ControlAllPIDCommand (com.neuronrobotics.sdk.commands.bcs.pid.ControlAllPIDCommand)1 ProgramSectionCommand (com.neuronrobotics.sdk.commands.neuronrobotics.bootloader.ProgramSectionCommand)1 GetAllChannelValuesCommand (com.neuronrobotics.sdk.commands.neuronrobotics.dyio.GetAllChannelValuesCommand)1 InvalidConnectionException (com.neuronrobotics.sdk.common.InvalidConnectionException)1 MACAddress (com.neuronrobotics.sdk.common.MACAddress)1 SPIChannel (com.neuronrobotics.sdk.dyio.peripherals.SPIChannel)1 UARTChannel (com.neuronrobotics.sdk.dyio.peripherals.UARTChannel)1