Search in sources :

Example 26 with BowlerDatagram

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

the class BowlerDatagramFactoryTests method test.

@Test
public void test() {
    // Must be a factor of default pool size
    int testBifferSize = 10 * BowlerDatagramFactory.getDefaultPoolSize();
    ArrayList<BowlerDatagram> myList = new ArrayList<BowlerDatagram>();
    // verify initial state
    if (BowlerDatagramFactory.getCurrentPoolSize() != BowlerDatagramFactory.getDefaultPoolSize()) {
        fail();
    }
    for (int i = 0; i < testBifferSize; i++) {
        myList.add(BowlerDatagramFactory.build(new MACAddress(), new PingCommand()));
    }
    for (BowlerDatagram b : myList) {
        if (b.isFree()) {
            // if any packets not marked as allocated
            fail();
        }
    // System.out.println(b);
    }
    // wait for packets to timeout
    ThreadUtil.wait((int) ((double) BowlerDatagramFactory.getPacketTimeout()) / 2);
    for (BowlerDatagram b : myList) {
        if (b.isFree())
            // if any packets not marked as free too soon
            fail();
    // System.out.println(b);
    }
    // wait for packets to timeout
    ThreadUtil.wait((int) ((double) BowlerDatagramFactory.getPacketTimeout()));
    for (BowlerDatagram b : myList) {
        if (!b.isFree())
            // any packets that failed to timeout
            fail();
    }
    // refill the array
    myList.clear();
    for (int i = 0; i < testBifferSize; i++) {
        myList.add(BowlerDatagramFactory.build(new MACAddress(), new PingCommand()));
    }
    for (BowlerDatagram b : myList) {
        if (b.isFree())
            // if any packets not marked as before freeing
            fail();
        BowlerDatagramFactory.freePacket(b);
        if (!b.isFree())
            // if any packets not marked as free after freeing it
            fail();
    }
}
Also used : MACAddress(com.neuronrobotics.sdk.common.MACAddress) BowlerDatagram(com.neuronrobotics.sdk.common.BowlerDatagram) ArrayList(java.util.ArrayList) PingCommand(com.neuronrobotics.sdk.commands.bcs.core.PingCommand) Test(org.junit.Test)

Example 27 with BowlerDatagram

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

Example 28 with BowlerDatagram

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

the class PPMReaderChannel method updateValues.

private void updateValues() {
    if (getChannel().getDevice().isLegacyParser()) {
        BowlerDatagram b = null;
        // System.out.println("Updating value map");
        try {
            b = getChannel().getDevice().send(new GetValueCommand(23));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (b != null) {
            crossLinks = new int[6];
            values = new int[6];
            for (int i = 0; i < values.length; i++) {
                values[i] = b.getData().getUnsigned(1 + i);
            }
            for (int i = 0; i < crossLinks.length; i++) {
                crossLinks[i] = b.getData().getUnsigned(1 + 6 + i);
            }
        }
    } else {
        Object[] args = getChannel().getDevice().send("bcs.io.*;0.3;;", BowlerMethod.GET, "strm", new Object[] { 23 });
        crossLinks = new int[6];
        values = new int[6];
        ByteList data = (ByteList) args[1];
        Log.debug("PPM link data: " + data.size());
        for (int i = 0; i < values.length; i++) {
            values[i] = data.getUnsigned(i);
        }
        for (int i = 0; i < crossLinks.length; i++) {
            crossLinks[i] = data.getUnsigned(6 + i);
        }
    }
}
Also used : ByteList(com.neuronrobotics.sdk.common.ByteList) BowlerDatagram(com.neuronrobotics.sdk.common.BowlerDatagram) GetValueCommand(com.neuronrobotics.sdk.commands.bcs.io.GetValueCommand)

Example 29 with BowlerDatagram

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

the class SPIChannel method write.

/**
 * This performs a full read/write transaction. The data is sent down, and the corosponding data is read back in.
 * @param ss the index of the DyIO channel to use as a slave select pin for the SPI
 * @param stream the Bytes to be sent out
 * @return the data received
 */
public byte[] write(int ss, byte[] stream) {
    if (dyio.isLegacyParser()) {
        BowlerDatagram b = sendSPIStream(ss, stream);
        if (b == null)
            return new byte[0];
        return b.getData().getBytes(2);
    } else {
        ByteList data = new ByteList(stream);
        data.insert(0, (byte) ss);
        dyio.send("bcs.io.*;0.3;;", BowlerMethod.POST, "strm", new Object[] { 0, data });
        int timeout = 1000;
        rx = null;
        while (timeout-- > 0) {
            ThreadUtil.wait(1);
            if (rx != null) {
                return rx;
            }
        }
        return new byte[0];
    }
}
Also used : ByteList(com.neuronrobotics.sdk.common.ByteList) BowlerDatagram(com.neuronrobotics.sdk.common.BowlerDatagram)

Example 30 with BowlerDatagram

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

the class UDPBowlerConnection method loadPacketFromPhy.

@Override
public BowlerDatagram loadPacketFromPhy(ByteList bytesToPacketBuffer) throws NullPointerException, IOException {
    long start = System.currentTimeMillis();
    Log.info("Waiting for UDP packet");
    // Timeout the socket after 1 ms
    udpSock.setSoTimeout(1);
    // System.err.println("Timeout set "+(System.currentTimeMillis()-start));
    start = System.currentTimeMillis();
    try {
        udpSock.receive(receivePacket);
    } catch (SocketTimeoutException ste) {
        return null;
    } catch (Exception ex) {
        // disconnect called
        // Log. warning("Receive bailed out because of close");
        ex.printStackTrace();
        return null;
    }
    // System.err.println("Recv "+(System.currentTimeMillis()-start));
    start = System.currentTimeMillis();
    Log.info("Got UDP packet");
    if (addrs == null)
        addrs = new ArrayList<InetAddress>();
    getAllAddresses().add(receivePacket.getAddress());
    byte[] data = receivePacket.getData();
    for (int i = 0; i < receivePacket.getLength(); i++) {
        bytesToPacketBuffer.add(data[i]);
    }
    // System.err.println("copy "+(System.currentTimeMillis()-start));
    start = System.currentTimeMillis();
    BowlerDatagram bd = BowlerDatagramFactory.build(bytesToPacketBuffer);
    // System.err.println("build "+(System.currentTimeMillis()-start));
    return bd;
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) BowlerDatagram(com.neuronrobotics.sdk.common.BowlerDatagram) ArrayList(java.util.ArrayList) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException)

Aggregations

BowlerDatagram (com.neuronrobotics.sdk.common.BowlerDatagram)31 ByteList (com.neuronrobotics.sdk.common.ByteList)9 MACAddress (com.neuronrobotics.sdk.common.MACAddress)5 IOException (java.io.IOException)5 InvalidResponseException (com.neuronrobotics.sdk.common.InvalidResponseException)4 ArrayList (java.util.ArrayList)4 PingCommand (com.neuronrobotics.sdk.commands.bcs.core.PingCommand)3 GetChannelModeCommand (com.neuronrobotics.sdk.commands.bcs.io.GetChannelModeCommand)2 GetValueCommand (com.neuronrobotics.sdk.commands.bcs.io.GetValueCommand)2 InvalidConnectionException (com.neuronrobotics.sdk.common.InvalidConnectionException)2 RpcEncapsulation (com.neuronrobotics.sdk.common.RpcEncapsulation)2 PIDCommandException (com.neuronrobotics.sdk.pid.PIDCommandException)2 SocketException (java.net.SocketException)2 Test (org.junit.Test)2 GetChannelModeListCommand (com.neuronrobotics.sdk.commands.bcs.io.GetChannelModeListCommand)1 GetDyIOChannelCountCommand (com.neuronrobotics.sdk.commands.bcs.io.GetDyIOChannelCountCommand)1 SetChannelModeCommand (com.neuronrobotics.sdk.commands.bcs.io.setmode.SetChannelModeCommand)1 ConfigurePIDCommand (com.neuronrobotics.sdk.commands.bcs.pid.ConfigurePIDCommand)1 ControlAllPIDCommand (com.neuronrobotics.sdk.commands.bcs.pid.ControlAllPIDCommand)1 ConfigureDynamicPIDCommand (com.neuronrobotics.sdk.commands.bcs.pid.DyPID.ConfigureDynamicPIDCommand)1