DhruvaG2000 / SmartDuinoSensorHub

An Open Source Project that aims to provide a Smart Sensor Hub with an extremely intuitive user interface using gestures and an OLED Display
MIT License
1 stars 0 forks source link

mbed: Serial debugging #2

Open DhruvaG2000 opened 3 years ago

DhruvaG2000 commented 3 years ago

I am unable to get any serial print logs via USB cable while using mbed RTOS. Here's a small code to demonstrate this issue:

#include <Arduino.h>
#include <mbed.h>
#include <Arduino_LSM9DS1.h>

rtos::Thread thread1;
rtos::Thread thread2;

mbed::DigitalOut led1(LED1);
mbed::DigitalOut led2(LED2);

void loopGyro()
{
  float x, y, z;
  while (true)
  {
    if (IMU.gyroscopeAvailable())
    {
      IMU.readGyroscope(x, y, z);
      led2 = !led2;
      Serial.print(x);
      Serial.print('\t');
      Serial.print(y);
      Serial.print('\t');
      Serial.println(z);
    }
    delay(1000);
    /* code */
  }
}

void loopAccel()
{
  float x, y, z, delta = 0.05;
  while (true)
  {
    if (IMU.accelerationAvailable())
    {
      IMU.readAcceleration(x, y, z);
      led1 = !led1;
      if (y <= delta && y >= -delta)
        Serial.println("flat");
      else if (y > delta && y < 1 - delta)
        Serial.println("tilted to the left");
      else if (y >= 1 - delta)
        Serial.println("left");
      else if (y < -delta && y > delta - 1)
        Serial.println("tilted to the right");
      else
        Serial.println("right");
    }
    delay(1500);
    /* code */
  }
}

int main()
{
  // -------------------setup-------------
  Serial.begin(9600);
  Serial.println("Started");

  if (!IMU.begin())
  {
    Serial.println("Failed to initialize IMU!");
  }

  Serial.print("Accelerometer sample rate = ");
  Serial.print(IMU.accelerationSampleRate());
  Serial.println(" Hz");
  Serial.println();
  Serial.println("Acceleration in g's");
  Serial.println("X\tY\tZ");

  Serial.println("Started gyro");

  Serial.print("Gyroscope sample rate = ");
  Serial.print(IMU.gyroscopeSampleRate());
  Serial.println(" Hz");
  Serial.println();
  Serial.println("Gyroscope in degrees/second");
  Serial.println("X\tY\tZ");
  // -------------------setup-------------

  thread1.start(loopGyro);
  thread2.start(loopAccel);
}

In this code, the LEDs do blink but not in an expected manner neither in a regular way. It seems as though the Real time/ parallel execution is not really happening here.

DhruvaG2000 commented 3 years ago

currently not planning to use RTOS as the MCU seems incapable as it has only one core.