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