Added moveControll in main.cpp
This commit is contained in:
+1
-1
@@ -23,5 +23,5 @@
|
|||||||
#define PWM_CHANNEL_M2 1
|
#define PWM_CHANNEL_M2 1
|
||||||
#define UPDATE_TIME 25
|
#define UPDATE_TIME 25
|
||||||
#define SPEED_STEPS 5 // A total of 20 levels ( 100 / SPEED_STEPS ) * UPDATE_TIME = 500ms
|
#define SPEED_STEPS 5 // A total of 20 levels ( 100 / SPEED_STEPS ) * UPDATE_TIME = 500ms
|
||||||
#define PWM_MIN 40
|
#define PWM_MIN 150
|
||||||
#define PWM_MAX 245 // Max 98% of 2^PWM_RES
|
#define PWM_MAX 245 // Max 98% of 2^PWM_RES
|
||||||
+81
-12
@@ -7,30 +7,99 @@
|
|||||||
#define INCLUDE_GAMEPAD_MODULE
|
#define INCLUDE_GAMEPAD_MODULE
|
||||||
#include <DabbleESP32.h>
|
#include <DabbleESP32.h>
|
||||||
|
|
||||||
|
void gamepadInput();
|
||||||
|
int8_t modSpeed(int8_t dir, int8_t speed);
|
||||||
|
|
||||||
MotorControl left_motor;
|
MotorControl left_motor;
|
||||||
|
MotorControl right_motor;
|
||||||
|
|
||||||
|
uint64_t last_millis = 0;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200); // make sure your Serial Monitor is also set at this baud rate.
|
Serial.begin(115200); // make sure your Serial Monitor is also set at this baud rate.
|
||||||
Dabble.begin("Kleiax-Rover"); //set bluetooth name of your device
|
Dabble.begin("Kleiax-Rover"); //set bluetooth name of your device
|
||||||
|
|
||||||
left_motor.init(M_PWM_1, PWM_CHANNEL_M1, M_DIR_11, M_DIR_12);
|
left_motor.init(M_PWM_1, PWM_CHANNEL_M1, M_DIR_11, M_DIR_12);
|
||||||
|
right_motor.init(M_PWM_2, PWM_CHANNEL_M2, M_DIR_21, M_DIR_22);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
delay(500);
|
if (millis() - last_millis > 300) {
|
||||||
left_motor.runMotorControl();
|
gamepadInput();
|
||||||
|
Serial.print("Motor Links: ");
|
||||||
left_motor.toString();
|
left_motor.toString();
|
||||||
Dabble.processInput(); //this function is used to refresh data obtained from smartphone.Hence calling this function is mandatory in order to get data properly from your mobile.
|
Serial.print("Motor Rechts: ");
|
||||||
//Serial.print("KeyPressed: ");
|
right_motor.toString();
|
||||||
if (GamePad.isUpPressed()) {
|
|
||||||
Serial.print("Up\n");
|
last_millis = millis();
|
||||||
left_motor.setTargetSpeed(100);
|
|
||||||
} else if (GamePad.isDownPressed()) {
|
|
||||||
Serial.print("Down\n");
|
|
||||||
left_motor.setTargetSpeed(-100);
|
|
||||||
} else if (GamePad.isCirclePressed()) {
|
|
||||||
left_motor.setTargetSpeed(0);
|
|
||||||
}
|
}
|
||||||
|
left_motor.runMotorControl();
|
||||||
|
right_motor.runMotorControl();
|
||||||
|
}
|
||||||
|
|
||||||
|
void gamepadInput() {
|
||||||
|
Dabble.processInput(); //this function is used to refresh data obtained from smartphone.Hence calling this function is mandatory in order to get data properly from your mobile.
|
||||||
|
//Serial.print("KeyPressed: ");
|
||||||
|
|
||||||
|
static int8_t speed_left = 0;
|
||||||
|
if (GamePad.isUpPressed()) {
|
||||||
|
speed_left = modSpeed(1, speed_left);
|
||||||
|
} else if (GamePad.isDownPressed()) {
|
||||||
|
speed_left = modSpeed(-1, speed_left);
|
||||||
|
} else {
|
||||||
|
speed_left = modSpeed(0, speed_left);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int8_t speed_right = 0;
|
||||||
|
if (GamePad.isTrianglePressed()) {
|
||||||
|
speed_right = modSpeed(1, speed_right);
|
||||||
|
} else if (GamePad.isCrossPressed()) {
|
||||||
|
speed_right = modSpeed(-1, speed_right);
|
||||||
|
} else {
|
||||||
|
speed_right = modSpeed(0, speed_right);
|
||||||
|
}
|
||||||
|
|
||||||
|
left_motor.setTargetSpeed(speed_left);
|
||||||
|
right_motor.setTargetSpeed(speed_right);
|
||||||
|
}
|
||||||
|
|
||||||
|
int8_t modSpeed(int8_t dir, int8_t speed) {
|
||||||
|
int8_t new_speed = 0;
|
||||||
|
if (dir == 1) {
|
||||||
|
if (speed > 0) {
|
||||||
|
new_speed = speed + 5;
|
||||||
|
} else if (speed == 0) {
|
||||||
|
new_speed = 5;
|
||||||
|
} else {
|
||||||
|
new_speed = speed + 5;
|
||||||
|
}
|
||||||
|
} else if (dir == 0) {
|
||||||
|
if (speed > 0) {
|
||||||
|
new_speed = speed - 5;
|
||||||
|
} else if (speed == 0) {
|
||||||
|
new_speed = 0;
|
||||||
|
} else {
|
||||||
|
new_speed = speed + 5;
|
||||||
|
}
|
||||||
|
} else if (dir == -1) {
|
||||||
|
if (speed > 0) {
|
||||||
|
new_speed = speed - 5;
|
||||||
|
} else if (speed == 0) {
|
||||||
|
new_speed = -5;
|
||||||
|
} else {
|
||||||
|
new_speed = speed - 5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (new_speed < -100) {
|
||||||
|
new_speed = -100;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (new_speed > 100) {
|
||||||
|
new_speed = 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new_speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,8 +26,7 @@ void MotorControl::init(uint8_t pwm_pin, uint8_t pwm_channel, uint8_t direction_
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MotorControl::runMotorControl() {
|
void MotorControl::runMotorControl() {
|
||||||
uint64_t now_millis = millis();
|
if (millis() - this->last_millis > 300) {
|
||||||
if (now_millis - this->last_millis > UPDATE_TIME) {
|
|
||||||
|
|
||||||
// Absolute difference between target_speed and speed
|
// Absolute difference between target_speed and speed
|
||||||
uint8_t abs_difference = abs(this->target_speed - this->speed);
|
uint8_t abs_difference = abs(this->target_speed - this->speed);
|
||||||
@@ -38,17 +37,18 @@ void MotorControl::runMotorControl() {
|
|||||||
// Check that the target speed is close to 0 and that the abs_difference is lower than SPEED_STEPS
|
// Check that the target speed is close to 0 and that the abs_difference is lower than SPEED_STEPS
|
||||||
if (abs(this->target_speed) < SPEED_STEPS && abs_difference < SPEED_STEPS) {
|
if (abs(this->target_speed) < SPEED_STEPS && abs_difference < SPEED_STEPS) {
|
||||||
this->setSpeed(0);
|
this->setSpeed(0);
|
||||||
|
this->last_millis = millis();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Correct speed
|
// Correct speed
|
||||||
if (abs_difference < SPEED_STEPS) {
|
if (abs_difference < SPEED_STEPS) {
|
||||||
|
this->last_millis = millis();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Positive or negative tagret speed
|
// Positive or negative tagret speed
|
||||||
if (this->target_speed >= 0) {
|
if (this->target_speed >= 0) {
|
||||||
|
|
||||||
// Positive or negative speed
|
// Positive or negative speed
|
||||||
if (this->speed >= 0) {
|
if (this->speed >= 0) {
|
||||||
if (difference > 0) {
|
if (difference > 0) {
|
||||||
@@ -61,7 +61,6 @@ void MotorControl::runMotorControl() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// Positive or negative speed
|
// Positive or negative speed
|
||||||
if (this->speed >= 0) {
|
if (this->speed >= 0) {
|
||||||
this->accelerate(-SPEED_STEPS);
|
this->accelerate(-SPEED_STEPS);
|
||||||
@@ -73,9 +72,8 @@ void MotorControl::runMotorControl() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
this->last_millis = millis();
|
this->last_millis = millis();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MotorControl::setTargetSpeed(int16_t speed) {
|
void MotorControl::setTargetSpeed(int16_t speed) {
|
||||||
@@ -140,7 +138,7 @@ void MotorControl::setSpeed(int16_t speed) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ledcWrite(this->pwm_channel, pwm_val);
|
ledcWrite(this->pwm_channel, pwm_val);
|
||||||
Serial.printf("pwm_val: %d, ", pwm_val);
|
// Serial.printf("pwm_val: %d, ", pwm_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MotorControl::accelerate(int8_t acc) {
|
void MotorControl::accelerate(int8_t acc) {
|
||||||
|
|||||||
Reference in New Issue
Block a user