RP2040 Crashes randomly

I am building a rocket and using the RP2040 as a flight computer. I realized my logging routine takes 30ms which significantly lowers my update rate so I decided to move logging to the second core. I now encounter the RP2040 crashing (both cores) sometime after setting the LED high in line 81 (crashing in the sense of not updating anymore). I checked if it might be stuck in a while loop or smth which it isn't so at this point I am asking for help.

This is my main.ino file (var.h below)
I'd be happy for anyones help

#include "var.h"

void logger_loop() {  
  Logger logger;

  //SD Check
  logger.Init();
  while (!logger.SD_InitSC) {
    logger.Init();
    digitalWrite(LEDG, HIGH);
    sleep_ms(500);
    digitalWrite(LEDG, LOW);
    //No second sleep_ms because logger.Init takes its time
  }
     
  logger.Read_Config();
  while (!logger.config_read_succes) {
    logger.Read_Config();
    digitalWrite(LEDR, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDB, HIGH);
    sleep_ms(500);
    digitalWrite(LEDR, LOW);
    digitalWrite(LEDG, LOW);
    digitalWrite(LEDB, LOW);
    //No second sleep_ms because reading takes its time
  } 
  //Transfer read values 
  pid.KpY = logger.KpY_logger;
  pid.KiY = logger.KiY_logger;
  pid.KdY = logger.KdY_logger;
  
  pid.KpZ = logger.KpZ_logger; 
  pid.KiZ = logger.KiZ_logger;
  pid.KpZ = logger.KdZ_logger; 
 
  
  // If SD Check is complete green LED gets set high as well
  digitalWrite(LEDG, HIGH);
  loggerSetupDone = true;

  while(true) {
    //Log Values and send them
    logger.Write(1.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, "LOGGER.CSV");
  }
}

void setup() {   
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  pinMode(Parachuet_Pin, OUTPUT);

  digitalWrite(LEDB, LOW);
  digitalWrite(LEDR, LOW);
  digitalWrite(LEDG, LOW);

  digitalWrite(LED_PIN, LOW);
  //5min sleep_ms to get away from the rocket 
  //sleep_ms(300000);
  
  bmp.Init();
  sleep_ms(400);
  
  mpu.Init();
  mpu.alpha = 0;
  sleep_ms(400);

  sleep_ms(5000);
  multicore_launch_core1(logger_loop);
  
  while(!loggerSetupDone) sleep_ms(1);
  
  StartUpServos();
  
  //If Rocket is ready turn Status LED is on 
  if (bmp.BMP_InitSC && mpu.MPU_In<itSC && loggerSetupDone) {
    digitalWrite(LED_PIN, HIGH);
  }
}


void loop() {
  ST = millis();
  GetValues();
  if (stateMachine.State == 1) {
    //Do the Corrections 
    //Calculate the pid value and add/subtract the middle pos so the PID Value is based on the center POS
    //Plus or minus just depnding on weather the PID Value already has the correct algebraic sign (You could also just invert the IMU inptut for the PID) 
    //Or you could mount the servo 180 deg upside down
    uy = pid.PIDy(rotY) + CenterPosY;
    uz = pid.PIDz(rotZ) + CenterPosZ;

    uy = constrain(uy, 80, 100);
    uz = constrain(uz, 80, 100);
    ServoY.write(uy);
    ServoZ.write(uz);
  }

  stateMachine.Update(accelX,accelY,accelZ,altitude_m);
  mpu.alpha = stateMachine.alpha;
}

void GetValues() {
  //Get all the values and write them in the Corresponding Variables
  mpu.filterData();

  accelX = mpu.accelX;
  accelY = mpu.accelY;
  accelZ = mpu.accelZ;

  rotY = mpu.rotFilteredY;
  rotZ = mpu.rotFilteredZ;

  bmp.getValues();
  altitude_m = bmp.altitude_m;
}


void StartUpServos() {
  ServoY.attach(3);
  ServoZ.attach(5);
  
  ServoY.write(CenterPosY);
  ServoZ.write(CenterPosZ);

  sleep_ms(1000);
 
  ServoY.write(minY);
  sleep_ms(200);
  ServoY.write(CenterPosY);
  sleep_ms(200);
  
  ServoY.write(maxY);
  sleep_ms(200);
  ServoY.write(CenterPosY);
  sleep_ms(200);
  
  ServoZ.write(minZ);
  sleep_ms(200);
  ServoZ.write(CenterPosZ);
  sleep_ms(200);
  ServoZ.write(maxZ);
  sleep_ms(200);
  ServoZ.write(CenterPosZ);
  sleep_ms(600);

  ServoY.write(CenterPosY);
  ServoZ.write(CenterPosZ);
}

This is my Variable header script

#ifndef var_h
#define var_h

#include <WiFiNINA.h>
/*Needed for the RGB LED because: 
The Nano RP2040 Connect uses the u-blox NINA-W102 module for Wi-Fi connectivity. 
But this module is also just a standard ESP32 microcontroller. 
No reason to let those precious GPIO pins go to waste. 
So the RGB LED is connected to the NINA module, 
rather than taking up the RP2040 pins. 
So in order to blink these LEDs it's necessary for the RP2040 to communicate with the NINA module. 
That communication is handled by the WiFiNINA library.
*/

#include <pico/multicore.h>

#include <Servo.h>

#include "BMP.h"
#include "IMU.h"
#include "Logger.h"
#include "Calculate.h"
#include "StateMachine.h"
#include "PID.h"


PID pid;
BMP bmp;
IMU mpu;
StateMachine stateMachine;
Calculate calc;

Servo ServoY;
Servo ServoZ;

//Servos
float CenterPosY = 90;
float CenterPosZ = 90;

float maxY = 100;
float minY = 80;

float maxZ = 100;
float minZ = 80;

float uy;
float uz;
  
//Value Variables
double altitude_m;
 
float accelX;
float accelY;
float accelZ;

float rotY;
float rotZ;

float driftY = -0.53;
float driftZ = -0.08;


const int LED_PIN = 8;
const int Parachuet_Pin = 7;

float ST = 0.0;

//File Log
const String LOG_FILE = "LOGGER.csv";

bool loggerSetupDone = false;

#endif var_h

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

A post was split to a new topic: Rp2040 thrown away

I couldn't make the RGB LEDs work from core1 either. My guess is that it's because those LEDs are attached to the U-blox Nina W102chip, so behind those digitalWrite(LEDG, HIGH); calls is some SPI? Serial? communication code which isn't multicore safe.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.