Lauffähig und aufgeräumt

This commit is contained in:
2021-12-16 17:09:19 +01:00
parent cd29191bb4
commit 29a636060c
8 changed files with 79 additions and 49 deletions
+11 -11
View File
@@ -1,20 +1,20 @@
ESP32 Rover Pinbelegung ESP32 Rover Pinbelegung
3V3 GND 3V3 GND
x 23 DirB1 x 23 DirR1
x 22 DirB2 x 22 PWMR
x x x TX PC
x x x RX PC
x 21 AKKU 35 21
INTA1 32 GND INTL1 32 GND
INTA2 33 19 INTL2 33 19
INTB1 25 18 INTB1 25 18
INTB2 26 5 INTB2 26 5
PWMA 27 x DirL1 27 TX GPS
PWMB 14 x DirR2 14 RX GPS
DirA1 12 4 DirL2 12 4 SD-Card
GND x GND x
DirA2 13 2 PWML 13 2 SD-Card
x 15 x 15
x x x x
CMD x CMD x
+18 -12
View File
@@ -1,7 +1,7 @@
/** /**
* @file network.h * @file network.h
* @author Alexander Klein (alex@kleiax.de) * @author Alexander Klein (alex@kleiax.de)
* @brief Some network and MQTT stuff * @brief TODO: write some stuff here too
* @version 0.1 * @version 0.1
* @date 2021-12-14 * @date 2021-12-14
* *
@@ -19,18 +19,24 @@
#define MQTT_TIME_RECONNECT 2500 #define MQTT_TIME_RECONNECT 2500
IPAddress local_IP; class Network {
extern IPAddress gateway; public:
extern IPAddress subnet; static void setIps();
extern IPAddress mqtt_server; static void connectWifi();
static void setupMQTT();
static void checkMQTT();
static PubSubClient* getMqtttClient();
extern WiFiClient wifi_client; private:
PubSubClient mqtt_client(wifi_client); static bool connectMQTT();
void setIps(); static IPAddress local_IP;
void connectWifi(); static IPAddress gateway;
void setupMQTT(); static IPAddress subnet;
bool connectMQTT(); static IPAddress mqtt_server;
void checkMQTT();
static WiFiClient wifi_client;
static PubSubClient* mqtt_client;
};
#endif // NETWORK_H #endif // NETWORK_H
+3 -3
View File
@@ -94,14 +94,14 @@ void MotorControl::runMotorControl() {
void MotorControl::setMinPwm(uint8_t min) { void MotorControl::setMinPwm(uint8_t min) {
if (min > 80) min = 80; if (min > 80) min = 80;
//transform percentage to real pwm value //transform percentage to real pwm value
min = (uint8_t) (((1 >> pwm_res) - 1) * (min / 100)); min = (uint8_t) (((1 << pwm_res) - 1) * (min / 100.0));
this->dutycycle_min = min; this->dutycycle_min = min;
} }
void MotorControl::setMaxPwm(uint8_t max) { void MotorControl::setMaxPwm(uint8_t max) {
if (max > 100) max = 100; if (max > 100) max = 100;
//transform percentage to real pwm value //transform percentage to real pwm value
max = (uint8_t) (((1 >> pwm_res) - 1) * (max / 100)); max = (uint8_t) (((1 << pwm_res) - 1) * (max / 100.0));
this->dutycycle_max = max; this->dutycycle_max = max;
} }
@@ -166,7 +166,7 @@ void MotorControl::setRealPower(int8_t power) {
return; return;
} }
if (power == 0) { if (this->power == 0) {
this->direction = 0; this->direction = 0;
digitalWrite(this->dir_1, LOW); digitalWrite(this->dir_1, LOW);
digitalWrite(this->dir_2, LOW); digitalWrite(this->dir_2, LOW);
@@ -24,10 +24,14 @@ void ManualControl::init(MoveControl *moveControl) {
} }
void ManualControl::loop() { void ManualControl::loop() {
this->moveControl->loop();
static uint32_t last_millis = 0; static uint32_t last_millis = 0;
if (millis() - last_millis < delay) { if (millis() - last_millis < delay) {
return; return;
} }
this->runManualControl();
last_millis = millis();
} }
void ManualControl::runManualControl() { void ManualControl::runManualControl() {
+2 -2
View File
@@ -16,8 +16,8 @@ DriveManager::DriveManager(MoveControl *moveControl) {
} }
void DriveManager::loop() { void DriveManager::loop() {
this->currentModus->loop(); if (currentModus)
this->moveControl->loop(); this->currentModus->loop();
} }
void DriveManager::runDriveManager() { void DriveManager::runDriveManager() {
+7 -4
View File
@@ -18,19 +18,22 @@ DriveManager driveManager(&moveController);
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Serial.println("Welcome...");
setIps(); Network::setIps();
connectWifi(); Network::connectWifi();
setupMQTT(); Network::setupMQTT();
Ps3.attach(callbackControllerAction); Ps3.attach(callbackControllerAction);
Ps3.attachOnConnect(callbackControllerConnect); Ps3.attachOnConnect(callbackControllerConnect);
Serial.println("\nReady to connect a PS3 Controller... \n"); Serial.println("\nReady to connect a PS3 Controller... \n");
Ps3.begin(); Ps3.begin();
driveManager.changeModus(Modi::ManualControl);
} }
void loop() { void loop() {
checkMQTT(); Network::checkMQTT();
driveManager.loop(); driveManager.loop();
} }
+2 -3
View File
@@ -1,8 +1,8 @@
#include "moveControl.h" #include "moveControl.h"
MoveControl::MoveControl() { MoveControl::MoveControl() {
this->left_motor = new MotorControl; this->left_motor = new MotorControl();
this->right_motor = new MotorControl; this->right_motor = new MotorControl();
this->left_speedometer = new Speedometer; this->left_speedometer = new Speedometer;
this->right_speedometer = new Speedometer; this->right_speedometer = new Speedometer;
@@ -113,7 +113,6 @@ void MoveControl::regulateMotors() {
case DrivingStatus::drive : case DrivingStatus::drive :
this->left_motor->setTargetPower( (int8_t) this->left_pid_out); this->left_motor->setTargetPower( (int8_t) this->left_pid_out);
this->right_motor->setTargetPower( (int8_t) this->right_pid_out); this->right_motor->setTargetPower( (int8_t) this->right_pid_out);
break; break;
default: default:
+32 -14
View File
@@ -1,7 +1,7 @@
/** /**
* @file network.cpp * @file network.cpp
* @author Alexander Klein (alex@kleiax.de) * @author Alexander Klein (alex@kleiax.de)
* @brief Contains function implementation of network.h * @brief TODO: write some stuff
* @version 0.1 * @version 0.1
* @date 2021-12-14 * @date 2021-12-14
* *
@@ -10,36 +10,50 @@
*/ */
#include "network.h" #include "network.h"
void setIps() {
IPAddress Network::local_IP;
IPAddress Network::gateway;
IPAddress Network::subnet;
IPAddress Network::mqtt_server;
WiFiClient Network::wifi_client;
PubSubClient* Network::mqtt_client;
void Network::setIps() {
local_IP.fromString(WLAN_IP); local_IP.fromString(WLAN_IP);
gateway.fromString(WLAN_GATEWAY); gateway.fromString(WLAN_GATEWAY);
subnet.fromString(WLAN_SUBNETMASK); subnet.fromString(WLAN_SUBNETMASK);
mqtt_server.fromString(MQTT_SERVER); mqtt_server.fromString(MQTT_SERVER);
} }
void setupMQTT() { void Network::setupMQTT() {
mqtt_client.setServer(mqtt_server, MQTT_PORT); mqtt_client = new PubSubClient(wifi_client);
mqtt_client->setServer(mqtt_server, MQTT_PORT);
// mqtt_client.setServer(MQTT_SERVER, MQTT_PORT); // mqtt_client.setServer(MQTT_SERVER, MQTT_PORT);
mqtt_client.setSocketTimeout(1); Network::mqtt_client->setSocketTimeout(1);
mqtt_client->setSocketTimeout(1);
connectMQTT(); connectMQTT();
} }
bool connectMQTT() { bool Network::connectMQTT() {
// Create a random client ID // Create a random client ID
String clientId = "ESP32Rover-"; String clientId = "ESP32Rover-";
clientId += String(random(0xffff), HEX); clientId += String(random(0xffff), HEX);
#ifdef MQTT_AUTH #ifdef MQTT_AUTH
if (mqtt_client.connect(clientId.c_str(), MQTT_USER, MQTT_PASSWORD)) { if (mqtt_client->connect(clientId.c_str(), MQTT_USER, MQTT_PASSWORD)) {
#endif //MQTT_AUTH #endif //MQTT_AUTH
#ifndef MQTT_AUTH #ifndef MQTT_AUTH
if (mqtt_client.connect(clientId.c_str())) { if (mqtt_client->connect(clientId.c_str())) {
#endif //MQTT_AUTH #endif //MQTT_AUTH
// Once connected, publish an announcement... // Once connected, publish an announcement...
mqtt_client.publish("Rover/Info", "Connected to Mqtt-Broker"); mqtt_client->publish("Rover/Info", "Connected to Mqtt-Broker");
} }
return mqtt_client.connected(); return mqtt_client->connected();
} }
void connectWifi() { void Network::connectWifi() {
// Configures static IP address // Configures static IP address
if (!WiFi.config(local_IP, gateway, subnet)) { if (!WiFi.config(local_IP, gateway, subnet)) {
Serial.println("STA Failed to configure"); Serial.println("STA Failed to configure");
@@ -61,9 +75,9 @@ void connectWifi() {
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
} }
void checkMQTT() { void Network::checkMQTT() {
static uint64_t lastReconnectAttempt = 0; static uint64_t lastReconnectAttempt = 0;
if (!mqtt_client.connected()) { if (!mqtt_client->connected()) {
long now = millis(); long now = millis();
if (now - lastReconnectAttempt > MQTT_TIME_RECONNECT) { if (now - lastReconnectAttempt > MQTT_TIME_RECONNECT) {
lastReconnectAttempt = now; lastReconnectAttempt = now;
@@ -72,5 +86,9 @@ void checkMQTT() {
lastReconnectAttempt = 0; lastReconnectAttempt = 0;
} }
} else } else
mqtt_client.loop(); mqtt_client->loop();
}
PubSubClient* Network::getMqtttClient() {
return mqtt_client;
} }