19/05
This commit is contained in:
parent
05af884087
commit
d8697147ca
|
|
@ -0,0 +1,49 @@
|
|||
#include <Servo.h>
|
||||
|
||||
Servo myservo; // create servo object to control a servo
|
||||
|
||||
const unsigned long interval = 10; // interrupt interval in milliseconds
|
||||
volatile bool interruptFlag = false; // flag to indicate interrupt occurrence
|
||||
|
||||
int potpin = A0; // analog pin used to connect the potentiometer
|
||||
const int numReadings = 5; // number of readings to average
|
||||
int readings[numReadings]; // array to store the readings
|
||||
int index = 0; // index for storing the readings
|
||||
int total = 0; // sum of the readings
|
||||
|
||||
// Coefficients from the polynomial regression
|
||||
// Replace these with your actual coefficients
|
||||
const float a = 5.118e-09; // Coefficient for x^2
|
||||
const float b = 1.289e-05; // Coefficient for x
|
||||
const float c = 0.0118; // Constant term
|
||||
const float d = 4.729; // Coefficient for x
|
||||
const float e = 743.2; // Constant term
|
||||
|
||||
// Function to map analog values to weight using polynomial regression
|
||||
float map_analog_to_weight(int analog_value) {
|
||||
float weight = a*analog_value*analog_value*analog_value*analog_value - b*analog_value*analog_value*analog_value + c*analog_value*analog_value-d*analog_value+e;
|
||||
return weight;
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
myservo.attach(9); // attaches the servo on pin 9 to the servo object
|
||||
Serial.begin(9600); // begin serial baud rate
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
|
||||
int sensorValue = analogRead(potpin); // Get the average of the readings
|
||||
|
||||
// Output the filtered sensor value
|
||||
|
||||
float weight = map_analog_to_weight(sensorValue);
|
||||
Serial.println(weight);// map analog value to weight
|
||||
int mappedValue = map(weight, 0, 500, 180, 0); // scale weight to angle (value between 0 and 180)
|
||||
myservo.write(mappedValue);
|
||||
delay(10);// sets the servo position according to the scaled value
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
weights = np.array([10, 20, 50, 100, 200, 500]) # weight in grams
|
||||
analog_values = np.array([920, 720, 360, 280, 180, 60]) # corresponding analog values
|
||||
|
||||
degree = 4 # degree of the polynomial you want to fit, adjust as needed
|
||||
coefficients = np.polyfit(analog_values, weights, degree)
|
||||
|
||||
polynomial = np.poly1d(coefficients)
|
||||
|
||||
# Plotting the original data points and the polynomial regression for visualization
|
||||
x = np.linspace(60, 920, 400)
|
||||
y = polynomial(x)
|
||||
plt.scatter(analog_values, weights, color='blue') # original data points
|
||||
plt.plot(x, y, color='red') # polynomial regression
|
||||
plt.show()
|
||||
print(polynomial)
|
||||
Loading…
Reference in New Issue