Search in sources :

Example 16 with ByteList

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

the class DyIOChannel method flush.

/**
 * This method performs a single channel flush. This method will take the cached value, or the current value if none were cached, and
 * send it to the device.
 * @return true if success
 */
public boolean flush() {
    // if(getCachedMode())
    // throw new RuntimeException("In chached mode and flushing from channel");
    // Log.enableDebugPrint(true);
    Log.info("Flushing channel: " + number);
    if (getDevice().isLegacyParser()) {
        ByteList b = new ByteList();
        switch(getMode()) {
            case COUNT_IN_INT:
            case COUNT_IN_DIR:
            case COUNT_IN_HOME:
            case COUNT_OUT_INT:
            case COUNT_OUT_DIR:
            case COUNT_OUT_HOME:
                b.addAs32(getCachedValue());
                b.addAs32((int) (getCachedTime() * 1000));
                break;
            case SERVO_OUT:
                b.add(getCachedValue());
                b.addAs16((int) (getCachedTime() * 1000));
                break;
            default:
                b.add(getCachedValue());
        }
        Log.info("Setting channel: " + getChannelNumber() + " to value: " + b);
        boolean back = setValue(b);
        // Log.enableDebugPrint(false);
        return back;
    } else {
        // Log.info("Setting channel: "+number+" to value: "+getCachedValue());
        getDevice().send("bcs.io.*;0.3;;", BowlerMethod.POST, "schv", new Object[] { number, getCachedValue(), (int) (getCachedTime() * 1000) });
        return true;
    }
}
Also used : ByteList(com.neuronrobotics.sdk.common.ByteList)

Example 17 with ByteList

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

the class NRBootLoader method write.

public boolean write(int core, ByteData flashData) {
    BowlerDatagram b = null;
    for (int i = 0; i < 10; i++) {
        try {
            b = send(new ProgramSectionCommand(core, (int) flashData.getStartAddress(), new ByteList(flashData.getData())));
        } catch (Exception e) {
            e.printStackTrace();
            b = null;
        }
        if (b != null) {
            if (!b.getRPC().contains("_err"))
                return true;
        }
    }
    System.err.println("\nFailed to send 10 times!\n");
    return false;
}
Also used : ByteList(com.neuronrobotics.sdk.common.ByteList) ProgramSectionCommand(com.neuronrobotics.sdk.commands.neuronrobotics.bootloader.ProgramSectionCommand) BowlerDatagram(com.neuronrobotics.sdk.common.BowlerDatagram) IOException(java.io.IOException)

Example 18 with ByteList

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

the class BowlerCamDevice method onAsyncResponse.

public void onAsyncResponse(BowlerDatagram data) {
    if (data.getRPC().contains("_img")) {
        ByteList d = data.getData();
        int camera = ByteList.convertToInt(d.popList(2));
        int index = ByteList.convertToInt(d.popList(2));
        int total = ByteList.convertToInt(d.popList(2));
        byte[] imgData = d.popList(d.size());
        Log.info("Got image chunk\n" + data + "\nindex: " + index + ", total: " + total + ", len: " + imgData.length);
        synchronized (tmp) {
            tmp.add(imgData);
        }
        if (index == (total)) {
            // //System.out.println("Making image");
            BufferedImage image = null;
            try {
                synchronized (tmp) {
                    image = ByteArrayToImage(tmp.getBytes());
                }
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                image = null;
            }
            synchronized (tmp) {
                tmp.clear();
            }
            images.set(camera, image);
            fireIWebcamImageListenerEvent(camera, images.get(camera));
        // System.out.println("Image OK");
        }
    }
    if (data.getRPC().contains("blob")) {
        int x = ByteList.convertToInt(data.getData().getBytes(0, 4));
        int y = ByteList.convertToInt(data.getData().getBytes(4, 4));
        int r = ByteList.convertToInt(data.getData().getBytes(8, 4));
        if (x == 0 && y == 0 && r == 0) {
            gotLastMark = true;
            return;
        }
        mark.add(new ItemMarker(x, y, r));
    }
}
Also used : ByteList(com.neuronrobotics.sdk.common.ByteList) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Example 19 with ByteList

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

the class HokuyoURGDevice method connectDeviceImp.

@Override
public boolean connectDeviceImp() {
    serial.connect();
    ins = new DataInputStream(serial.getInputStream());
    outs = new DataOutputStream(serial.getOutputStream());
    receive = new Thread() {

        public void run() {
            setName("HokuyoURGDevice updater");
            ByteList bl = new ByteList();
            // System.out.println("Starting listener");
            while (run && !Thread.interrupted()) {
                try {
                    if (ins.available() > 0) {
                        while (ins.available() > 0 && run && !Thread.interrupted()) {
                            int b = ins.read();
                            if (b == 10 && bl.get(bl.size() - 1) == 10) {
                                if (bl.size() > 0) {
                                    try {
                                        URG2Packet p = new URG2Packet(new String(bl.getBytes()));
                                        Log.debug("New Packet: \n" + p);
                                        setPacket(p);
                                        bl = new ByteList();
                                    } catch (Exception ex) {
                                        setPacket(null);
                                    // System.out.println("Unknown packet");
                                    // ex.printStackTrace();
                                    }
                                }
                            } else {
                                bl.add(b);
                            }
                            ThreadUtil.wait(1);
                        }
                    } else {
                    }
                } catch (Exception e) {
                    // e.printStackTrace();
                    run = false;
                }
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    run = false;
                }
            }
            done = true;
        }
    };
    clear();
    receive.start();
    return serial.isConnected();
}
Also used : ByteList(com.neuronrobotics.sdk.common.ByteList) DataOutputStream(java.io.DataOutputStream) DataInputStream(java.io.DataInputStream) IOException(java.io.IOException)

Example 20 with ByteList

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

the class PacketValidationTest method packetTest.

@Test
public void packetTest() {
    Log.enableInfoPrint();
    BowlerDatagram bd = BowlerDatagramFactory.build(new MACAddress(), new PingCommand());
    System.out.println(bd);
    ByteList data = new ByteList(bd.getBytes());
    System.out.println(data);
    BowlerDatagram back = BowlerDatagramFactory.build(data);
    if (back == null)
        fail();
    System.out.println(back);
}
Also used : MACAddress(com.neuronrobotics.sdk.common.MACAddress) ByteList(com.neuronrobotics.sdk.common.ByteList) BowlerDatagram(com.neuronrobotics.sdk.common.BowlerDatagram) PingCommand(com.neuronrobotics.sdk.commands.bcs.core.PingCommand) Test(org.junit.Test)

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