Hello! I am working on a project for school that uses the Braccio robotic arm. We replaced the gripper on the end with our own design and added the SparkFun robotic finger sensor v2 (attached below). The issue we're having is that when we write the if statement (in void loop) for controlling the gripper servo positions based on pressure being applied to the object picking it up, it ignores the if statement when the Braccio library or any of the servos from the Braccio are called to move.
The pressure code with the two servos in the gripper works when it's in its own code.
The two servos in the gripper work with the Braccio simplemovements pre-written code.
It does not work when the if statement is written in and I have no idea why. Is someone able to help? I am fairly new to Arduino and am trying my best. I feel like I'm just missing something simple. Any help will be greatly appreciated. I attached the codes below.
Here is the sensor: Robotic Finger Sensor v2 - SPX-14687 - SparkFun Electronics
Pressure Code w/out Braccio: Works
#include <Servo.h>
#include <Wire.h>
#include "SparkFun_LPS25HB_Arduino_Library.h"
#include "SparkFun_VCNL4040_Arduino_Library.h"
LPS25HB pressureSensor;
VCNL4040 proximitySensor;
const int pressureThreshold = 415;
const int proximityThreshold = 12000;
// Initialize servo objects
Servo thumb;
Servo finger;
const int thumbMaxAngle = 80;
const int thumbMinAngle = 0;
const int fingerMaxAngle = 180;
const int fingerMinAngle = 110;
void setup() {
// Attach servo motors to pins
finger.attach(7);
thumb.attach(4);
// Set initial positions of servo motors
finger.write(fingerMaxAngle); // Set to 90 degrees
thumb.write(thumbMaxAngle); // Set to 90 degrees
pressureSensor.begin();
proximitySensor.begin();
// Initialize serial communication
Serial.begin(9600);
Wire.begin();
Wire.setClock(400000);
}
void loop() {
// Read proximity sensor value
int proximityValue = proximitySensor.getProximity();
// Read pressure sensor value
int pressureValue = pressureSensor.getPressure_hPa();
// Check if both thresholds are met
if (proximityValue < proximityThreshold && pressureValue < pressureThreshold) {
// Decrease servo angles
int currentAngle;
// Decrease finger servo angle
currentAngle = finger.read();
if (currentAngle > fingerMinAngle) {
finger.write(currentAngle - 2); // Decrease angle by 2 degrees
}
// Decrease thumb servo angle
currentAngle = thumb.read();
if (currentAngle > thumbMinAngle) {
thumb.write(currentAngle - 3); // Decrease angle by 2 degrees
}
}
else {
delay (2000);
thumb.write(thumbMaxAngle);
finger.write(fingerMaxAngle);
}
// Print sensor values (for debugging purposes)
Serial.print("Proximity: ");
Serial.print(proximityValue);
Serial.print(" Pressure: ");
Serial.println(pressureValue);
delay(100); // Delay for stability
}
Braccio Arm Code w/ pressure code (supposed to turn the gripper servo motors): Does NOT work
#include <Wire.h>
#include <Braccio.h>
#include <Servo.h>
#include "SparkFun_LPS25HB_Arduino_Library.h"
#include "SparkFun_VCNL4040_Arduino_Library.h"
LPS25HB pressureSensor;
VCNL4040 proximitySensor;
const int pressureThreshold = 415;
const int proximityThreshold = 13000;
Servo base;
Servo shoulder;
Servo elbow;
Servo wrist_rot;
Servo wrist_ver;
Servo gripper;
Servo thumb;
Servo finger;
const int thumbMaxAngle = 80;
const int thumbMinAngle = 0;
const int fingerMaxAngle = 180;
const int fingerMinAngle = 110;
void setup() {
// Attach servo motors to pins
finger.attach(7);
thumb.attach(4);
Braccio.begin();
// Set initial positions of servo motors
finger.write(fingerMaxAngle); // Set to 90 degrees
thumb.write(thumbMaxAngle); // Set to 90 degrees
pressureSensor.begin();
proximitySensor.begin();
// Initialize serial communication
Serial.begin(9600);
Wire.begin();
Wire.setClock(400000);
//Initialization functions and set up the initial position for Braccio
//All the servo motors will be positioned in the "safety" position:
//Base (M1):90 degrees
//Shoulder (M2): 45 degrees
//Elbow (M3): 180 degrees
//Wrist vertical (M4): 180 degrees
//Wrist rotation (M5): 90 degrees
//gripper (M6): 10 degrees
}
void loop() {
//BASE MOVES
Braccio.ServoMovement(20, 0, 95, 105, 90, 90, 10);
//Wait 1 second
delay(1000);
Braccio.ServoMovement(20, 180, 95, 105, 90, 90, 10);
delay(1000);
// Read proximity sensor value
int proximityValue = proximitySensor.getProximity();
// Read pressure sensor value
int pressureValue = pressureSensor.getPressure_hPa();
// Check if both thresholds are met
if (proximityValue < proximityThreshold && pressureValue < pressureThreshold) {
// Decrease servo angles
int currentAngle;
// Decrease finger servo angle
currentAngle = finger.read();
if (currentAngle > fingerMinAngle) {
finger.write(currentAngle - 2); // Decrease angle by 2 degrees
}
// Decrease thumb servo angle
currentAngle = thumb.read();
if (currentAngle > thumbMinAngle) {
thumb.write(currentAngle - 3); // Decrease angle by 3 degrees
}
delay(1500); // Delay after servo movement
} else {
// Reset servos to max angles
delay(2000);
//BASE MOVES
Braccio.ServoMovement(20, 0, 95, 105, 90, 90, 10);
//Wait 1 second
delay(1000);
Braccio.ServoMovement(20, 180, 95, 105, 90, 90, 10);
delay(1000);
thumb.write(thumbMaxAngle);
finger.write(fingerMaxAngle);
delay(1500); // Delay after servo movement
}
// Print sensor values (for debugging purposes)
Serial.print("Proximity: ");
Serial.print(proximityValue);
Serial.print(" Pressure: ");
Serial.println(pressureValue);
delay(100); // Delay for stability
}
Braccio Simple Joint Movements w/ gripper servo motors (no pressure sensor): Works
#include <Braccio.h>
#include <Servo.h>
Servo base;
Servo shoulder;
Servo elbow;
Servo wrist_rot;
Servo wrist_ver;
Servo gripper;
Servo finger;
Servo thumb;
int fingerOpen = 180;
int fingerclosed = 110;
int thumbopen = 80;
int thumbclosed = 30;
void setup()
{
//starts stopped
Serial.begin(9600);
pinMode(2, INPUT);
finger.write(fingerOpen);
finger.attach(7);
thumb.attach(4);
Braccio.begin();
}
void loop()
{
//code you always run here; you can leave this section blank if you want the entire program to stop and start, or add code here if you want it to always run
//check button press here and if it is pressed then toggle run variable between 0 and 255; REQUIRED!
finger.write(fingerOpen);
thumb.write(thumbopen);
if(digitalRead(2) == HIGH) //funcitons based off of button pulling input pin LOW
{
finger.write(fingerOpen);
thumb.write(thumbopen);
delay(100);
finger.write(fingerclosed);
thumb.write(thumbclosed);
delay(2500);
finger.write(fingerOpen);
thumb.write(thumbopen);
delay (1000);
// the arm is aligned upwards and the gripper is closed
//(step delay, M1, M2, M3, M4, M5, M6);
//BASE MOVES
Braccio.ServoMovement(20, 0, 95, 105, 90, 90, 10);
//Wait 1 second
delay(1000);
Braccio.ServoMovement(20, 180, 95, 105, 90, 90, 10);
delay(1000);
//SHOULDER MOVES
//Wait 1 second
Braccio.ServoMovement(20, 180, 95,105,90,90,10);
delay(1000);
Braccio.ServoMovement(20, 180, 120, 105, 90, 90,10);
delay(1000);
//ELBOW MOVES
Braccio.ServoMovement(20, 180, 95, 105, 90,90, 10);
delay(1000);
Braccio.ServoMovement(20, 180, 95, 150, 90, 90, 10);
delay(1000);
//WRIST MOVES
Braccio.ServoMovement(20, 180, 95, 105, 150, 90, 10);
delay(1000);
Braccio.ServoMovement(20, 180, 95, 105, 90, 90, 10);
delay(1000);
//WRIST PITCH MOVES
Braccio.ServoMovement(20, 180, 95, 105, 90, 0, 10);
delay(1000);
Braccio.ServoMovement(20, 180, 95, 105, 90, 90, 10);
delay(1000);
//RETURN TO START
Braccio.ServoMovement(20, 180, 95, 105, 90,90, 10);
delay(1000);
}
}