Hello, everyone,
I want to create a project with an Arduino Nano 33 IOT to send the coordinates of the gyyroscope x,y and z through the serial port (displaying on the serial monitor). To do this, I'm using an example sketch created by Riccardo Rizzo. I can see that the coordinates are displayed on the serial monitor when I move the board to a certain position, but the values are not maintained and return to previous values.
I searched the internet for some help and found this video tutorial which corresponds to what I want but the board used is a Nano Ble and not a Nano 33 IOT even though they use the same LM9DS3 library.
Can anyone help me solve this problem?
Thank you very much!
The gyro measures rates of rotation about three axes. The rates have nothing to do with coordinates. Likewise, the accelerometer measures rates of change of velocity along three axes, not coordinates.
/*
Arduino LSM6DS3 - Simple Gyroscope
This example reads the gyroscope values from the LSM6DS3
sensor and continuously prints them to the Serial Monitor
or Serial Plotter.
The circuit:
- Arduino Uno WiFi Rev 2 or Arduino Nano 33 IoT
created 10 Jul 2019
by Riccardo Rizzo
This example code is in the public domain.
*/
#include <Arduino_LSM6DS3.h>
void setup() {
Serial.begin(9600);
while (!Serial);
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
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");
}
void loop() {
float x, y, z;
if (IMU.gyroscopeAvailable()) {
IMU.readGyroscope(x, y, z);
Serial.print(x);
Serial.print('\t');
Serial.print(y);
Serial.print('\t');
Serial.println(z);
}
}
I have the board built into a box with a video camera that moves with a servo motor. I want the board to send the coordinate data to a patch I've created in Max/msp in a synthesis image application. The problem is that when the camera changes position and stops, the values change while there is movement but do not stabilise when the camera stops.
What do you mean by coordinate data? The sensor you have does not measure coordinates.
If properly calibrated, the sensor can be used with fusion filter code to estimate 3D orientation angles for a short while. Is that what you are thinking?
Thank you for your response. I tried creating a code that would compensate for the readings, but it is still not stable enough to produce accurate readings of the positions the camera assumes. This can be observed in the point cloud generated by my patch in Max/MSP. I am attaching the code and the link to the project video.
#include <WiFiNINA.h> // Biblioteca Wi-Fi para a Nano 33 IoT
#include <WiFiUdp.h> // Biblioteca UDP para envio de pacotes
#include <OSCMessage.h> // Biblioteca OSC para mensagens OSC
#include <Arduino_LSM6DS3.h> // Biblioteca para o giroscópio LSM6DS3 integrado
// Definições de rede Wi-Fi
const char* ssid = "RedePrivadaArduino";
const char* password = "senha1234";
// Definições de IP e porta para UDP
IPAddress localIp(192, 168, 4, 21); // IP desejado para a Nano 33 IoT
IPAddress outIp(192, 168, 4, 7); // IP do computador com Max/MSP
IPAddress gateway(192, 168, 4, 1); // IP do gateway (roteador)
IPAddress subnet(255, 255, 255, 0); // Máscara de sub-rede
const unsigned int localPort = 8888; // Porta local para envio de UDP
const unsigned int outPort = 2222; // Porta para envio OSC ao Max/MSP
// Cliente UDP para enviar os pacotes OSC
WiFiUDP Udp;
float gyroX, gyroY, gyroZ;
void setup() {
Serial.begin(9600);
delay(1000);
// Configurar IP estático antes de conectar
WiFi.config(localIp, gateway, subnet);
// Conectar ao Wi-Fi
Serial.print("Conectando-se a ");
Serial.println(ssid);
int retryCount = 0;
const int maxRetries = 5; // Número de tentativas de conexão antes de reiniciar o Wi-Fi
WiFi.begin(ssid, password);
// Tentar conectar até o limite de tentativas
while (WiFi.status() != WL_CONNECTED) {
delay(2000); // Aguardar 2 segundos entre as tentativas
Serial.print("Tentativa de conexão ");
Serial.print(retryCount + 1);
Serial.println(" falhou. Tentando novamente...");
retryCount++;
// Se o número de tentativas ultrapassar o máximo, reiniciar o Wi-Fi e recomeçar as tentativas
if (retryCount >= maxRetries) {
Serial.println("Número máximo de tentativas atingido. Reiniciando Wi-Fi...");
WiFi.disconnect();
delay(2000); // Aguardar um pouco antes de tentar novamente
WiFi.begin(ssid, password); // Recomeçar a tentativa de conexão
retryCount = 0; // Reiniciar o contador de tentativas
}
}
Serial.println("\nConectado ao Wi-Fi!");
Serial.print("Endereço IP da Nano 33 IoT: ");
Serial.println(WiFi.localIP());
// Inicializar o cliente UDP com a porta local
Udp.begin(localPort);
// Inicializar o giroscópio
if (!IMU.begin()) {
Serial.println("Falha ao iniciar o giroscópio LSM6DS3!");
while (1); // Travar o programa se o giroscópio não for inicializado
}
Serial.println("Giroscópio LSM6DS3 iniciado com sucesso.");
}
void loop() {
if (IMU.gyroscopeAvailable()) {
IMU.readGyroscope(gyroX, gyroY, gyroZ);
// Exibir as coordenadas no Monitor Serial
Serial.print("Giroscópio - X: ");
Serial.print(gyroX);
Serial.print(" Y: ");
Serial.print(gyroY);
Serial.print(" Z: ");
Serial.println(gyroZ);
// Criar e enviar mensagem OSC com os valores X, Y e Z
OSCMessage gyroMsg("/gyroscope");
gyroMsg.add(gyroX);
gyroMsg.add(gyroY);
gyroMsg.add(gyroZ);
Udp.beginPacket(outIp, outPort);
gyroMsg.send(Udp); // Enviar a mensagem OSC corretamente formatada
Udp.endPacket();
gyroMsg.empty(); // Limpar a mensagem para reutilização
// Confirmação de envio no Monitor Serial
Serial.println("Mensagem OSC enviada para o Max/MSP");
delay(10); // Intervalo entre envios para evitar sobrecarga
}
}
Knowing that my project involves sending OSC messages via wifi, at this point we'll simplify matters and focus on solving the problem by displaying coordinates on the serial monitor.
So, I want the x, y and z coordinate values for the board positions to be displayed on the serial monitor. I can see from the example code in the Arduino LSM9DS3 library that the x, y and z coordinates change when I move the board to a certain position, but then return to their initial values. This prevents me from being able to determine the coordinates of each position the board assumes.
The accelerometer's X, Y can tell you the pitch and roll - and whether it's upside-down.
A magnetometer can give you an idea of position - like a compass heading. (That may be subject to local magnetic fields.)