コード例 #1
0
  /**
   * Send a message on the CAN bus through the CAN driver in FRC_NetworkCommunication
   *
   * <p>Trusted messages require a 2-byte token at the beginning of the data payload. If the message
   * being sent is trusted, make space for the token.
   *
   * @param messageID The messageID to be used on the CAN bus
   * @param data The up to 8 bytes of data to be sent with the message
   * @param dataSize Specify how much of the data in "data" to send
   */
  protected static void sendMessage(int messageID, byte[] data, int dataSize)
      throws CANTimeoutException {
    final int[] kTrustedMessages = {
      JaguarCANProtocol.LM_API_VOLT_T_EN, JaguarCANProtocol.LM_API_VOLT_T_SET,
      JaguarCANProtocol.LM_API_SPD_T_EN, JaguarCANProtocol.LM_API_SPD_T_SET,
      JaguarCANProtocol.LM_API_VCOMP_T_EN, JaguarCANProtocol.LM_API_VCOMP_T_SET,
      JaguarCANProtocol.LM_API_POS_T_EN, JaguarCANProtocol.LM_API_POS_T_SET,
      JaguarCANProtocol.LM_API_ICTRL_T_EN, JaguarCANProtocol.LM_API_ICTRL_T_SET
    };

    byte i;
    for (i = 0; i < kTrustedMessages.length; i++) {
      if ((kFullMessageIDMask & messageID) == kTrustedMessages[i]) {
        sendTrustedDataBuffer[0] = 0;
        sendTrustedDataBuffer[1] = 0;
        // Make sure the data will still fit after adjusting for the token.
        if (dataSize > JaguarCANDriver.kMaxMessageDataSize - 2) {
          throw new RuntimeException("CAN message has too much data.");
        }

        byte j;
        for (j = 0; j < dataSize; j++) {
          sendTrustedDataBuffer[j + 2] = data[j];
        }

        JaguarCANDriver.sendMessage(messageID, sendTrustedDataBuffer, dataSize + 2);
        return;
      }
    }
    JaguarCANDriver.sendMessage(messageID, data, dataSize);
  }
コード例 #2
0
 /**
  * Receive a message from the CAN bus through the CAN driver in FRC_NetworkCommunication
  *
  * @param messageID The messageID to read from the CAN bus
  * @param data The up to 8 bytes of data that was received with the message
  * @param timeout Specify how long to wait for a message (in seconds)
  */
 protected static byte receiveMessage(int messageID, byte[] data, double timeout)
     throws CANTimeoutException {
   JaguarCANDriver canDriver = new JaguarCANDriver();
   byte dataSize = canDriver.receiveMessage(messageID, data, timeout);
   return dataSize;
 }