ka und alte code wieder zurücl
This commit is contained in:
20
INF/beispiele/01.4_Battery_level/01.4_Battery_level.ino
Normal file
20
INF/beispiele/01.4_Battery_level/01.4_Battery_level.ino
Normal file
@@ -0,0 +1,20 @@
|
||||
/**********************************************************************
|
||||
Product : Freenove 4WD Car for ESP32
|
||||
Description : Read battery voltage.
|
||||
Auther : www.freenove.com
|
||||
Modification: 2020/12/18
|
||||
**********************************************************************/
|
||||
#include "Freenove_4WD_Car_For_ESP32.h"
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200); //Set the Serial Baud rate
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.print("Battery ADC : ");
|
||||
Serial.println(Get_Battery_Voltage_ADC());//Gets the battery ADC value
|
||||
Serial.print("Battery Voltage : ");
|
||||
Serial.print(Get_Battery_Voltage()); //Get the battery voltage value
|
||||
Serial.println("V");
|
||||
delay(300);
|
||||
}
|
||||
221
INF/beispiele/01.4_Battery_level/Freenove_4WD_Car_For_ESP32.cpp
Normal file
221
INF/beispiele/01.4_Battery_level/Freenove_4WD_Car_For_ESP32.cpp
Normal file
@@ -0,0 +1,221 @@
|
||||
#include <Arduino.h>
|
||||
#include "Freenove_4WD_Car_For_ESP32.h"
|
||||
|
||||
/////////////////////Servo drive area///////////////////////////////////
|
||||
int servo_1_offset = 0; //Define the offset variable for servo 1
|
||||
int servo_2_offset = 0; //Define the offset variable for servo 2
|
||||
PCA9685 PCA9685_SERVO;//Instantiate a PCA9685 object
|
||||
|
||||
//servo initialization
|
||||
void Servo_Setup(void)
|
||||
{
|
||||
Wire.begin(PCA9685_SERVO_SDA, PCA9685_SERVO_SCL);
|
||||
PCA9685_SERVO.setupSingleDevice(Wire, PCA9685_ADDRESS);
|
||||
PCA9685_SERVO.setToFrequency(SERVO_FREQUENCY);
|
||||
}
|
||||
|
||||
//Set the rotation parameters of servo 1, and the parameters are 0-180 degrees
|
||||
void Servo_1_Angle(float angle)
|
||||
{
|
||||
if (PCA9685_SERVO.getFrequency() != SERVO_FREQUENCY)
|
||||
Servo_Setup();
|
||||
angle = constrain(angle, 0, 180);
|
||||
angle = map(angle, 0, 180, 500, 2500);
|
||||
PCA9685_SERVO.setChannelServoPulseDuration(PCA9685_CHANNEL_0, int(angle) + servo_1_offset);
|
||||
}
|
||||
|
||||
//Set the rotation parameters of servo 2, and the parameters are 0-180 degrees
|
||||
void Servo_2_Angle(float angle)
|
||||
{
|
||||
if (PCA9685_SERVO.getFrequency() != SERVO_FREQUENCY)
|
||||
Servo_Setup();
|
||||
angle = constrain(angle, 0, 180);
|
||||
angle = map(angle, 0, 180, 500, 2500);
|
||||
PCA9685_SERVO.setChannelServoPulseDuration(PCA9685_CHANNEL_1, int(angle) + servo_2_offset);
|
||||
}
|
||||
|
||||
//Set servo 1 offset
|
||||
void Set_Servo_1_Offset(int offset)
|
||||
{
|
||||
servo_1_offset = offset;
|
||||
}
|
||||
|
||||
//Set servo 2 offset
|
||||
void Set_Servo_2_Offset(int offset)
|
||||
{
|
||||
servo_2_offset = offset;
|
||||
}
|
||||
|
||||
//Servo sweep function
|
||||
//id = 1: angle_start: 0 - 180; angle_end: 0 - 180.
|
||||
//id = 2: angle_start: 90 - 150; angle_end: 90 - 150
|
||||
void Servo_Sweep(int servo_id, int angle_start, int angle_end)
|
||||
{
|
||||
if (servo_id == 1)
|
||||
{
|
||||
angle_start = constrain(angle_start, 0, 180);
|
||||
angle_end = constrain(angle_end, 0, 180);
|
||||
}
|
||||
else if (servo_id == 2)
|
||||
{
|
||||
angle_start = constrain(angle_start, 90, 150);
|
||||
angle_end = constrain(angle_end, 90, 150);
|
||||
}
|
||||
|
||||
if (angle_start > angle_end)
|
||||
{
|
||||
for (int i = angle_start; i >= angle_end; i--)
|
||||
{
|
||||
if (servo_id == 1)
|
||||
Servo_1_Angle(i);
|
||||
else if (servo_id == 2)
|
||||
Servo_2_Angle(i);
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
if (angle_start < angle_end)
|
||||
{
|
||||
for (int i = angle_start; i <= angle_end; i++)
|
||||
{
|
||||
if (servo_id == 1)
|
||||
Servo_1_Angle(i);
|
||||
else if (servo_id == 2)
|
||||
Servo_2_Angle(i);
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////Motor drive area///////////////////////////////////
|
||||
PCA9685 PCA9685_MOTOR;//Instantiate a PCA9685 object to control the motor
|
||||
|
||||
//The initialization function for PCA9685
|
||||
void Motor_Setup(void)
|
||||
{
|
||||
Wire.begin(PCA9685_MOTOR_SDA, PCA9685_MOTOR_SCL);
|
||||
PCA9685_MOTOR.setupSingleDevice(Wire, PCA9685_ADDRESS);
|
||||
PCA9685_Close_Com_Address();
|
||||
PCA9685_MOTOR.setToFrequency(MOTOR_FREQUENCY);
|
||||
}
|
||||
|
||||
//A function to control the car motor
|
||||
void Motor_Move(int m1_speed, int m2_speed, int m3_speed, int m4_speed)
|
||||
{
|
||||
if (PCA9685_MOTOR.getFrequency() != MOTOR_FREQUENCY)
|
||||
Motor_Setup();
|
||||
|
||||
m1_speed = constrain(m1_speed, MOTOR_SPEED_MIN, MOTOR_SPEED_MAX);
|
||||
m2_speed = constrain(m2_speed, MOTOR_SPEED_MIN, MOTOR_SPEED_MAX);
|
||||
m3_speed = constrain(m3_speed, MOTOR_SPEED_MIN, MOTOR_SPEED_MAX);
|
||||
m4_speed = constrain(m4_speed, MOTOR_SPEED_MIN, MOTOR_SPEED_MAX);
|
||||
|
||||
if (m1_speed >= 0)
|
||||
{
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M1_IN1, m1_speed);
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M1_IN2, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
m1_speed = -m1_speed;
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M1_IN1, 0);
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M1_IN2, m1_speed);
|
||||
}
|
||||
if (m2_speed >= 0)
|
||||
{
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M2_IN1, m2_speed);
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M2_IN2, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
m2_speed = -m2_speed;
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M2_IN1, 0);
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M2_IN2, m2_speed);
|
||||
}
|
||||
if (m3_speed >= 0)
|
||||
{
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M3_IN1, m3_speed);
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M3_IN2, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
m3_speed = -m3_speed;
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M3_IN1, 0);
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M3_IN2, m3_speed);
|
||||
}
|
||||
if (m4_speed >= 0)
|
||||
{
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M4_IN1, m4_speed);
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M4_IN2, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
m4_speed = -m4_speed;
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M4_IN1, 0);
|
||||
PCA9685_MOTOR.setChannelPulseWidth(PIN_MOTOR_M4_IN2, m4_speed);
|
||||
}
|
||||
}
|
||||
|
||||
void PCA9685_Close_Com_Address(void)
|
||||
{
|
||||
Wire.beginTransmission(PCA9685_ADDRESS);
|
||||
Wire.write(0x00);
|
||||
Wire.write(0x00);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
//////////////////////Buzzer drive area///////////////////////////////////
|
||||
//Buzzer initialization
|
||||
void Buzzer_Setup(void)
|
||||
{
|
||||
pinMode(PIN_BUZZER, OUTPUT);
|
||||
ledcSetup(BUZZER_CHN, BUZZER_FREQUENCY, 10);
|
||||
ledcAttachPin(PIN_BUZZER, BUZZER_CHN);
|
||||
ledcWriteTone(BUZZER_CHN, 0);
|
||||
delay(10);
|
||||
}
|
||||
|
||||
//Buzzer alarm function
|
||||
void Buzzer_Alert(int beat, int rebeat)
|
||||
{
|
||||
beat = constrain(beat, 1, 9);
|
||||
rebeat = constrain(rebeat, 1, 255);
|
||||
for (int j = 0; j < rebeat; j++)
|
||||
{
|
||||
for (int i = 0; i < beat; i++)
|
||||
{
|
||||
ledcWriteTone(BUZZER_CHN, BUZZER_FREQUENCY);
|
||||
delay(100);
|
||||
ledcWriteTone(BUZZER_CHN, 0);
|
||||
delay(100);
|
||||
}
|
||||
delay(500);
|
||||
}
|
||||
ledcWriteTone(BUZZER_CHN, 0);
|
||||
}
|
||||
|
||||
////////////////////Battery drive area/////////////////////////////////////
|
||||
float batteryVoltage = 0; //Battery voltage variable
|
||||
float batteryCoefficient = 4; //Set the proportional coefficient
|
||||
|
||||
//Gets the battery ADC value
|
||||
int Get_Battery_Voltage_ADC(void)
|
||||
{
|
||||
pinMode(PIN_BATTERY, INPUT);
|
||||
int batteryADC = 0;
|
||||
for (int i = 0; i < 5; i++)
|
||||
batteryADC += analogRead(PIN_BATTERY);
|
||||
return batteryADC / 5;
|
||||
}
|
||||
|
||||
//Get the battery voltage value
|
||||
float Get_Battery_Voltage(void)
|
||||
{
|
||||
int batteryADC = Get_Battery_Voltage_ADC();
|
||||
batteryVoltage = (batteryADC / 4096.0 * 3.9 ) * batteryCoefficient;
|
||||
return batteryVoltage;
|
||||
}
|
||||
|
||||
void Set_Battery_Coefficient(float coefficient)
|
||||
{
|
||||
batteryCoefficient = coefficient;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
#ifndef _FREENOVE_4WD_CAR_H
|
||||
#define _FREENOVE_4WD_CAR_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <PCA9685.h>
|
||||
|
||||
void PCA9685_Close_Com_Address(void);//Close the PCA9685 public address
|
||||
|
||||
/////////////////////Servo drive area///////////////////////////////////
|
||||
#define PCA9685_SERVO_SDA 13 //Define SDA pins
|
||||
#define PCA9685_SERVO_SCL 14 //Define SCL pins
|
||||
#ifndef PCA9685_ADDRESS
|
||||
#define PCA9685_ADDRESS 0x5F
|
||||
#endif
|
||||
#define SERVO_FREQUENCY 50 //Define the operating frequency of servo
|
||||
#define PCA9685_CHANNEL_0 0 //Define PCA9685 channel to control servo 1
|
||||
#define PCA9685_CHANNEL_1 1 //Define the PCA9685 channel to control servo 2
|
||||
#define SERVO_MIDDLE_POINT 1500 //Define the middle position of the servo
|
||||
extern int servo_1_offset; //Define the offset variable for servo 1
|
||||
extern int servo_2_offset; //Define the offset variable for servo 2
|
||||
extern PCA9685 PCA9685_SERVO; //Instantiate a PCA9685 object
|
||||
|
||||
void Servo_Setup(void); //servo initialization
|
||||
void Servo_1_Angle(float angle); //Set the rotation parameters of servo 1, and the parameters are 0-180 degrees
|
||||
void Servo_2_Angle(float angle); //Set the rotation parameters of servo 2, and the parameters are 0-180 degrees
|
||||
void Set_Servo_1_Offset(int offset); //Set servo 1 offset
|
||||
void Set_Servo_2_Offset(int offset); //Set servo 2 offset
|
||||
//Servo sweep function; id = 1: angle_start: 0 - 180; angle_end: 0 - 180; id = 2: angle_start: 90 - 150; angle_end: 90 - 150
|
||||
void Servo_Sweep(int servo_id, int angle_start, int angle_end);
|
||||
|
||||
/////////////////////Motor drive area///////////////////////////////////
|
||||
//Motor pin definition
|
||||
#define PCA9685_MOTOR_SDA 13 //Define the SDA pin number
|
||||
#define PCA9685_MOTOR_SCL 14 //Define the SCL pin number
|
||||
#ifndef PCA9685_ADDRESS
|
||||
#define PCA9685_ADDRESS 0x5F
|
||||
#endif
|
||||
#define MOTOR_FREQUENCY 1000 //PWM Frequency
|
||||
#define MOTOR_SPEED_MIN -4095 //Define a minimum speed limit for wheels
|
||||
#define MOTOR_SPEED_MAX 4095 //Define a maximum speed limit for wheels
|
||||
#define PIN_MOTOR_M1_IN1 15 //Define the positive pole of M1
|
||||
#define PIN_MOTOR_M1_IN2 14 //Define the negative pole of M1
|
||||
#define PIN_MOTOR_M2_IN1 9 //Define the positive pole of M2
|
||||
#define PIN_MOTOR_M2_IN2 8 //Define the negative pole of M2
|
||||
#define PIN_MOTOR_M3_IN1 12 //Define the positive pole of M3
|
||||
#define PIN_MOTOR_M3_IN2 13 //Define the negative pole of M3
|
||||
#define PIN_MOTOR_M4_IN1 10 //Define the positive pole of M4
|
||||
#define PIN_MOTOR_M4_IN2 11 //Define the negative pole of M4
|
||||
|
||||
extern PCA9685 PCA9685_MOTOR; //Instantiate a PCA9685 object to control the motor
|
||||
void Motor_Setup(void); //The initialization function for PCA9685
|
||||
void Motor_Move(int m1_speed, int m2_speed, int m3_speed, int m4_speed);//A function to control the car motor
|
||||
|
||||
//////////////////////Buzzer drive area///////////////////////////////////
|
||||
//Buzzer pin definition
|
||||
#define PIN_BUZZER 2 //Define the pins for the ESP32 control buzzer
|
||||
#define BUZZER_CHN 0 //Define the PWM channel for ESP32
|
||||
#define BUZZER_FREQUENCY 2000 //Define the resonant frequency of the buzzer
|
||||
|
||||
void Buzzer_Setup(void); //Buzzer initialization
|
||||
void Buzzer_Alert(int beat, int rebeat);//Buzzer alarm function
|
||||
|
||||
////////////////////Battery drive area/////////////////////////////////////
|
||||
#define PIN_BATTERY 32 //Set the battery detection voltage pin
|
||||
#define LOW_VOLTAGE_VALUE 2100 //Set the minimum battery voltage
|
||||
|
||||
extern float batteryCoefficient; //Set the proportional coefficient
|
||||
|
||||
int Get_Battery_Voltage_ADC(void); //Gets the battery ADC value
|
||||
float Get_Battery_Voltage(void); //Get the battery voltage value
|
||||
void Set_Battery_Coefficient(float coefficient);//Set the partial pressure coefficient
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user