need help programing Imu



  • I need some help with an assignment for a class. The class is an introduction to the Internet of Things. I have no coding experience outside of an introduction to HTML, CSS, and JavaScript. The instructor is leaning heavily towards using the M5stickC to code and demonstrate knowledge of how it works. The problem is that I have no experience and can't figure out what I am doing wrong.

    The assignment is to program the M5stickC built-in LED to blink once the imu senses a drop larger than 10cm.

    any advice is greatly appreciated.
    Im using the M5stickC P2

    #include "M5StickCPlus2.h"

    float accXSq = 0.0F;
    float accYSq = 0.0F;
    float accZSq = 0.0F;

    float accMagSq = 0.0F;
    float accMag = 0.0F;
    float MaxAccMag = 0.0F;

    void setup() {
    auto cfg = M5.config();
    StickCP2.begin(cfg);
    StickCP2.Display.setRotation(1);
    StickCP2.Display.setTextColor(GREEN);
    StickCP2.Display.setTextDatum(middle_center);
    StickCP2.Display.setFont(&fonts::FreeSansBold9pt7b);
    StickCP2.Display.setTextSize(1);
    #define BUILTIN_LED 19
    }

    void loop(void) {
    auto imu_update = StickCP2.Imu.update();
    if (imu_update) {
    StickCP2.Display.setCursor(0, 40);
    StickCP2.Display.clear(); // Delay 100ms 延迟100ms

        auto data = StickCP2.Imu.getImuData();
    
        accXSq = sq(accX);
        accYSq = sq(accY);
        accZSq = sq(accZ);
    
        accMagSq = accXSq + accYSq + accZSq;
        accMag = sqrt(accMagSq);
    
    
        if(accMag > maxAccMag){
          maxAccMag = accMag;
        }
    
        if(data.accel.z > 0.15){
          digitalWrite(LED_BUILTIN, HIGH);
          delay(100);
          digitalWrite(LED_BUILTIN, LOW);
          delay(100);
        }
    
        Serial.printf("ax:%f  ay:%f  az:%f\r\n", data.accel.x, data.accel.y,
                      data.accel.z);
        Serial.printf("gx:%f  gy:%f  gz:%f\r\n", data.gyro.x, data.gyro.y,
                      data.gyro.z);
    
        StickCP2.Display.printf("IMU:\r\n");
        StickCP2.Display.printf("%0.2f %0.2f %0.2f\r\n", data.accel.x,
                                data.accel.y, data.accel.z);
        StickCP2.Display.printf("%0.2f %0.2f %0.2f\r\n", data.gyro.x,
                                data.gyro.y, data.gyro.z);
        StickCP2.Display.println(maxAccMag);
    }
    delay(100);
    

    }