Hi everyone,
I am trying to program a 3DOF robotic arm (using 3 servo motors). I would like to move the servos using an Android app (developed by Ionic 5, Angular and Capacitor), by means of bluetooth (HC-05).
I developed a 3tabs app, in whose there is an interface with 6 buttons (Up and Down moving the first servo for vertical movements; Left and Right moving the second servo for horizontal movements; Forward and Backward moving the third servo), an interface for simply text sending (digited by keyboard) and the last interface that allows you to create a personalised interface (you can choose the matrix for your customised keypad, the number of servo you want to control and, for each button you want to activate, you can choose label, icon, colour and positions for each servo). I'll attach a screenshot for each tab.
To communicate with Arduino (code attached), the App sends a JSON file in which are reported all the information that Arduino needs to move servos as requested (example of JSON received by Arduino, printed in the command control:
deserializeJson() ok:
{"action":"manual","id":1,"update":10} // action = identified the corrisponding tab; id = servo you want to move; update = I defined an angula step of 10 degrees
updateServoAngle: Servo #1 wants to move at 103
updateServoAngle: Servo #1 middle movement at 0
updateServoAngle: Servo #1 moved at 103
Servo #1 moved at 103
).
I am having a problem with servo movements: also using a power supply, servos respond correctly (moving according to what received via Bluetooth) few times and then start to move randomly (for instance, instead of completing the 10-degrees movement, servo either just makes a small movement back- and forward or just starts rotating continuously never-ending).
Seen that, I tried just with only one servo, directly powered by Arduino, but the problem persists (the baud rate is 9600).
Can anyone help me with that?
Thank you.
Giammarco
Arduino Code:
#define ANGLE_STEP 10 // 10 degrees steps
#define ANGLE_MIN 10
#define ANGLE_MAX 170
#define BT_BEGIN String("[@")
#define BT_END String("@]")
#define JSON_SIZE 96
#include <ArduinoJson.h> // Json Library (installed with Tools --> Manage Libraries --> typed "json" --> install the "Arduinojson by Benoit Blanchon"
#include <SoftwareSerial.h> // Bluetooth library
SoftwareSerial bluetooth(11, 10); // BlueTooth RX ,TX
#include <Servo.h> // Servo library
#define NO_OF_SERVOS 3 // See: https://forum.arduino.cc/index.php?topic=444373.0
Servo servo01, servo02, servo03; // Servos names
Servo servos[NO_OF_SERVOS] = {servo01, servo02, servo03}; // Servo Array
// Input here Servo pins
int servos_pins[NO_OF_SERVOS] = {12, 13, 7};
// DEFAULT angles of servos
int servos_angles[NO_OF_SERVOS] = {90, 90, 90};
// https://www.arduino.cc/reference/en/libraries/servo/read/
void setup(){
// Bluetooth setup
Serial.begin(9600);
bluetooth.begin(9600);
Serial.println("The bluetooth gates are open.\n Connect to HC-05 from any other bluetooth device with 1234 as pairing key!.\nReady ...\n");
delay(1000);
// Servo setup
for(int i = 0; i < NO_OF_SERVOS; i++){
// attach servo to pin
servos[i].attach(servos_pins[i]);
// move servo to default position
delay(50);
updateServoAngle(i, servos_angles[i]);
delay(50);
}
}
void loop(){
while(true){
DynamicJsonDocument json = rxBt();
String action = json["action"];
if(String("position") == action){
JsonArray servosAngles = json["angles"];
size_t servosAnglesSize = servosAngles.size();
for(int i=0; i<NO_OF_SERVOS && i<servosAnglesSize; i++){
updateServoAngle(i, servosAngles[i]);
delay(500);
}
} else if(String("manual") == action){
int servoId = json["id"];
int servoUpdate = json["update"];
int currentServoAngle = servos[servoId].read();
int servoPosition = max(ANGLE_MIN, min(currentServoAngle + servoUpdate, ANGLE_MAX));
updateServoAngle(servoId, servoPosition);
Serial.println("Servo #" + String(servoId) + " moved at " + String(servoPosition));
delay(500);
} else if(String("get_keypads") == action){
//code still in development
}
}
}
void updateServoAngle(int servoId, int newAngle){
Serial.println("updateServoAngle: Servo #" + String(servoId) + " wants to move at " + String(newAngle));
int servoEndAngle = max(ANGLE_MIN, min(newAngle, ANGLE_MAX)); // checks that the number is between 0 and 180
int currentAngle = servos[servoId].read();
while(currentAngle != servoEndAngle){
int direction = servoEndAngle > currentAngle ? +1 : -1;
currentAngle = (abs(servoEndAngle-currentAngle) >= ANGLE_STEP) ? currentAngle+ANGLE_STEP*direction : servoEndAngle;
servos[servoId].write(currentAngle);
//servos[servoId].writeMicroseconds(currentAngle);
delay(200);
Serial.println("updateServoAngle: Servo #" + String(servoId) + " middle movement at " + String(servos[servoId].read()));
}
Serial.println("updateServoAngle: Servo #" + String(servoId) + " moved at " + String(servoEndAngle));
}
// Read from the bluetooth
DynamicJsonDocument rxBt(){
DynamicJsonDocument doc(JSON_SIZE);
String rxBt;
while (true){
delay(500);
if (bluetooth.available()){
rxBt = bluetooth.readString();
const char* json = rxBt.c_str();
DeserializationError error = deserializeJson(doc, json);
if (error) {
Serial.println("deserializeJson() failed: ");
Serial.println(rxBt);
Serial.println(error.c_str());
} else {
Serial.println("deserializeJson() ok: ");
Serial.println(rxBt);
return doc;
}
}
}
}




