PID Controller to move the hand robot in one way

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.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

  milliNew = millis()

void loop() {
  //Serial.println("Readings: ");
  weight = scale.get_units(), 10;
  ounces = weight * 0.035274;
  //  Serial.print(weight);
  //  Serial.print(" grams");
  //  Serial.print("  ");

  milliOld = milliNew;
  milliNew = millis();
  dt = milliNew - milliOld;

  weightErrorOld = weightError;
  weightError = weightErrorTargert

  if (weight < -200 )
    pos -= 1;
    if (pos <= 0)pos = 0;
  if (weight  >= 200) {
    pos += 1;
    if (pos >= 90)pos = 90;

Can you please identify the source of the feedback that is required for the PID to function?

PID is used to position something by controlling the speed of the movement to minimize overshoot.

while a position measurement is an input to PID, force requires a sensor that measures pressure

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 .

the load cell. the weight of the load cell i have been set 200. so when it move, the pid controller will find back the 200 force of the load cell

yes i using the force sensor. im using the load cell.

i did google but i did not how to match up with my code. Because i want the pid control will follow based on load cell which is 200

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.

some explanation of your application would help

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?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.