Hello everyone, I'm making a prosthetic robotic arm controlled by an EEG (Nuerosky Mindwave 2) and I'm having problems with the servomotors and I think it's because of the programming.
1- I'm using 4 MG995 servomotors and 1 DS3225mg servomotor
2- 4 of the servomotors (all MG995) don't stop turning and the DS3225mg servomotor doesn't turn and I don't understand the reason, whether it's because of incompatibility or because of the power supply.
Code:
#include <SoftwareSerial.h>
#include <Servo.h>
// Definición de los servomotores
Servo pinkie;
Servo ring;
Servo middle;
Servo index;
Servo thumb;
// Estados lógicos para cada dedo
int pinkieL = 180;
int pinkieH = 90;
int ringL = 180;
int ringH = 80;
int middleL = 0;
int middleH = 126;
int indexL = 100;
int indexH = 15;
int thumbL = 0;
int thumbH = 123;
#define BAUDRATE 57600 // Velocidad en 57600 baudios para comunicación Bluetooth
#define DEBUGOUTPUT 0 // Habilitar lectura de bluetooth para depuración
// Variables de CHECKSUM
byte generatedChecksum = 0;
byte checksum = 0;
int payloadLength = 0;
byte payloadData[64] = {0};
byte poorQuality = 0;
byte attention = 0;
byte meditation = 0;
// Variables del sistema
long lastReceivedPacket = 0;
boolean bigPacket = false;
SoftwareSerial bluetooth(10, 11); // RX, TX
void setup() {
// Inicializar los servos en los pines correspondientes
pinkie.attach(9);
ring.attach(6);
middle.attach(5);
index.attach(3);
thumb.attach(13);
// Inicializar la comunicación serial con el módulo Bluetooth
bluetooth.begin(BAUDRATE);
Serial.begin(9600); // Para depuración
Serial.println("Sistema iniciado. Esperando datos de NeuroSky...");
// Inicializar los servomotores en la posición abierta
pinkie.write(pinkieL);
ring.write(ringL);
middle.write(middleL);
index.write(indexL);
thumb.write(thumbL);
// LEER DATOS DEL BLUETOOTH
delay(2000);
}
byte ReadOneByte() {
int ByteRead;
while (!bluetooth.available());
ByteRead = bluetooth.read();
#if DEBUGOUTPUT
Serial.print((char)ByteRead); // Echo del byte para depuración
#endif
return ByteRead;
}
void loop() {
// Buscar bytes
if (ReadOneByte() == 170) {
if (ReadOneByte() == 170) {
payloadLength = ReadOneByte();
if (payloadLength > 169) // Payload length can not be greater than 169
return;
generatedChecksum = 0;
for (int i = 0; i < payloadLength; i++) {
payloadData[i] = ReadOneByte(); // Leer payload en memoria
generatedChecksum += payloadData[i];
}
checksum = ReadOneByte(); // Leer byte de checksum
generatedChecksum = 255 - generatedChecksum; // Tomar complemento uno del checksum generado
if (checksum == generatedChecksum) {
poorQuality = 200;
attention = 0;
meditation = 0;
for (int i = 0; i < payloadLength; i++) { // Parsear el payload
switch (payloadData[i]) {
case 2:
i++;
poorQuality = payloadData[i];
bigPacket = true;
break;
case 4:
i++;
attention = payloadData[i];
break;
case 5:
i++;
meditation = payloadData[i];
break;
case 0x80:
i = i + 3;
break;
case 0x83:
i = i + 25;
break;
default:
break;
}
}
#if DEBUGOUTPUT
Serial.print("PoorQuality: ");
Serial.print(poorQuality, DEC);
Serial.print(" Attention: ");
Serial.print(attention, DEC);
Serial.print(" Time since last packet: ");
Serial.print(millis() - lastReceivedPacket, DEC);
Serial.println();
#endif
if (bigPacket) {
if (poorQuality == 0) {
// Determinar el gesto basado en el nivel de atención
switch (attention / 20) {
case 0: // Open
pinkie.write(pinkieL);
ring.write(ringL);
middle.write(middleL);
index.write(indexL);
thumb.write(thumbL);
break;
case 1: // Grip (thumb and index)
pinkie.write(pinkieL);
ring.write(ringL);
middle.write(middleL);
index.write(indexH);
thumb.write(thumbH);
break;
case 2: // Grip (thumb and middle)
pinkie.write(pinkieL);
ring.write(ringL);
middle.write(middleH);
index.write(indexL);
thumb.write(thumbH);
break;
case 3: // Point (with index)
pinkie.write(pinkieH);
ring.write(ringH);
middle.write(middleH);
index.write(indexL);
thumb.write(thumbH);
break;
case 4: // Closed
pinkie.write(pinkieH);
ring.write(ringH);
middle.write(middleH);
index.write(indexH);
thumb.write(thumbH);
break;
}
}
}
bigPacket = false;
} else {
// Error de Checksum
Serial.println("Error de Checksum");
}
}
}
}