Merge branch 'feature/#5-motion' into feature/#14-implement-motion-detection

This commit is contained in:
hhaupt 2024-05-07 22:17:49 +02:00
commit 8db66a0a19
8 changed files with 182 additions and 26 deletions

View File

@ -136,6 +136,10 @@ For instance, in `src/Dezibot.h`, to include `src/motion/Motion.h`, you should w
* USB Mode: "Hardware CDC and JTAG" * USB Mode: "Hardware CDC and JTAG"
* Programmer: "Esptool" * Programmer: "Esptool"
Using `arduino-cli` to compile and upload:
`arduino-cli upload /Users/jo/Documents/Arduino/theSketch -p /dev/cu.usbmodem101 -b esp32:esp32:nora_w10`
`arduino-cli compile /Users/jo/Documents/Arduino/theSketch -p /dev/cu.usbmodem101 -b esp32:esp32:nora_w10`
#### Display #### Display
It is important to specify the SDA and SCL ports by using `Wire.begin(SDA, SCL)`. It is important to specify the SDA and SCL ports by using `Wire.begin(SDA, SCL)`.
@ -169,4 +173,3 @@ void Dezibot::begin(void) {
vTaskDelay(2000); vTaskDelay(2000);
} }
``` ```

View File

@ -1,20 +0,0 @@
#include "Dezibot.h"
Dezibot dezibot;
void setup() {
dezibot.begin();
dezibot.motionDetection.begin();
//dezibot.motionDetection.end();
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
//Serial.println(dezibot.motionDetection.getTemperature());
Serial.println(dezibot.motionDetection.getAcceleration().z);
//Serial.println(dezibot.motionDetection.getRotation().x);
Serial.println(dezibot.motionDetection.getWhoAmI());
delay(5000);
}

View File

@ -0,0 +1,19 @@
#include <Dezibot.h>
Dezibot dezibot = Dezibot();
const uint8_t MYFOO = 10;
void setup() {
dezibot.begin();
}
void loop() {
dezibot.motion.move();
delay(1000);
dezibot.motion.rotateAntiClockwise();
delay(1000);
dezibot.motion.rotateClockwise();
delay(1000);
dezibot.motion.stop();
delay(1000);
}

20
example/motion/motion.ino Normal file
View File

@ -0,0 +1,20 @@
#include <Dezibot.h>
Dezibot dezibot = Dezibot();
void setup() {
dezibot.begin();
Serial.begin(9600);
}
void loop() {
Serial.println("bla");
dezibot.motion.move(1000);
dezibot.multiColorLight.setLed(BOTTOM, RED);
delay(2000);
dezibot.motion.rotateAnticlockwise(1000);
dezibot.multiColorLight.setLed(BOTTOM, GREEN);
delay(2000);
dezibot.motion.rotateClockwise(1000);
dezibot.multiColorLight.setLed(BOTTOM, BLUE);
delay(2000);
dezibot.multiColorLight.turnOffLed();
delay(2000);
}

View File

@ -1,5 +0,0 @@
#include "Dezibot.h"
int main(){
return 0;
}

View File

@ -16,5 +16,6 @@ Dezibot::Dezibot():multiColorLight(),motionDetection(){
}; };
void Dezibot::begin(void) { void Dezibot::begin(void) {
motion.begin();
multiColorLight.begin(); multiColorLight.begin();
}; };

81
src/motion/Motion.cpp Normal file
View File

@ -0,0 +1,81 @@
/**
* @file Motion.cpp
* @author Jonathan Schulze, Nick Hübenthal, Hans Haupt
* @brief Implementation of the Motion class.
* @version 0.1
* @date 2023-12-13
*
* @copyright Copyright (c) 2023
*
*/
#include "Motion.h"
TaskHandle_t xMoveTaskHandle = NULL;
TaskHandle_t xClockwiseTaskHandle = NULL;
TaskHandle_t xAntiClockwiseTaskHandle = NULL;
// Initialize the movement component.
void Motion::begin(void) {
};
void Motion::moveTask(void * args) {
analogWrite(MOTOR_LEFT_PIN, LEFT_MOTOR_DUTY);
analogWrite(MOTOR_RIGHT_PIN, RIGHT_MOTOR_DUTY);
vTaskDelay((uint32_t) args / portTICK_PERIOD_MS);
analogWrite(MOTOR_LEFT_PIN, 0);
analogWrite(MOTOR_RIGHT_PIN, 0);
vTaskDelete(xMoveTaskHandle);
};
// Move forward for a certain amount of time.
void Motion::move(uint32_t moveForMs) {
if (moveForMs > 0){
xTaskCreate(moveTask, "Move", 4096, (void*)moveForMs, 10, &xMoveTaskHandle);
} else{
analogWrite(MOTOR_LEFT_PIN, LEFT_MOTOR_DUTY);
analogWrite(MOTOR_RIGHT_PIN, RIGHT_MOTOR_DUTY);
}
};
void Motion::leftMotorTask(void * args) {
analogWrite(MOTOR_LEFT_PIN, LEFT_MOTOR_DUTY);
analogWrite(MOTOR_RIGHT_PIN, 0);
vTaskDelay((uint32_t) args / portTICK_PERIOD_MS);
analogWrite(MOTOR_LEFT_PIN, 0);
vTaskDelete(xClockwiseTaskHandle);
};
// Rotate clockwise for a certain amount of time.
void Motion::rotateClockwise(uint32_t rotateForMs) {
if (rotateForMs > 0){
xTaskCreate(leftMotorTask, "LeftMotor", 4096, (void*)rotateForMs, 10, &xClockwiseTaskHandle);
} else {
analogWrite(MOTOR_LEFT_PIN,LEFT_MOTOR_DUTY);
analogWrite(MOTOR_RIGHT_PIN,0);
}
};
void Motion::rightMotorTask(void * args) {
analogWrite(MOTOR_RIGHT_PIN, RIGHT_MOTOR_DUTY);
analogWrite(MOTOR_LEFT_PIN,0);
vTaskDelay((uint32_t) args / portTICK_PERIOD_MS);
analogWrite(MOTOR_RIGHT_PIN, 0);
vTaskDelete(xAntiClockwiseTaskHandle);
};
// Rotate anticlockwise for a certain amount of time.
void Motion::rotateAntiClockwise(uint32_t rotateForMs) {
if(rotateForMs > 0){
xTaskCreate(rightMotorTask, "RightMotor", 4096, (void*)rotateForMs, 10, &xAntiClockwiseTaskHandle);
} else {
analogWrite(MOTOR_RIGHT_PIN,RIGHT_MOTOR_DUTY);
analogWrite(MOTOR_LEFT_PIN,0);
}
};
void Motion::stop(void){
analogWrite(MOTOR_LEFT_PIN,0);
analogWrite(MOTOR_RIGHT_PIN,0);
}

View File

@ -1,7 +1,64 @@
/**
* @file Motion.h
* @author Jonathan Schulze, Nick Hübenthal
* @brief This component controls the ability to rotate and change position.
* @version 0.1
* @date 2023-12-13
*
* @copyright Copyright (c) 2023
*
*/
#ifndef Motion_h #ifndef Motion_h
#define Motion_h #define Motion_h
#include <stdint.h>
#include <Arduino.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
class Motion{ class Motion{
protected:
static const uint8_t RIGHT_MOTOR_DUTY = 128;
static const uint8_t LEFT_MOTOR_DUTY = 128;
static const int MOTOR_RIGHT_PIN = 11;
static const int MOTOR_LEFT_PIN = 12;
static void moveTask(void * args);
static void leftMotorTask(void * args);
static void rightMotorTask(void * args);
public:
/**
* @brief Initialize the movement component.
*
*/
void begin(void);
/**
* @brief Move forward for a certain amount of time.
* Call with moveForMs 0 will start movement, that must be stopped explicit by call to stop().
* @param moveForMs Representing the duration of forward moving in milliseconds.
*/
static void move(uint32_t moveForMs=0);
/**
* @brief Rotate clockwise for a certain amount of time.
* Call with moveForMs 0 will start movement, that must be stopped explicit by call to stop().
* @param rotateForMs Representing the duration of rotating clockwise in milliseconds.
*/
static void rotateClockwise(uint32_t rotateForMs=0);
/**
* @brief Rotate anticlockwise for a certain amount of time.
* Call with moveForMs 0 will start movement, that must be stopped explicit by call to stop().
* @param rotateForMs Representing the duration of rotating anticlockwise in milliseconds.
*/
static void rotateAntiClockwise(uint32_t rotateForMs=0);
/**
* @brief stops any current movement, no matter if timebased or endless
*
*/
static void stop(void);
}; };
#endif //Motion_h #endif //Motion_h