Hi, i have problem with my arduino code . I currently working on PID control to move the hand robot . How to write the code for PID control to detect the desired force ? I want the the hand robot follow the 200 of desire force .This is some code that i have being try but it did not work.
#include "HX711.h"
#include <Servo.h>
#define DOUT 3
#define CLK 2
//#define kp 0
//#define ki 0
//#define kd 0
//double priError = 0;
//double toError = 0;
Servo myservo;
float calibration_factor = 242;
float weight = 763;
float ounces;
float kp = 0.5;
float ki = 0.001;
float kd = 70;
int milliOld;
int milliNew;
int dt;
float weightError = 0;
float weightErrorOld;
float weightErrorChange;
float weightErrorSlope = 0;
float weightErrorArea = 0;
int pos = 0;
HX711 scale;
void setup() {
Serial.begin(9600);
//Serial.println("Remove all weight from scale");
//Serial.println("After readings begin, place known weight on scale");
scale.begin(DOUT, CLK);
scale.set_scale(calibration_factor); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
myservo.attach(9);
myservo.write(pos);
milliNew = millis()
}
void loop() {
//Serial.println("Readings: ");
weight = scale.get_units(), 10;
ounces = weight * 0.035274;
// Serial.print(weight);
// Serial.print(" grams");
// Serial.print(" ");
Serial.println((String)pos);
milliOld = milliNew;
milliNew = millis();
dt = milliNew - milliOld;
weightErrorOld = weightError;
weightError = weightErrorTargert
if (weight < -200 )
{
pos -= 1;
if (pos <= 0)pos = 0;
myservo.write(pos);
}
if (weight >= 200) {
pos += 1;
if (pos >= 90)pos = 90;
myservo.write(pos);
}
You cannot control force with a servo , servos move things to set positions and shouldn’t be stalled at any position to provide “ “force”.
You could use a motor with a worm gearbox pulling on a spring - that could provide a variable force attached to a fixed object . The PID control could drive the motor on response to your force transducer feedback
You code is basically an on/off controller - you need to google PID control and Arduino examples .
it's not obvious what you plan to do using a force sensor.
presumably you would position an arm up against some object and then apply power to the arm to increase the force against the object.
PID would not be used when applying the force. presumably the force would be increased in a controlled way where an increase in power is expected to result in an increase in the measured force by some limited amount.
it's not obvious if the force is expected to move the object nor what happens if the object does move at less than the target force. in unexpected cases, power should be removed.
im doing a hand robot where the hand will move when the servo motor move from 0 - 90 degree. The servo motor are controlled by load cell. when the load cell weight >200, the servo will move to 0- 90 degree. if the the weight <200 , the servo will turning back to from 90 - 0 degree. So i want to put the PID control based on the load cell weight . So if the hand move ,the servo will follow the movement based on the weight.
you've repeatedly said you want to use a load cell using PID to control the movement of a servo.
you've been told that PID is typically used to control position or speed based on a position or speed input to minimize overshoot.
PID doesn't appear to be applicable in your application
what happens while the input remains < 200?
does the servo start in contact with an object?
is the servo driven at full speed, does it "smack" into an object and then attempt to slow down as the force increases?
would the servo stop at 90 degs?