You are here

Parsing PPM data from CAN Bus

1 post / 0 new
walleywalker
Offline
Last seen: 1 year 8 months ago
Joined: 2018-10-13 21:43
Posts: 2
Parsing PPM data from CAN Bus

I believe I am getting a VESC response now with PPM information, though I am having trouble understanding the output format. My short Arduino code is below.

My primary question is, have I gone about this correctly to read the PPM info, is the output as expected, and can anyone advise of the parsing format or where I can dig to find it?

B4r9Pk5gvm.png

 

#include <Arduino.h>
#include <ESP32CAN.h>
#include <CAN_config.h>


CAN_device_t CAN_cfg;               // CAN Config
unsigned long previousMillis = 0;   // will store last time a CAN Message was send
const int interval = 500;          // interval at which send CAN Messages (milliseconds)
const int rx_queue_size = 10;       // Receive Queue size
uint8_t vesc_id = 0;
uint16_t CAN_PACKET_PROCESS_SHORT_BUFFER = 8;   //From CanBus.H header typedef enum list kicks out --> CAN_PACKET_ID;  



void setup() {
  Serial.begin(9600);
  Serial.println("Basic Demo - ESP32-Arduino-CAN");
  CAN_cfg.speed = CAN_SPEED_500KBPS;
  CAN_cfg.tx_pin_id = GPIO_NUM_22;
  CAN_cfg.rx_pin_id = GPIO_NUM_21;
  CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t));
  // Init CAN Module
  ESP32Can.CANInit();
}

void loop() {

  CAN_frame_t rx_frame;

  unsigned long currentMillis = millis();

  // Receive next CAN frame from queue
  if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE) {

    if (rx_frame.FIR.B.FF == CAN_frame_std) {
      printf("New standard frame");
    }
    else {
      printf("New extended frame");
    }

    if (rx_frame.FIR.B.RTR == CAN_RTR) {
      printf(" RTR from 0x%08X, DLC %d\r\n", rx_frame.MsgID,  rx_frame.FIR.B.DLC);
    }
    else {
      printf(" from 0x%08X, DLC %d, Data ", rx_frame.MsgID,  rx_frame.FIR.B.DLC);
      for (int i = 0; i < rx_frame.FIR.B.DLC; i++) {
        printf("0x%02X ", rx_frame.data.u8[i]);
      }
      printf("\n");
    }
  }
  // Send CAN Message
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    CAN_frame_t tx_frame;
    tx_frame.FIR.B.FF = CAN_frame_ext;   
    tx_frame.MsgID = (uint32_t(0x8000) << 16) + (uint16_t(CAN_PACKET_PROCESS_SHORT_BUFFER) << 8) + vesc_id;   
    tx_frame.FIR.B.DLC = 0x07;
    tx_frame.data.u8[0] = 0x11;
    tx_frame.data.u8[1] = 0x00;
    tx_frame.data.u8[2] = 0x1F; // Comm_Packet_ID --> Comm_GET_DECODED_PPM     ,   0x1F = 31 in decimal
    tx_frame.data.u8[3] = 0x00;
    tx_frame.data.u8[4] = 0x00;
    tx_frame.data.u8[5] = 0x00;
    tx_frame.data.u8[6] = 0x00;
   
    ESP32Can.CANWriteFrame(&tx_frame);
  }
}