Commit fad8c6d3 authored by Manuel Pitz's avatar Manuel Pitz
Browse files

Students version of the software structure for the 4th semester project

car
parents
# Institutsprojekt 4. Semester #
## Aufbau eines elektrischen Modellautos ##
\ No newline at end of file
#include "DirectionControl.h"
#include "Arduino.h"
DirectionControl::DirectionControl(int servoPinIn, directionParameterSet directionParameters, pidParameterSet pidParameters) : controller(pidParameters){
}
DirectionControl::~DirectionControl(){}
void DirectionControl::setup(){
DirectionControl::directionServo.attach(DirectionControl::servoPin); // initialize servodirectionServo.attach(pin_servo); // initialize
}
void DirectionControl::updateController(double is) {
}
int DirectionControl::getDirection() {
}
void DirectionControl::updateDirection(){
}
void DirectionControl::testServo() {
}
#pragma once
#include "PID.h"
#include <Servo.h>
struct directionParameterSet{
int maxPos, minPos, centerPos;
};
class DirectionControl{
public:
DirectionControl(int,directionParameterSet,pidParameterSet);
~DirectionControl();
int getDirection();
void updateDirection();
void updateController(double);
void testServo();
void setup();
Servo directionServo;
private:
PID controller;
int servoPin;
int centerPos;
int maxPos;
int minPos;
unsigned int currentDirection;
};
#include "PID.h"
PID::PID(pidParameterSet parameterIn) {
PID::p = parameterIn.p;
PID::i = parameterIn.i;
PID::d = parameterIn.d;
PID::soll = parameterIn.soll;
PID::base = parameterIn.base;
PID::antiWindup = parameterIn.antiWindup;
PID::outMin = parameterIn.outMin;
PID::outMax = parameterIn.outMax;
}
PID::~PID() {}
double PID::calcOutput(double input) {
return out;
}
double PID::getBase() {
return base;
}
void PID::resetIntegrator() {
integral = 0;
}
void PID::setSoll(double soll) {
PID::soll = soll;
}
#pragma once
struct pidParameterSet{
double p, i, d, soll, base, antiWindup, outMin, outMax;
};
class PID {
public:
PID(pidParameterSet);
~PID();
void setSoll(double soll);
void resetIntegrator();
double calcOutput(double input);
double getBase();
private:
double lastError, integral;
double p, i, d;
double outMin = 0, outMax = 255, soll, base;
double antiWindup;
};
### Arduino-Software ###
https://www.arduino.cc/en/Main/Software
### Documentation ###
https://www.arduino.cc/en/Reference/HomePage
\ No newline at end of file
#include <string.h>
#include <math.h>
#include "DirectionControl.h"
#include "SpeedSensor.h"
#include "motorControl.h"
// pin declaration
// do NOT use DigitalOut 0 & 1 (reserved for Serial)
const int pin_servo = 0;//pin for steering control
const int pin_motor = 0;//pin for motor control
const int pin_deadManSwitch = 0;//dead man swich pin
const int pin_spuleLeft = 0;//Left resonant circuit
const int pin_spuleRight = 0;//Right resonant circuit
const int pin_speedSensor = 0;//Input signal for speed sensor signal
//init global variables and objects
const int directionServoCenter = 0;//Center level for directionServo
const int directionMaxValue = 0;
const int directionMinValue = 0;
const int ofTrackDetectionLevel = 0; // minValue for off-track detection
DirectionControl directionControl = DirectionControl(pin_servo,{directionMaxValue,directionMinValue,directionServoCenter},{0, 0, 0, 0, directionServoCenter, 0, directionMinValue, directionMaxValue});
const double idleSpeed = 0;//Speed setpoint for idle
MotorControl motorControl(pin_deadManSwitch,pin_motor,{0,0,0,0,0,0,0,0});
SpeedSensor speedSens(pin_speedSensor);
void setup() {
staticInitCode();//this function call must not be removed
//debugging
Serial.begin(9600); // for serial logging
pinMode(LED_BUILTIN, OUTPUT); // declare onboard-led (indicates "on track")
directionControl.testServo();
}
/**
* Main loop
*/
void loop() {
}
void speedInterrupt() {
}
int getAverage(int pin) {
}
/**
* The following code must not be changed
*/
void staticInitCode(){
pinMode(pin_deadManSwitch, INPUT_PULLUP);//Set pullup for dead man switch
}
#include "SpeedSensor.h"
#include "Arduino.h"
SpeedSensor::SpeedSensor(int speedSendPinIn){
}
SpeedSensor::~SpeedSensor() {
}
void SpeedSensor::setup(){
pinMode(SpeedSensor::speedSendPin, INPUT);
attachInterrupt(digitalPinToInterrupt(SpeedSensor::speedSendPin), , );
}
void SpeedSensor::interrupt(unsigned long now) {
}
bool SpeedSensor::getState() {
}
double SpeedSensor::getSpeed() {
}
#pragma once
extern void speedInterrupt();
class SpeedSensor {
public:
SpeedSensor(int);
~SpeedSensor();
void interrupt(unsigned long microseconds);
bool getState();
double getSpeed();
void setup();
private:
int speedSendPin;
bool state = false;
long rawValArray[8] = {0};
int arraySize = 8;
unsigned long lastTurn = 0;
unsigned long avgPos = 0;
unsigned long avgVal = 0;
unsigned int initOk = 0;
double currentSpeed = 0;
};
#include "motorControl.h"
#include "Arduino.h"
MotorControl::MotorControl(unsigned int deadManSwitchPinIn,unsigned int motorPinIn, pidParameterSet pidParameters) : controller(pidParameters) {
}
/**
* This function must not be changed
*/
void MotorControl::setup(){
pinMode(MotorControl::motorPin, OUTPUT);
MotorControl::setState(MOTOR_STATES::STOP);
}
void MotorControl::setSpeed(double speedSetpoint){
}
void MotorControl::updateController(double currentSpeed){
}
/**
* This function must not be changed
*/
void MotorControl::updateMotor(void){
if(MotorControl::motorState==MOTOR_STATES::RUN && !digitalRead(MotorControl::deadManSwitchPin)){
analogWrite(MotorControl::motorPin, MotorControl::motorDuty);
}else{
analogWrite(MotorControl::motorPin, 0);//Stop motor
}
}
/**
* This function must not be changed
*/
void MotorControl::setState(MOTOR_STATES stateIn){
if(MotorControl::motorState != stateIn){
MotorControl::motorState = stateIn;
if(MotorControl::motorState==MOTOR_STATES::RUN){
MotorControl::controller.resetIntegrator();
}
MotorControl::updateMotor();//update Motor in case of a state change
}
}
#pragma once
#include "PID.h"
enum class MOTOR_STATES{STOP,RUN};
class MotorControl {
public:
MotorControl(unsigned int,unsigned int,pidParameterSet);
void setSpeed(double);
void updateMotor(void);
void setState(MOTOR_STATES);
void updateController(double);
void setup();
private:
unsigned int motorPin;
unsigned int deadManSwitchPin;
int motorDuty;
PID controller;
MOTOR_STATES motorState=MOTOR_STATES::STOP;
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment