francoisboivin
1 mW
- Joined
- Oct 13, 2015
- Messages
- 12
Hi,
The battery I have for the moment doesn't have enough capacity for the controller/motor on my trike so I decided to build a current limiter with an arduino nano.
This is a beta version as I haven't tested it yet. I putting this here to see if anyone has an opinion to help me improve it before I have the PCB fabricated.
-I do not know if the gains (kp,ki and kd) are ok, experiment will tell
-I do not even know if the PID function will work, but that can be reprogram when I receive the PCB. If anyone have an idea about this, your opinion is welcome.
-I will implement a watt-hour meter some day
The next version will also have the solar energy management implemented, so I tought I publlished this one as the PCB is smaller (and cheaper). I will run on that for a few months at least until I take the time to setup my solar panel on my trike. Also, as I have to order a minimum quantity, I can redistribute the leftover if anyone is interrested.
Here's the work I have done so far:
The battery I have for the moment doesn't have enough capacity for the controller/motor on my trike so I decided to build a current limiter with an arduino nano.
This is a beta version as I haven't tested it yet. I putting this here to see if anyone has an opinion to help me improve it before I have the PCB fabricated.
-I do not know if the gains (kp,ki and kd) are ok, experiment will tell
-I do not even know if the PID function will work, but that can be reprogram when I receive the PCB. If anyone have an idea about this, your opinion is welcome.
-I will implement a watt-hour meter some day
The next version will also have the solar energy management implemented, so I tought I publlished this one as the PCB is smaller (and cheaper). I will run on that for a few months at least until I take the time to setup my solar panel on my trike. Also, as I have to order a minimum quantity, I can redistribute the leftover if anyone is interrested.
Here's the work I have done so far:


Code:
#include <PID_v1.h>
#include <SoftwareSerial.h>
/********************************************************
* A0 Tension de la batterie
* A1 Capteur de courant batterie
* A2 Entree signal du throttle
* D3 Sortie PWM vers le controleur du signal de vitesse
* D9 Tx Arduino
* D10 Rx Arduino
* D11 KEY Bluetooth
* D12 VCC Bluetooth
********************************************************/
//------------Specify the links and initial tuning parameters------------
#define Battery_Voltage 0 //0-51V to 0-5 V voltage divider
#define DC_Current_Sensor 1 //0-100A to 0-5V current sensor
#define Throttle_Input_Pin 3 // Throttle input A0
#define Throttle_Output_Pin 3 //PWM output to controller
#define VCC_Bluetooth 12 //Digital Pin to activate bluetooth
#define Key_Bluetooth 11 //Key for AT commands on bluetooth
#define Rx_Arduino 10 //Rx Pin to bluetooth
#define Tx_Arduino 9 //Tx Pin to bluetooth
//Variables
double Throttle_IN_Value;
double Current_Value;
double Throttle_Output_limits;
double Throttle_OUT_Value=0; //Intialize at zero
int Current_Max=24; //Enter the desired current limit (lower than the BMS)
double Battery_V=0; //Battery voltage
double Power_W=0; //Instaneous Power (watts)
boolean BlueT_Activator = false;
SoftwareSerial mySerial(Rx_Arduino, Tx_Arduino); //Initialize bluetooth communication port (Rx,Tx)
// ------------ PID setup -------------
//PID parameters
float kp=0.1;
float ki=0.25;
float kd=0;
double PID_Input, PID_Output, PID_Setpoint;
PID myPID(&PID_Input, &PID_Output,&PID_Setpoint,kp,ki,kd, DIRECT);
// ----------------------------------------
void setup()
{
//------------initialize serial communication at 9600 bits per second:------------
Serial.begin(9600);
mySerial.begin(9600);
//------------initialize the variables ------------
pinMode(Throttle_Output_Pin, OUTPUT);
pinMode(Throttle_Input_Pin, INPUT);
pinMode(DC_Current_Sensor, INPUT);
pinMode(Battery_Voltage, INPUT);
pinMode(VCC_Bluetooth, OUTPUT);
pinMode(Key_Bluetooth, OUTPUT);
pinMode(VCC_Bluetooth, LOW); //set the VCC to LOW to disable bluetooth by default
pinMode(Key_Bluetooth, LOW); //set the key to LOW to enable communication
//------------Intialize the PID------------
myPID.SetMode(AUTOMATIC); //initialize pid
myPID.SetOutputLimits(0,254); //set maximum output value to max throttle value
myPID.SetSampleTime(10); //compute pid every 10 ms
myPID.SetTunings(kp,ki,kd); //set PID constants
}
//***********************************************************
void loop(){
BlueTooth_F();
//------------Get all the data from sensors de la batterie------------
Battery_V = analogRead(Battery_Voltage);
Battery_V = (Battery_V * (5.0/1023.0) * (1000000.0 + 110000.0) / 110000.0); //Transform 0-5V to 0-50.4V (12S@4.2 = 50.4) R1=1M & R2=110k
Current_Value =analogRead(DC_Current_Sensor);
Current_Value = ((Current_Value / 1023.0 ) * 5000.0); //Transprocessingform it to mV
Current_Value = ((Current_Value - 2500.0 ) / 20.0); //Offset at 2.5V and 20mV/A with a bi-directionnal ACS758
Power_W = (Battery_V * Current_Value); //Calculate power
//------------Send them to serial------------
Serial.print("Tension:");
Serial.println(Battery_V);
Serial.print("Courant:");
Serial.println(Current_Value);
Serial.print("Puissance:");
Serial.println(Power_W);
//------------Compute the new throttle value------------
Throttle_IN_Value = analogRead(Throttle_Input_Pin);
PID_Input = Current_Value;
PID_Setpoint = Current_Max;
myPID.Compute();
Throttle_OUT_Value = constrain(PID_Output,0,254); // set throttle OUT value with the PID output
analogWrite(Throttle_Output_Pin, Throttle_OUT_Value); // write throttle output value to controller
}
//***********************************************************
//------------BLUETOOTH FUNCTION------------
void BlueTooth_F(){
if(BlueT_Activator==true && VCC_Bluetooth==LOW){ //Active the VCC when the bluetooth mode is activated
digitalWrite(VCC_Bluetooth, HIGH);
}
if(mySerial.available()) Serial.write(mySerial.read());
if(Serial.available()) mySerial.println(Serial.read());
}