ka und alte code wieder zurücl

This commit is contained in:
danielvici123
2024-12-03 09:21:41 +01:00
parent c810f9541d
commit 0ec0460b49
22 changed files with 2005 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
/**********************************************************************
Product : Freenove 4WD Car for ESP32
Description : Ultrasonic Car.
Auther : www.freenove.com
Modification: 2020/12/18
**********************************************************************/
#include <Arduino.h>
#include "Freenove_4WD_Car_For_ESP32.h"
#define OBSTACLE_DISTANCE 40
#define OBSTACLE_DISTANCE_LOW 20
int distance[4]; //Storage of ultrasonic data
void setup() {
Ultrasonic_Setup();//Initialize the ultrasonic module
PCA9685_Setup(); //PCA9685 chip initializes
}
void loop()
{
get_distance(1);//Get multiple Angle distance data
if (distance[1] > OBSTACLE_DISTANCE)//There is no obstacle within 40cm of the front of the car
{
if (distance[0] >= distance[2] && distance[2] < OBSTACLE_DISTANCE_LOW) //There is an obstacle on the right
Motor_Move(-1000, -1000, 2000, 2000);
else if (distance[0] < distance[2] && distance[0] < OBSTACLE_DISTANCE_LOW)//There is an obstacle on the left
Motor_Move(2000, 2000, -1000, -1000);
else //There are no obstacles around
Motor_Move(1000, 1000, 1000, 1000);
delay(20);
}
else if (distance[1] < OBSTACLE_DISTANCE) //There is an obstacle ahead
{
Motor_Move(0, 0, 0, 0); //Stop the car to judge the situation
get_distance(2);
if (distance[1] < OBSTACLE_DISTANCE_LOW) //The car got too close to the obstacle
Motor_Move(-1000, -1000, -1000, -1000);
else if (distance[0] < distance[2]) //There is also an obstacle on the left
Motor_Move(2000, 2000, -2000, -2000);
else if (distance[0] > distance[2]) //There is also an obstacle on the right
Motor_Move(-2000, -2000, 2000, 2000);
delay(200);
}
}
//Get distance values for different angles
void get_distance(int car_mode)
{
int add_angle=30;
if(car_mode==1)
add_angle=0;
else
add_angle=30;
Servo_2_Angle(90);
Servo_1_Angle(120+add_angle);
delay(100);
distance[0] = Get_Sonar();//Get the data on the left
Servo_1_Angle(90);
delay(100);
distance[1] = Get_Sonar();//Get the data in the middle
Servo_1_Angle(60-add_angle);
delay(100);
distance[2] = Get_Sonar();//Get the data on the right
Servo_1_Angle(90);
delay(100);
distance[1] = Get_Sonar();//Get the data in the middle
}

View File

@@ -0,0 +1,266 @@
#include <Arduino.h>
#include "Freenove_4WD_Car_For_ESP32.h"
/////////////////////PCA9685 drive area///////////////////////////////////
#define PCA9685_SDA 13 //Define SDA pins
#define PCA9685_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
#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
PCA9685 pca9685;//Instantiate a PCA9685 object
//PCA9685 initialization
void PCA9685_Setup(void)
{
Wire.begin(PCA9685_SDA, PCA9685_SCL);
Wire.beginTransmission(PCA9685_ADDRESS);
Wire.write(0x00);
Wire.write(0x00);
Wire.endTransmission();
pca9685.setupSingleDevice(Wire, PCA9685_ADDRESS);
pca9685.setToFrequency(SERVO_FREQUENCY);
}
//Set the rotation parameters of servo 1, and the parameters are 0-180 degrees
void Servo_1_Angle(float angle)
{
angle = constrain(angle, 0, 180);
angle = map(angle, 0, 180, 102, 512);
pca9685.setChannelPulseWidth(PCA9685_CHANNEL_0, int(angle));
}
//Set the rotation parameters of servo 2, and the parameters are 0-180 degrees
void Servo_2_Angle(float angle)
{
angle = constrain(angle, 0, 180);
angle = map(angle, 0, 180, 102, 512);
pca9685.setChannelPulseWidth(PCA9685_CHANNEL_1, int(angle));
}
//Servo sweep function
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);
}
}
}
//A function to control the car motor
void Motor_Move(int m1_speed, int m2_speed, int m3_speed, int m4_speed)
{
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.setChannelPulseWidth(PIN_MOTOR_M1_IN1, m1_speed);
pca9685.setChannelPulseWidth(PIN_MOTOR_M1_IN2, 0);
}
else
{
m1_speed = -m1_speed;
pca9685.setChannelPulseWidth(PIN_MOTOR_M1_IN1, 0);
pca9685.setChannelPulseWidth(PIN_MOTOR_M1_IN2, m1_speed);
}
if (m2_speed >= 0)
{
pca9685.setChannelPulseWidth(PIN_MOTOR_M2_IN1, m2_speed);
pca9685.setChannelPulseWidth(PIN_MOTOR_M2_IN2, 0);
}
else
{
m2_speed = -m2_speed;
pca9685.setChannelPulseWidth(PIN_MOTOR_M2_IN1, 0);
pca9685.setChannelPulseWidth(PIN_MOTOR_M2_IN2, m2_speed);
}
if (m3_speed >= 0)
{
pca9685.setChannelPulseWidth(PIN_MOTOR_M3_IN1, m3_speed);
pca9685.setChannelPulseWidth(PIN_MOTOR_M3_IN2, 0);
}
else
{
m3_speed = -m3_speed;
pca9685.setChannelPulseWidth(PIN_MOTOR_M3_IN1, 0);
pca9685.setChannelPulseWidth(PIN_MOTOR_M3_IN2, m3_speed);
}
if (m4_speed >= 0)
{
pca9685.setChannelPulseWidth(PIN_MOTOR_M4_IN1, m4_speed);
pca9685.setChannelPulseWidth(PIN_MOTOR_M4_IN2, 0);
}
else
{
m4_speed = -m4_speed;
pca9685.setChannelPulseWidth(PIN_MOTOR_M4_IN1, 0);
pca9685.setChannelPulseWidth(PIN_MOTOR_M4_IN2, m4_speed);
}
}
//////////////////////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
//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/////////////////////////////////////
#define PIN_BATTERY 32 //Set the battery detection voltage pin
#define LOW_VOLTAGE_VALUE 2100 //Set the minimum battery voltage
float batteryVoltage = 0; //Battery voltage variable
float batteryCoefficient = 3.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();
if (batteryADC <= LOW_VOLTAGE_VALUE)
{
return 0;
}
batteryVoltage = (batteryADC / 4096.0 * 3.3 ) * batteryCoefficient;
return batteryVoltage;
}
void Set_Battery_Coefficient(float coefficient)
{
batteryCoefficient = coefficient;
}
/////////////////////Photosensitive drive area//////////////////////////
#define PHOTOSENSITIVE_PIN 33
//Photosensitive initialization
void Photosensitive_Setup(void)
{
pinMode(PHOTOSENSITIVE_PIN, INPUT);
}
//Gets the photosensitive resistance value
int Get_Photosensitive(void)
{
int photosensitiveADC = analogRead(PHOTOSENSITIVE_PIN);
return photosensitiveADC;
}
/////////////////////Ultrasonic drive area//////////////////////////////
#define PIN_SONIC_TRIG 12 //define Trig pin
#define PIN_SONIC_ECHO 15 //define Echo pin
#define MAX_DISTANCE 300 //cm
#define SONIC_TIMEOUT (MAX_DISTANCE*60) // calculate timeout
#define SOUND_VELOCITY 340 //soundVelocity: 340m/s
//Ultrasonic initialization
void Ultrasonic_Setup(void)
{
pinMode(PIN_SONIC_TRIG, OUTPUT);// set trigPin to output mode
pinMode(PIN_SONIC_ECHO, INPUT); // set echoPin to input mode
}
//Obtain ultrasonic distance data
float Get_Sonar(void)
{
unsigned long pingTime;
float distance;
digitalWrite(PIN_SONIC_TRIG, HIGH); // make trigPin output high level lasting for 10μs to triger HC_SR04,
delayMicroseconds(10);
digitalWrite(PIN_SONIC_TRIG, LOW);
pingTime = pulseIn(PIN_SONIC_ECHO, HIGH, SONIC_TIMEOUT); // Wait HC-SR04 returning to the high level and measure out this waitting time
if (pingTime != 0)
distance = (float)pingTime * SOUND_VELOCITY / 2 / 10000; // calculate the distance according to the time
else
distance = MAX_DISTANCE;
return distance; // return the distance value
}

View File

@@ -0,0 +1,33 @@
#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
/////////////////////PCA9685 drive area//////////////////////////////////////
void PCA9685_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 Servo_Sweep(int servo_id, int angle_start, int angle_end);//Servo sweep function;
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////////////////////////////////////
void Buzzer_Setup(void); //Buzzer initialization
void Buzzer_Alert(int beat, int rebeat);//Buzzer alarm function
////////////////////Battery drive area/////////////////////////////////////
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
////////////////////Photosensitive drive area//////////////////////////////
void Photosensitive_Setup(void); //Photosensitive initialization
int Get_Photosensitive(void); //Gets the photosensitive resistance value
/////////////////////Ultrasonic drive area/////////////////////////////////
void Ultrasonic_Setup(void);//Ultrasonic initialization
float Get_Sonar(void);//Obtain ultrasonic distance data
#endif