diff --git a/doc/PinBelegungen.txt b/doc/PinBelegungen.txt index 62981a2..7d5b2b0 100644 --- a/doc/PinBelegungen.txt +++ b/doc/PinBelegungen.txt @@ -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 diff --git a/include/network.h b/include/network.h index 5d79d66..f2a64c0 100644 --- a/include/network.h +++ b/include/network.h @@ -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 \ No newline at end of file diff --git a/lib/MotorControl/motorControl.cpp b/lib/MotorControl/motorControl.cpp index 86fefb2..e344118 100644 --- a/lib/MotorControl/motorControl.cpp +++ b/lib/MotorControl/motorControl.cpp @@ -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); diff --git a/src/driveModi/Modi/ManualControl/manualControl.cpp b/src/driveModi/Modi/ManualControl/manualControl.cpp index 86bfa81..561b98c 100644 --- a/src/driveModi/Modi/ManualControl/manualControl.cpp +++ b/src/driveModi/Modi/ManualControl/manualControl.cpp @@ -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() { diff --git a/src/driveModi/driveManager.cpp b/src/driveModi/driveManager.cpp index 2c20726..0827950 100644 --- a/src/driveModi/driveManager.cpp +++ b/src/driveModi/driveManager.cpp @@ -16,8 +16,8 @@ DriveManager::DriveManager(MoveControl *moveControl) { } void DriveManager::loop() { - this->currentModus->loop(); - this->moveControl->loop(); + if (currentModus) + this->currentModus->loop(); } void DriveManager::runDriveManager() { diff --git a/src/main.cpp b/src/main.cpp index 23dd089..0da9e0a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(); } diff --git a/src/moveControl.cpp b/src/moveControl.cpp index ed3211e..b77130c 100644 --- a/src/moveControl.cpp +++ b/src/moveControl.cpp @@ -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: diff --git a/src/network.cpp b/src/network.cpp index bdd4691..8a3788a 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -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; }