Forum Rule: Always post complete source code & details to reproduce any issue!
Page 48 of 48 FirstFirst ... 38 46 47 48
Results 1,176 to 1,193 of 1193

Thread: FlexCAN_T4 - FlexCAN for Teensy 4

  1. #1176
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    4,095
    I see you setting K bus filter but not setting up the K bus, unless you posted partial code, can't help much. the filters are only updatable while the bus is deactivated, so that is fine and does as it's expected to. But if you're setting things that are not complete it could result in a lock, which is why you don't see the KBUS print in serial monitor.

  2. #1177
    Junior Member
    Join Date
    May 2023
    Posts
    6
    Morning, thanks for the response. K-bus is set up, I just included some sample code to show how the existing stuff initialises things and sets filters- K/V bus very similar in terms of filters.

    Will try disableFIFO() instead, hopefully that helps - not seeing a method to deactivate the bus entirely, but they're defined as:

    Code:
    FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_256> K_Bus;
    Dispose of the object, perhaps?

    edit: disableFIFO hasn't hung the Teensy, so will keep on with the program, maybe that's all I needed. Thanks!

  3. #1178
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    4,095
    you can disable and reenable fifo by default it'll accept all
    then filter afterwards as needed

  4. #1179
    Junior Member
    Join Date
    May 2023
    Posts
    6
    Awesome, thank you !

    Code:
    Disabled KBUS
    10:45:40:237 -> 0, V-Bus, MB: 4, ID: 0x18FFFB13, EXT: 1, LEN: 8, DATA: 0, 26, 4, 29, 165, 56, 0, 15, 
    Sending status stuff to UDP:  ResponseCode: 174  barr length: 24  Bus: 2
    10:45:40:407 -> 0, V-Bus, MB: 4, ID: 0x18FFFB13, EXT: 1, LEN: 8, DATA: 0, 26, 4, 29, 165, 56, 0, 15, 
    Sending status stuff to UDP:  ResponseCode: 174  barr length: 24  Bus: 2
    10:45:40:501 -> 0, V-Bus, MB: 4, ID: 0x18FFFF13, EXT: 1, LEN: 8, DATA: 118, 16, 77, 5, 161, 255, 73, 255, 
    Sending status stuff to UDP:  ResponseCode: 174  barr length: 24  Bus: 2
    10:45:40:623 -> 0, V-Bus, MB: 4, ID: 0x18FFFB13, EXT: 1, LEN: 8, DATA: 0, 26, 4, 29, 165, 56, 0, 15, 
    Sending status stuff to UDP:  ResponseCode: 174  barr length: 24  Bus: 2
    10:45:40:824 -> 0, V-Bus, MB: 4, ID: 0x18FFFB13, EXT: 1, LEN: 8, DATA: 0, 26, 4, 29, 165, 56, 0, 15, 
    Sending status stuff to UDP:  ResponseCode: 174  barr length: 24  Bus: 2

  5. #1180
    Junior Member
    Join Date
    Feb 2023
    Posts
    11
    I am trying to do a multi-thread with 4.1 teenys. Is this possible with this teensy? This is my code:

    #include <Arduino.h>
    #include <FlexCAN_T4.h>

    FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
    int state = 0;
    int buzzerPin = 15;
    int frequency = 1000;
    bool first = false;
    int N_SAMPLES = 10;

    int MAX_APPS = 1024;

    void control() {
    if(state == 0) {
    state++;
    //mandar dar início ao precharge
    }
    else if(state == 1) {
    state++;
    // digitalWrite(24,HIGH); //ativar o TS
    }
    else if(state == 2) {
    // state++;
    // digitalWrite(25,HIGH); //ativar o R2D mode

    analogWrite(buzzerPin, 0); // Turn off the buzzer for the other half of the period
    delay(4000);
    analogWrite(buzzerPin,256);
    delay(4000);

    }
    else if(state == 3) {
    state = 0;
    }
    }

    void handleAPPS()
    {
    while (1) {
    if(state == 0){
    Serial.println("Entered state 0");
    if(digitalRead(8) == HIGH) {
    control();
    Serial.println("Going out state 0");
    delay(1000);
    }
    delay(1000);
    }
    else if(state == 1){
    Serial.println("Entered state 1");
    if(digitalRead(9) == HIGH) {
    control();
    Serial.println("Going out state 1");
    delay(1000);
    }
    delay(1000);
    }
    else if(state == 2) {
    Serial.println("Entered state 2");
    control();

    if(digitalRead(10) == HIGH) {
    control();
    Serial.println("Going out state 2");
    }
    }
    else if(state == 3) {
    if(!first)Serial.println("Entered state 3"); first = true;

    int v_apps1 = 0;
    int v_apps2 = 0;

    for (int i = 0; i < N_SAMPLES; i++) {
    v_apps1 += analogRead(A17);
    v_apps2 += analogRead(A16);
    delay(1);
    }

    v_apps1 /= N_SAMPLES;
    v_apps2 /= N_SAMPLES;

    double avg = (v_apps1 + v_apps2) / 2.0;
    double value = (avg / MAX_APPS) * 100.0;
    int16_t value_bamo = static_cast<int16_t>((avg/100) * 32767.0);

    // desvio(v_apps1, v_apps2);

    // String output = String(v_apps1, DEC) + "\t" + String(v_apps2, DEC) + "\t" + desvio(v_apps1, v_apps2) + "%";
    // Serial.printf("Value being transmited: %d \n",output);

    uint8_t byte1 = (value_bamo >> 8) & 0xFF; //MSB
    uint8_t byte2 = value_bamo & 0xFF; //LSB

    CAN_message_t msg;
    // definir a mensagem de acordo com o que o BAMOCAR pede
    // torque command value
    msg.id = 0x201;
    msg.len = 3;
    msg.buf[0] = 0x90;
    msg.buf[1] = byte2; //2 bytes for the value: 50% -> 16380
    msg.buf[2] = byte1;
    can1.write(msg);

    // CAN_message_t msg;
    // // definir a mensagem de acordo com o que o BAMOCAR pede
    // // speed command value
    // msg.id = 0x201;
    // msg.len = 3;
    // msg.buf[0] = 0x31;
    // msg.buf[1] = 0xCD;

    // msg.buf[2] = 0x0C; //2 bytes for the value: 100% -> 32767
    // can1.write(msg);
    delay(100);

    }

    delay(10); // Adjust the delay based on your task requirements
    }
    }

    void handleDisplay()
    {
    while (1) {
    // Handle display programming logic
    // ...

    delay(20); // Adjust the delay based on your task requirements
    }
    }

    void setup() {
    Serial.begin(9600);

    can1.begin();
    can1.setBaudRate(125000);

    pinMode(buzzerPin,OUTPUT);
    pinMode(24,OUTPUT);
    pinMode(25,OUTPUT);

    pinMode(40,INPUT);
    pinMode(18,INPUT);
    pinMode(23,INPUT);
    pinMode(10,INPUT);

    analogWrite(buzzerPin, 256); // Set the duty cycle to 50%

    // Start the multitasking system by creating separate tasks
    // Each task runs in an infinite loop and cooperatively yields control to other tasks using delays

    // Task 1: Handling APPS
    Thread appThread(handleAPPS, "APPS", 4096, NULL, 1); // Create a thread with 4KB stack size

    // Task 2: Programming the display
    Thread displayThread(handleDisplay, "Display", 4096, NULL, 1); // Create a thread with 4KB stack size
    }

    void loop() {

    }



    Is it multi-threading possible with teensy 4.1? thanks you for your time

  6. #1181
    Senior Member
    Join Date
    Apr 2020
    Location
    DFW area in Texas
    Posts
    659
    Quote Originally Posted by jmlima44 View Post
    I am trying to do a multi-thread with 4.1 teenys. Is this possible with this teensy? This is my code:

    Code:
    #include <Arduino.h>
    #include <FlexCAN_T4.h>
    
    FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
    int state = 0;
    int buzzerPin = 15;
    int frequency = 1000;
    bool first = false;
    int N_SAMPLES = 10;
    
    int MAX_APPS = 1024;
    
    void control() {
      if (state == 0) {
        state++;
        //mandar dar início ao precharge
      }
      else if (state == 1) {
        state++;
        // digitalWrite(24,HIGH); //ativar o TS
      }
      else if (state == 2) {
        // state++;
        // digitalWrite(25,HIGH); //ativar o R2D mode
    
        analogWrite(buzzerPin, 0); // Turn off the buzzer for the other half of the period
        delay(4000);
        analogWrite(buzzerPin, 256);
        delay(4000);
    
      }
      else if (state == 3) {
        state = 0;
      }
    }
    
    void handleAPPS()
    {
      while (1) {
        if (state == 0) {
          Serial.println("Entered state 0");
          if (digitalRead(8) == HIGH) {
            control();
            Serial.println("Going out state 0");
            delay(1000);
          }
          delay(1000);
        }
        else if (state == 1) {
          Serial.println("Entered state 1");
          if (digitalRead(9) == HIGH) {
            control();
            Serial.println("Going out state 1");
            delay(1000);
          }
          delay(1000);
        }
        else if (state == 2) {
          Serial.println("Entered state 2");
          control();
    
          if (digitalRead(10) == HIGH) {
            control();
            Serial.println("Going out state 2");
          }
        }
        else if (state == 3) {
          if (!first)Serial.println("Entered state 3"); first = true;
    
          int v_apps1 = 0;
          int v_apps2 = 0;
    
          for (int i = 0; i < N_SAMPLES; i++) {
            v_apps1 += analogRead(A17);
            v_apps2 += analogRead(A16);
            delay(1);
          }
    
          v_apps1 /= N_SAMPLES;
          v_apps2 /= N_SAMPLES;
    
          double avg = (v_apps1 + v_apps2) / 2.0;
          double value = (avg / MAX_APPS) * 100.0;
          int16_t value_bamo = static_cast<int16_t>((avg / 100) * 32767.0);
    
          // desvio(v_apps1, v_apps2);
    
          // String output = String(v_apps1, DEC) + "\t" + String(v_apps2, DEC) + "\t" + desvio(v_apps1, v_apps2) + "%";
          // Serial.printf("Value being transmited: %d \n",output);
    
          uint8_t byte1 = (value_bamo >> 8) & 0xFF; //MSB
          uint8_t byte2 = value_bamo & 0xFF; //LSB
    
          CAN_message_t msg;
          // definir a mensagem de acordo com o que o BAMOCAR pede
          // torque command value
          msg.id = 0x201;
          msg.len = 3;
          msg.buf[0] = 0x90;
          msg.buf[1] = byte2; //2 bytes for the value: 50% -> 16380
          msg.buf[2] = byte1;
          can1.write(msg);
    
          // CAN_message_t msg;
          // // definir a mensagem de acordo com o que o BAMOCAR pede
          // // speed command value
          // msg.id = 0x201;
          // msg.len = 3;
          // msg.buf[0] = 0x31;
          // msg.buf[1] = 0xCD;
    
          // msg.buf[2] = 0x0C; //2 bytes for the value: 100% -> 32767
          // can1.write(msg);
          delay(100);
    
        }
    
        delay(10); // Adjust the delay based on your task requirements
      }
    }
    
    void handleDisplay()
    {
      while (1) {
        // Handle display programming logic
        // ...
    
        delay(20); // Adjust the delay based on your task requirements
      }
    }
    
    void setup() {
      Serial.begin(9600);
    
      can1.begin();
      can1.setBaudRate(125000);
    
      pinMode(buzzerPin, OUTPUT);
      pinMode(24, OUTPUT);
      pinMode(25, OUTPUT);
    
      pinMode(40, INPUT);
      pinMode(18, INPUT);
      pinMode(23, INPUT);
      pinMode(10, INPUT);
    
      analogWrite(buzzerPin, 256); // Set the duty cycle to 50%
    
      // Start the multitasking system by creating separate tasks
      // Each task runs in an infinite loop and cooperatively yields control to other tasks using delays
    
      // Task 1: Handling APPS
      Thread appThread(handleAPPS, "APPS", 4096, NULL, 1); // Create a thread with 4KB stack size
    
      // Task 2: Programming the display
      Thread displayThread(handleDisplay, "Display", 4096, NULL, 1); // Create a thread with 4KB stack size
    }
    
    void loop() {
    
    }

    Is it multi-threading possible with teensy 4.1? thanks you for your time
    When posting code in your message, you can use the "#" button (just above the area where you enter the text") to enclose the code in "CODE" tags. Doing so will preserve the formatting, which as you can see above, makes the code much easier to read.

    To answer your primary question (is this type of multi-threading possible with T4.1 ??), the answer is "maybe". I did not actually load/run your sketch, so quick my analysis may be flawed. Also, I've not used threading on the T4.x myself, so I am by no means an expert on the topic. However, I would point out a couple of things to consider:

    - inside of a thread, you should probably be using some kind of thread-safe "threadDelay()" rather than "delay()"

    - your current sketch appears that it will get stuck in state 2 ("state++" is commented out)

    - why use threads at all ?? you could probably keep the "state-driven" logic in "handleApps()" by just calling handleApps() in your "loop()"...then, all it appears that you need is some way to decide when to call "handleDisplay()"

    - if you use a physical display with a driver that allows a framebuffer, you could write to the display asynchronously, then you could call "handleDisplay()" if/as needed (when something changes, or when an update is required)

    Hope that helps . . .

    Mark J Culross
    KD5RXT

  7. #1182
    Junior Member
    Join Date
    May 2023
    Posts
    3
    EDIT: I found that my problem is with my CAN adapter, nothing else as of now. I will update this message later when I am sure there are no other issues, thanks.

    Hi, I'm working on getting my first CAN program to function, and in order to troubleshoot I've set about uploading tonton81's github example "CAN2.0_example_FIFO_with_interrupts.ino" from https://github.com/tonton81/FlexCAN_...interrupts.ino to my Teensy 4.1 board.

    Near as I can tell, I needed to adjust the FlexCan_T4 constructor (I'm coming from the software side of things, I'm working with an electrical engineer on this project) to use the CAN2 option because the schematic I'm working with uses pins 22/23 for TX/RX. My slightly adjusted code is below.

    Code:
    #include <FlexCAN_T4.h>
    FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> Can2; // Pin 22 = TX, Pin 23 = RX
    int x = 0;
    
    void setup(void) {
      Serial.begin(115200); delay(400);
      //pinMode(6, OUTPUT); digitalWrite(6, LOW); /* optional tranceiver enable pin; I commented this out */
      Can2.begin();
      Can2.setBaudRate(250000);
      Can2.setMaxMB(16);
      Can2.enableFIFO();
      Can2.enableFIFOInterrupt();
      Can2.onReceive(canSniff);
      
    }
    
    void canSniff(const CAN_message_t &msg) {
      Serial.print("MB "); Serial.print(msg.mb);
      Serial.print("  OVERRUN: "); Serial.print(msg.flags.overrun);
      Serial.print("  LEN: "); Serial.print(msg.len);
      Serial.print(" EXT: "); Serial.print(msg.flags.extended);
      Serial.print(" TS: "); Serial.print(msg.timestamp);
      Serial.print(" ID: "); Serial.print(msg.id, HEX);
      Serial.print(" Buffer: ");
      for ( uint8_t i = 0; i < msg.len; i++ ) {
        Serial.print(msg.buf[i], HEX); Serial.print(" ");
      } Serial.println();
    }
    
    void loop() {
      Can2.events();
    
      static uint32_t timeout = millis();
      if ( millis() - timeout > 200 ) {
        CAN_message_t msg;
        msg.id = random(0x1,0x7FE);
        for ( uint8_t i = 0; i < 8; i++ ) msg.buf[i] = i + 1;
        Can2.write(msg);
        timeout = millis();
      }
      
      Serial.print("HELLO WORLD\n");
      delay(1000);
      Serial.print(x);
      Serial.print("\n");
      x++;
    }
    My serial output functions as I expect, with incrementing x values, however when I run PCAN-View software to see if there is activity on the CAN bus, I don't see any data being written (I am new to PCAN-View, but I think I have that configured correctly regardless). I have verified that the wiring on the CAN adapter is accurately hooked up according to the documentation. How can I see the CAN output that I need? Screenshots of my PCAN-View settings are below.

    Click image for larger version. 

Name:	pcan_view_settings.PNG 
Views:	37 
Size:	46.0 KB 
ID:	31240
    Click image for larger version. 

Name:	pcan_view_settings2.PNG 
Views:	32 
Size:	37.5 KB 
ID:	31241

    Things I've already tried include adjusting the acceptance filter in the above settings to include Extended (29-bit) IDs and uncommenting out the pinMode(6, Output) line (in tonton81's original code, this line is not commented out, I've since read elsewhere that this line is unnecessary for what I'm doing). On another note, I am suspicious that there is something that pertains to the transceiver my electrical engineer is using that I'll need to edit, though I didn't find anything that relates to transceivers in the FlexCAN_T4 library.

    I'm open to adjusting anything about my strategy, as I said I'm new to CAN buses and it is likely that, from an experienced developer's perspective, I am missing something basic. Let me know if you require more info to help and thank you for any help.
    Last edited by tmc11; 06-07-2023 at 04:15 PM. Reason: Learned that the # button is for code formatting

  8. #1183
    Junior Member
    Join Date
    May 2023
    Posts
    3
    Evidently I can't edit my own message from yesterday? Regardless, this issue was solved by troubleshooting my hardware. I don't think I can delete my message either, so I guess we'll have to let it be a small monument to software engineers who haven't yet learned how to verify board functionality. Fwiw, I should have asked for help from an electrical engineer sooner...

  9. #1184
    Junior Member
    Join Date
    Feb 2023
    Posts
    11

    Problems sending a message

    Hi I am programming my teensy 4.1 in order to send messages to my motor. The goal is to change the motor's speed but nothing is happenning. I attached the CAN manual.
    Basically, I am reading a value from a pontentiometer and converting to the proportion of my motor and then I send it through a CAN message. This is my code:

    Code:
    #include <Arduino.h>
    #include <FlexCAN_T4.h>
    
    FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
    
    int max_v = 1023;
    
    void setup() {
      Serial.begin(9600);
    
      can1.begin();
      can1.setBaudRate(500000);
    }
    
    void loop() {
    
      int val = analogRead(15);
      
      uint16_t value_bamo = val * 32767.0 / max_v;
      
      uint8_t byte1 = (value_bamo >> 8) & 0xFF; //MSB
      uint8_t byte2 = value_bamo & 0xFF; //LSB
    
      Serial.print("byte1: ");
      Serial.print(byte1,HEX);
      Serial.print("\n byte2: ");
      Serial.print(byte2,HEX);
      //definir a mensagem de acordo com o que o BAMOCAR pede
      //speed command value
    
      Serial.printf("\n Value sent: %d",value_bamo);
      
      CAN_message_t msg;
      msg.id = 0x201;
      msg.len = 3;
      msg.buf[0] = 0x31;
      msg.buf[1] = (byte2); 
      msg.buf[2] = (byte1);
      
      can1.write(msg);
    
      Serial.println("\n Message sent!");
      delay(1000);
    
      CAN_message_t message2;
        if (can1.read(message2)) {
        Serial.print("Received message with ID 0x");
        Serial.print(message2.id, HEX);
        Serial.print(": ");
        for (int i = 0; i < message2.len; i++) {
          Serial.print(message2.buf[i]);
        }
        Serial.print('\n');
      }
    }
    Attached Files Attached Files

  10. #1185
    Junior Member
    Join Date
    Nov 2013
    Posts
    12
    @jmlima44 you should at least call can1.events(); at every loop and some other settings maybe missing.

    I'd suggest to first start from using the existing samples and ensure you are able to run them on your hardware:
    e.g. https://github.com/tonton81/FlexCAN_...aster/examples

  11. #1186
    Junior Member
    Join Date
    Jun 2023
    Location
    Navarra - Spain
    Posts
    2
    I am implementing the FlexCAN_T4 library, and I wanted to control that canBus.begin(); it wakes up or starts and like many other controllers... you can assign a variable: StatusCAN = canBus.begin(); and then be able to put a message.
    It's not possible ?
    I have seen that it worked before...
    Can someone give me a bit of their wisdom.
    a flying smile

  12. #1187
    Junior Member
    Join Date
    Jul 2023
    Location
    Brake, Germany
    Posts
    4
    Hello tonton81

    The FlexCAN library works like a charm, setup and send & receive worked out of the box.

    I would like to use the Teensy as a testing device, so I would need to switch the can tranceivers on and off with different baud rates and in listen/normal mode (e.g. to scan for baud rates). Getting also some information about error frames and bus heavy / bus dead would be great.

    Is that anyhow supported by the FlexCAN library, or what else I would have to do while not interfearing the library functionality. It is possible at all?

    Maybe you could give me some advice?

    thanks in advance!

  13. #1188
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    4,095
    well it does support self-recovery, you could just set the baudrates in listen-only mode and check if the callback fires. callbacks only fire for valid receptions

  14. #1189
    Junior Member
    Join Date
    Jul 2023
    Location
    Brake, Germany
    Posts
    4
    Good morning to Montreal

    I'm just reading the header file.. So the magic would be to use the setBaudRate() method on an existing can object to change its baudrate and its mode (normal or listen) to switch between normal and listen?

  15. #1190
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    4,095
    yes it can be done dynamically the baudrates

  16. #1191
    Junior Member
    Join Date
    Jul 2023
    Location
    Brake, Germany
    Posts
    4
    I've already given it a try - it works. As you said, Bus off I can identify on not having any msgs received. So at last I need to find the error flag in the documentation :-)

  17. #1192
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    4,095
    the error info has been added there if you check the source file, as per another user request, it has it's own callback

  18. #1193
    Junior Member
    Join Date
    Jul 2023
    Location
    Brake, Germany
    Posts
    4
    It's working ! Thanks a lot & have a nice weekend

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •