Search in sources :

Example 1 with LynxDatagram

use of com.qualcomm.hardware.lynx.commands.LynxDatagram in project robotcode by OutoftheBoxFTC.

the class LynxModule method sendCommand.

/**
 * Sends a command to the module, scheduling retransmissions as necessary.
 */
public void sendCommand(LynxMessage command) throws InterruptedException, LynxUnsupportedCommandNumberException {
    command.setMessageNumber(getNewMessageNumber());
    int msgnumCur = command.getMessageNumber();
    // Serialize this guy and remember it
    // throws LynxUnsupportedCommandNumberException
    LynxDatagram datagram = new LynxDatagram(command);
    command.setSerialization(datagram);
    // Remember this guy as someone who needs acknowledgement
    boolean moduleWillReply = command.isAckable() || command.isResponseExpected();
    this.unfinishedCommands.put(msgnumCur, (LynxRespondable) command);
    // Send it on out!
    this.lynxUsbDevice.transmit(command);
    // If the module isn't going to send something back to us in response, then it's finished
    if (!moduleWillReply) {
        finishedWithMessage(command);
    }
}
Also used : LynxDatagram(com.qualcomm.hardware.lynx.commands.LynxDatagram)

Example 2 with LynxDatagram

use of com.qualcomm.hardware.lynx.commands.LynxDatagram in project robotcode by OutoftheBoxFTC.

the class LynxUsbDeviceImpl method transmit.

@Override
public void transmit(LynxMessage message) throws InterruptedException // Note that this might be called on ANY thread.
{
    synchronized (engageLock) {
        if (this.isArmedOrArming() && !this.hasShutdownAbnormally() && isEngaged) {
            if (DEBUG_LOG_DATAGRAMS || DEBUG_LOG_MESSAGES) {
                RobotLog.vv(TAG, "xmit'ing: mod=%d cmd=0x%02x(%s) msg#=%d ref#=%d ", message.getModuleAddress(), message.getCommandNumber(), message.getClass().getSimpleName(), message.getMessageNumber(), message.getReferenceNumber());
            }
            LynxDatagram datagram = message.getSerialization();
            Assert.assertTrue(datagram != null);
            byte[] bytes = datagram.toByteArray();
            try {
                this.robotUsbDevice.write(bytes);
            } catch (// RuntimeException is just paranoia
            RobotUsbException | RuntimeException e) {
                // For now, at least, we're brutal: we don't quarter ANY usb transmission errors
                // before giving up and shutting things down. In the wake of future experience, it
                // might later be reasonable to reconsider this decision.
                shutdownAbnormally();
                // 
                RobotLog.ee(TAG, e, "exception thrown in LynxUsbDevice.transmit");
                // 
                return;
            }
            long now = System.nanoTime();
            message.setNanotimeLastTransmit(now);
            // "The keep alive must be sent at least every 2500 milliseconds. The Controller Module
            // will perform the actions specified in Fail Safe (7F05) if it fails to receive a timely
            // Keep Alive". Other messages will do the trick, too.
            // 
            message.resetModulePingTimer();
        } else {
            message.onPretendTransmit();
        }
    }
    // Do this last so as to make LynxModule.retransmitDatagrams() interlock more robust
    message.noteHasBeenTransmitted();
}
Also used : RobotUsbException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException) LynxDatagram(com.qualcomm.hardware.lynx.commands.LynxDatagram)

Aggregations

LynxDatagram (com.qualcomm.hardware.lynx.commands.LynxDatagram)2 RobotUsbException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException)1