I made a wattmeter for monitoring of consuption of a brushless motor and everything works fine.
I control the ESC via manual PWM signal
Now i would like to get a value of PWM in funcion of analog read of watt sensor
Example:
Automatic acceleration writing PWM signal on pin 11 (1,2,3,4...10....50...2000...255), at the same moment reading the value of the analog pin for wattmeter and change the value of PWM tryin to make costant the value po analog pin for wattmeter that i chiose
Something like this
Value o range i chiose : 30-35 watt
Read anog pin 10 watt, and accelerate till the value will be between 30 and 35 and decelerate in case will go over
int ESCpin = 11; // ESC signal wire connected to pin 11 on arduino
int val = 0; //variable to store PWM value
void setup()
{
Serial.begin(9600);
}
void loop()
{
for(val = 178; val <= 255; val += 1)
{
analogWrite(ESCpin, val);
delay(8000);
Serial.println (val);
}
}
and this code to find the value of watt
void setup() {
Serial.begin(9600);
}
void loop() {
float average = 0;
for(int i = 0; i < 1000; i++) {
average = average + (0.0049 * analogRead(A4)/0.185 -13.5447) / 1000;
delay(1);
}
Serial.print("mAmp...");
Serial.println(average*1000);
Serial.print("Watt...");
Serial.println(average*37);
}
From what you are describing, it sounds like you are looking to make a PID controller.
Sudo code that could get you a first pass on what you are wanting:
Throttle = start point
loop start
Check Current Power
if Current Power < Target Power: increase Throttle
if Current power > Target Power: decrease Throttle
loop end
Your power sample code snippet is taking an average over a full second, is that needed for your application?
If so, how rapidly do you need your Throttle to adjust to variations in power usage?
From what you are describing, it sounds like you are looking to make a PID controller.
Sudo code that could get you a first pass on what you are wanting:
Throttle = start point
loop start
Check Current Power
if Current Power < Target Power: increase Throttle
if Current power > Target Power: decrease Throttle
loop end
Your power sample code snippet is taking an average over a full second, is that needed for your application?
If so, how rapidly do you need your Throttle to adjust to variations in power usage?
Yes it is exactly what i need!!!
how can i put this code on my sketch?
for sure i would like to make this change as fast as it possible, how fast can it be?
This is putting your code segments together to fit with my sudo code. It compiles, but I don't have your hardware setup to test its functionality. As written it will only update the Throttle at less then 1 time a second because your code to gather the power average takes 1 second in delay time alone. If you don't need a 1 second average or can use a rolling average (all depends on your application), you could get a more frequently update cycle.
With the current update cycle it will take more then 4.25 minutes to go from no power to full power or full power to no power.
int ESCpin = 11; // ESC signal wire connected to pin 11 on arduino
int Throttle = 0; // variable to store PWM value
float GoalWatt = 10.0; // Set point for Goal wattage
void setup() {
Serial.begin(9600);
}
void loop() {
float average = 0;
for(int i = 0; i < 1000; i++) {
average = average + (0.0049 * analogRead(A4)/0.185 -13.5447) / 1000;
delay(1);
}
Serial.print("mAmp...");
Serial.println(average*1000);
Serial.print("Watt...");
Serial.println(average*37);
if(average > GoalWatt) { // Higher then goal
if(Throttle > 0) Throttle--; // decrease Throttle if greater then min
} else if (average < GoalWatt) { // Lower then goal
if(Throttle < 255) Throttle++; // increase Throttle if less then max
}
analogWrite(ESCpin, Throttle); // Update PIN output
}