Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ACS
Public
Teaching materials
4. Semester Projekt
Commits
d1fa300d
Commit
d1fa300d
authored
May 20, 2019
by
Manuel Pitz
Browse files
update for 2019
parent
fad8c6d3
Changes
12
Hide whitespace changes
Inline
Side-by-side
Sketch/DirectionControl.cpp
View file @
d1fa300d
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
#include
"Arduino.h"
#include
"Arduino.h"
//The following function needs modifications
DirectionControl
::
DirectionControl
(
int
servoPinIn
,
directionParameterSet
directionParameters
,
pidParameterSet
pidParameters
)
:
controller
(
pidParameters
){
DirectionControl
::
DirectionControl
(
int
servoPinIn
,
directionParameterSet
directionParameters
,
pidParameterSet
pidParameters
)
:
controller
(
pidParameters
){
}
}
...
@@ -13,19 +13,46 @@ void DirectionControl::setup(){
...
@@ -13,19 +13,46 @@ void DirectionControl::setup(){
DirectionControl
::
directionServo
.
attach
(
DirectionControl
::
servoPin
);
// initialize servodirectionServo.attach(pin_servo); // initialize
DirectionControl
::
directionServo
.
attach
(
DirectionControl
::
servoPin
);
// initialize servodirectionServo.attach(pin_servo); // initialize
}
}
//The following function needs modifications
void
DirectionControl
::
updateController
(
double
is
)
{
void
DirectionControl
::
updateController
(
double
is
)
{
}
}
double
DirectionControl
::
getControllerP
(){
return
controller
.
getP
();
}
double
DirectionControl
::
getControllerI
(){
return
controller
.
getI
();
}
double
DirectionControl
::
getControllerD
(){
return
controller
.
getD
();
}
void
DirectionControl
::
setControllerP
(
double
parameterP
){
controller
.
setP
(
parameterP
);
}
void
DirectionControl
::
setControllerI
(
double
parameterI
){
controller
.
setI
(
parameterI
);
}
void
DirectionControl
::
setControllerD
(
double
parameterD
){
controller
.
setD
(
parameterD
);
}
//The following function needs modifications
int
DirectionControl
::
getDirection
()
{
int
DirectionControl
::
getDirection
()
{
}
}
//The following function needs modifications
void
DirectionControl
::
updateDirection
(){
void
DirectionControl
::
updateDirection
(){
}
}
//The following function needs modifications
void
DirectionControl
::
testServo
()
{
void
DirectionControl
::
testServo
()
{
}
}
...
...
Sketch/DirectionControl.h
View file @
d1fa300d
/////////////////////////////////////////////////////////
///// This file does not need any modification, /////////
///// if needed it can be extended //////////////////////
/////////////////////////////////////////////////////////
#pragma once
#pragma once
#include
"PID.h"
#include
"PID.h"
...
@@ -13,12 +17,19 @@ class DirectionControl{
...
@@ -13,12 +17,19 @@ class DirectionControl{
~
DirectionControl
();
~
DirectionControl
();
int
getDirection
();
int
getDirection
();
void
updateDirection
();
void
updateDirection
();
void
updateController
(
double
);
void
updateController
(
double
);
double
getControllerP
();
double
getControllerI
();
double
getControllerD
();
void
setControllerP
(
double
parameterP
);
void
setControllerI
(
double
parameterI
);
void
setControllerD
(
double
parameterD
);
void
testServo
();
void
testServo
();
void
setup
();
void
setup
();
Servo
directionServo
;
Servo
directionServo
;
private:
private:
PID
controller
;
PID
controller
;
...
...
Sketch/PID.cpp
View file @
d1fa300d
...
@@ -13,6 +13,7 @@ PID::PID(pidParameterSet parameterIn) {
...
@@ -13,6 +13,7 @@ PID::PID(pidParameterSet parameterIn) {
PID
::~
PID
()
{}
PID
::~
PID
()
{}
//The following function needs modifications
double
PID
::
calcOutput
(
double
input
)
{
double
PID
::
calcOutput
(
double
input
)
{
return
out
;
return
out
;
...
@@ -22,6 +23,35 @@ double PID::getBase() {
...
@@ -22,6 +23,35 @@ double PID::getBase() {
return
base
;
return
base
;
}
}
double
PID
::
getP
(){
return
p
;
}
double
PID
::
getI
(){
return
i
;
}
double
PID
::
getD
(){
return
d
;
}
void
PID
::
setP
(
double
parameterP
){
p
=
parameterP
;
return
;
}
void
PID
::
setI
(
double
parameterI
){
i
=
parameterI
;
resetIntegrator
();
return
;
}
void
PID
::
setD
(
double
parameterD
){
d
=
parameterD
;
return
;
}
void
PID
::
resetIntegrator
()
{
void
PID
::
resetIntegrator
()
{
integral
=
0
;
integral
=
0
;
}
}
...
...
Sketch/PID.h
View file @
d1fa300d
/////////////////////////////////////////////////////////
///// This file does not need any modification, /////////
///// if needed it can be extended //////////////////////
/////////////////////////////////////////////////////////
#pragma once
#pragma once
struct
pidParameterSet
{
struct
pidParameterSet
{
...
@@ -14,6 +18,13 @@ class PID {
...
@@ -14,6 +18,13 @@ class PID {
void
resetIntegrator
();
void
resetIntegrator
();
double
calcOutput
(
double
input
);
double
calcOutput
(
double
input
);
double
getBase
();
double
getBase
();
double
getP
();
double
getI
();
double
getD
();
void
setP
(
double
parameterP
);
void
setI
(
double
parameterI
);
void
setD
(
double
parameterD
);
private:
private:
double
lastError
,
integral
;
double
lastError
,
integral
;
...
...
Sketch/README.md
View file @
d1fa300d
...
@@ -2,4 +2,13 @@
...
@@ -2,4 +2,13 @@
https://www.arduino.cc/en/Main/Software
https://www.arduino.cc/en/Main/Software
### Documentation ###
### Documentation ###
https://www.arduino.cc/en/Reference/HomePage
https://www.arduino.cc/en/Reference/HomePage
\ No newline at end of file
If files are marked in the head with the following disclaimer they don't need modification:
<pre>
This file does not need any modification
</pre>
If the following comment is before a function there are some changes needed:
<pre>
The following function needs modifications
</pre>
Sketch/Sketch.ino
View file @
d1fa300d
#include
<string.h>
#include
<math.h>
#include
<math.h>
#include
"DirectionControl.h"
#include
"DirectionControl.h"
#include
"SpeedSensor.h"
#include
"SpeedSensor.h"
#include
"motorControl.h"
#include
"motorControl.h"
#include
"uart.h"
// pin declaration
// pin declaration
// do NOT use DigitalOut 0 & 1 (reserved for Serial)
// do NOT use DigitalOut 0 & 1 (reserved for Serial)
...
@@ -13,46 +14,42 @@ const int pin_spuleLeft = 0;//Left resonant circuit
...
@@ -13,46 +14,42 @@ const int pin_spuleLeft = 0;//Left resonant circuit
const
int
pin_spuleRight
=
0
;
//Right resonant circuit
const
int
pin_spuleRight
=
0
;
//Right resonant circuit
const
int
pin_speedSensor
=
0
;
//Input signal for speed sensor signal
const
int
pin_speedSensor
=
0
;
//Input signal for speed sensor signal
//init global variables and objects
//init global variables and objects
const
int
directionServoCenter
=
0
;
//Center level for directionServo
const
int
directionServoCenter
=
0
;
//Center level for directionServo
const
int
directionMaxValue
=
0
;
const
int
directionMaxValue
=
0
;
const
int
directionMinValue
=
0
;
const
int
directionMinValue
=
0
;
const
int
ofTrackDetectionLevel
=
0
;
// minValue for off-track detection
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
});
DirectionControl
directionControl
=
DirectionControl
(
pin_servo
,{
directionMaxValue
,
directionMinValue
,
directionServoCenter
},{
0
,
0
,
0
,
0
,
directionServoCenter
,
0
,
directionMinValue
,
directionMaxValue
});
const
double
idleSpeed
=
0
;
//Speed setpoint for idle
const
double
idleSpeed
=
0
;
//Speed setpoint for idle
MotorControl
motorControl
(
pin_deadManSwitch
,
pin_motor
,{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
});
MotorControl
motorControl
(
pin_deadManSwitch
,
pin_motor
,{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
});
SpeedSensor
speedSens
(
pin_speedSensor
);
SpeedSensor
speedSens
(
pin_speedSensor
);
//The following function needs modifications
void
setup
()
{
void
setup
()
{
staticInitCode
();
//this function call must not be removed
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")
pinMode
(
LED_BUILTIN
,
OUTPUT
);
// declare onboard-led (indicates "on track")
directionControl
.
testServo
();
directionControl
.
testServo
();
initUart
(
&
directionControl
,
&
motorControl
);
}
}
/**
//The following function needs modifications
* Main loop
*/
void
loop
()
{
void
loop
()
{
handleSerialInput
();
}
}
//The following function needs modifications
void
speedInterrupt
()
{
void
speedInterrupt
()
{
}
}
//The following function needs modifications
int
getAverage
(
int
pin
)
{
int
getAverage
(
int
pin
)
{
}
}
...
...
Sketch/SpeedSensor.cpp
View file @
d1fa300d
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
#include
"Arduino.h"
#include
"Arduino.h"
SpeedSensor
::
SpeedSensor
(
int
speedSendPinIn
){
SpeedSensor
::
SpeedSensor
(
int
speedSendPinIn
){
SpeedSensor
::
speedSendPin
=
speedSendPinIn
;
}
}
SpeedSensor
::~
SpeedSensor
()
{
SpeedSensor
::~
SpeedSensor
()
{
...
@@ -10,18 +10,21 @@ SpeedSensor::~SpeedSensor() {
...
@@ -10,18 +10,21 @@ SpeedSensor::~SpeedSensor() {
void
SpeedSensor
::
setup
(){
void
SpeedSensor
::
setup
(){
pinMode
(
SpeedSensor
::
speedSendPin
,
INPUT
);
pinMode
(
SpeedSensor
::
speedSendPin
,
INPUT
);
attachInterrupt
(
digitalPinToInterrupt
(
SpeedSensor
::
speedSendPin
),
,
);
attachInterrupt
(
digitalPinToInterrupt
(
SpeedSensor
::
speedSendPin
),
speedInterrupt
,
CHANGE
);
}
}
//The following function needs modifications
void
SpeedSensor
::
interrupt
(
unsigned
long
now
)
{
void
SpeedSensor
::
interrupt
(
unsigned
long
now
)
{
}
}
//The following function needs modifications
bool
SpeedSensor
::
getState
()
{
bool
SpeedSensor
::
getState
()
{
}
}
//The following function needs modifications
double
SpeedSensor
::
getSpeed
()
{
double
SpeedSensor
::
getSpeed
()
{
}
}
Sketch/SpeedSensor.h
View file @
d1fa300d
/////////////////////////////////////////////////////////
///// This file does not need any modification, /////////
///// if needed it can be extended //////////////////////
/////////////////////////////////////////////////////////
#pragma once
#pragma once
extern
void
speedInterrupt
();
extern
void
speedInterrupt
();
...
...
Sketch/motorControl.cpp
View file @
d1fa300d
...
@@ -2,28 +2,27 @@
...
@@ -2,28 +2,27 @@
#include
"Arduino.h"
#include
"Arduino.h"
MotorControl
::
MotorControl
(
unsigned
int
deadManSwitchPinIn
,
unsigned
int
motorPinIn
,
pidParameterSet
pidParameters
)
:
controller
(
pidParameters
)
{
MotorControl
::
MotorControl
(
unsigned
int
deadManSwitchPinIn
,
unsigned
int
motorPinIn
,
pidParameterSet
pidParameters
)
:
controller
(
pidParameters
)
{
MotorControl
::
motorDuty
=
0
;
MotorControl
::
motorPin
=
motorPinIn
;
MotorControl
::
deadManSwitchPin
=
deadManSwitchPinIn
;
}
}
/**
* This function must not be changed
*/
void
MotorControl
::
setup
(){
void
MotorControl
::
setup
(){
pinMode
(
MotorControl
::
motorPin
,
OUTPUT
);
pinMode
(
MotorControl
::
motorPin
,
OUTPUT
);
MotorControl
::
setState
(
MOTOR_STATES
::
STOP
);
MotorControl
::
setState
(
MOTOR_STATES
::
STOP
);
}
}
//The following function needs modifications
void
MotorControl
::
setSpeed
(
double
speedSetpoint
){
void
MotorControl
::
setSpeed
(
double
speedSetpoint
){
}
}
//The following function needs modifications
void
MotorControl
::
updateController
(
double
currentSpeed
){
void
MotorControl
::
updateController
(
double
currentSpeed
){
}
}
/**
* This function must not be changed
*/
void
MotorControl
::
updateMotor
(
void
){
void
MotorControl
::
updateMotor
(
void
){
if
(
MotorControl
::
motorState
==
MOTOR_STATES
::
RUN
&&
!
digitalRead
(
MotorControl
::
deadManSwitchPin
)){
if
(
MotorControl
::
motorState
==
MOTOR_STATES
::
RUN
&&
!
digitalRead
(
MotorControl
::
deadManSwitchPin
)){
analogWrite
(
MotorControl
::
motorPin
,
MotorControl
::
motorDuty
);
analogWrite
(
MotorControl
::
motorPin
,
MotorControl
::
motorDuty
);
...
@@ -32,9 +31,6 @@ void MotorControl::updateMotor(void){
...
@@ -32,9 +31,6 @@ void MotorControl::updateMotor(void){
}
}
}
}
/**
* This function must not be changed
*/
void
MotorControl
::
setState
(
MOTOR_STATES
stateIn
){
void
MotorControl
::
setState
(
MOTOR_STATES
stateIn
){
if
(
MotorControl
::
motorState
!=
stateIn
){
if
(
MotorControl
::
motorState
!=
stateIn
){
MotorControl
::
motorState
=
stateIn
;
MotorControl
::
motorState
=
stateIn
;
...
@@ -45,3 +41,27 @@ void MotorControl::setState(MOTOR_STATES stateIn){
...
@@ -45,3 +41,27 @@ void MotorControl::setState(MOTOR_STATES stateIn){
}
}
}
}
double
MotorControl
::
getControllerP
(){
return
controller
.
getP
();
}
double
MotorControl
::
getControllerI
(){
return
controller
.
getI
();
}
double
MotorControl
::
getControllerD
(){
return
controller
.
getD
();
}
void
MotorControl
::
setControllerP
(
double
parameterP
){
controller
.
setP
(
parameterP
);
}
void
MotorControl
::
setControllerI
(
double
parameterI
){
controller
.
setI
(
parameterI
);
}
void
MotorControl
::
setControllerD
(
double
parameterD
){
controller
.
setD
(
parameterD
);
}
Sketch/motorControl.h
View file @
d1fa300d
/////////////////////////////////////////////////////////
///// This file does not need any modification, /////////
///// if needed it can be extended //////////////////////
/////////////////////////////////////////////////////////
#pragma once
#pragma once
#include
"PID.h"
#include
"PID.h"
...
@@ -14,6 +18,12 @@ class MotorControl {
...
@@ -14,6 +18,12 @@ class MotorControl {
void
setState
(
MOTOR_STATES
);
void
setState
(
MOTOR_STATES
);
void
updateController
(
double
);
void
updateController
(
double
);
void
setup
();
void
setup
();
double
getControllerP
();
double
getControllerI
();
double
getControllerD
();
void
setControllerP
(
double
parameterP
);
void
setControllerI
(
double
parameterI
);
void
setControllerD
(
double
parameterD
);
private:
private:
unsigned
int
motorPin
;
unsigned
int
motorPin
;
...
...
Sketch/uart.cpp
0 → 100644
View file @
d1fa300d
/////////////////////////////////////////////////////////
///// This file does not need any modification, /////////
///// if needed it can be extended with more handlers ///
/////////////////////////////////////////////////////////
#include
"uart.h"
void
getMotorP
(
double
inputNumber
){
Serial
.
print
(
"motorControl P = "
);
Serial
.
println
(
uartControl
.
motorControl
->
getControllerP
());
Serial
.
flush
();
//wait for outgoing serial transmission to end
}
void
getMotorI
(
double
inputNumber
){
Serial
.
print
(
"motorControl I = "
);
Serial
.
println
(
uartControl
.
motorControl
->
getControllerI
());
Serial
.
flush
();
//wait for outgoing serial transmission to end
}
void
getMotorD
(
double
inputNumber
){
Serial
.
print
(
"motorControl D = "
);
Serial
.
println
(
uartControl
.
motorControl
->
getControllerD
());
Serial
.
flush
();
//wait for outgoing serial transmission to end
}
void
setMotorP
(
double
inputNumber
){
uartControl
.
motorControl
->
setControllerP
(
inputNumber
);
getMotorP
(
inputNumber
);
}
void
setMotorI
(
double
inputNumber
){
uartControl
.
motorControl
->
setControllerI
(
inputNumber
);
getMotorI
(
inputNumber
);
}
void
setMotorD
(
double
inputNumber
){
uartControl
.
motorControl
->
setControllerD
(
inputNumber
);
getMotorD
(
inputNumber
);
}
void
getDirP
(
double
inputNumber
){
Serial
.
print
(
"directionControl P = "
);
Serial
.
println
(
uartControl
.
directionControl
->
getControllerP
());
Serial
.
flush
();
//wait for outgoing serial transmission to end
}
void
getDirI
(
double
inputNumber
){
Serial
.
print
(
"directionControl I = "
);
Serial
.
println
(
uartControl
.
directionControl
->
getControllerI
());
Serial
.
flush
();
//wait for outgoing serial transmission to end
}
void
getDirD
(
double
inputNumber
){
Serial
.
print
(
"directionControl D = "
);
Serial
.
println
(
uartControl
.
directionControl
->
getControllerD
());
Serial
.
flush
();
//wait for outgoing serial transmission to end
}
void
setDirP
(
double
inputNumber
){
uartControl
.
directionControl
->
setControllerP
(
inputNumber
);
getDirP
(
inputNumber
);
}
void
setDirI
(
double
inputNumber
){
uartControl
.
directionControl
->
setControllerI
(
inputNumber
);
getDirI
(
inputNumber
);
}
void
setDirD
(
double
inputNumber
){
uartControl
.
directionControl
->
setControllerD
(
inputNumber
);
getDirD
(
inputNumber
);
}
void
help
(
double
inputNumber
){
Serial
.
println
(
"BEGIN OF HELP"
);
Serial
.
println
(
""
);
Serial
.
println
(
"Commands must be terminated with ;!"
);
Serial
.
println
(
"With set command the value comes immidiately after command!"
);
Serial
.
println
(
"Example: setDirP12.5;"
);
Serial
.
println
(
""
);
Serial
.
println
(
"Available commands:"
);
Serial
.
println
(
"getMotorP: get P of motor control"
);
Serial
.
println
(
"getMotorI: get I of motor control"
);
Serial
.
println
(
"getMotorD: get D of motor control"
);
Serial
.
println
(
"setMotorP: set P of motor control"
);
Serial
.
println
(
"setMotorI: set I of motor control"
);
Serial
.
println
(
"setMotorD: set D of motor control"
);
Serial
.
println
(
"getDirP: get P of direction control"
);
Serial
.
println
(
"getDirI: get I of direction control"
);
Serial
.
println
(
"getDirD: get D of direction control"
);
Serial
.
println
(
"setDirP: set P of direction control"
);
Serial
.
println
(
"setDirI: set I of direction control"
);
Serial
.
println
(
"setDirD: set D of direction control"
);
Serial
.
println
(
"help: show help"
);
Serial
.
println
(
""
);
Serial
.
println
(
"END OF HELP"
);
Serial
.
flush
();
//wait for outgoing serial transmission to end
}
//the following two arrays have to have the SAME number of elements each command corrosbinding to one function pointer
//list of commands
const
char
*
callbackName
[]
=
{
"getDirP"
,
"getDirI"
,
"getDirD"
,
"setDirP"
,
"setDirI"
,
"setDirD"
,
"getMotorP"
,
"getMotorI"
,
"getMotorD"
,
"setMotorP"
,
"setMotorI"
,
"setMotorD"
,
"help"
};
//list of function pointer from above
void
(
*
callbackFunc
[])(
double
inputNumber
)
=
{
getDirP
,
getDirI
,
getDirD
,
setDirP
,
setDirI
,
setDirD
,
getMotorP
,
getMotorI
,
getMotorD
,
setMotorP
,
setMotorI
,
setMotorD
,
help
};
//////////////////////////////////////////////////////
//////////before this only handler functions /////////
//////////////////////////////////////////////////////
void
initUart
(
DirectionControl
*
directionControlIn
,
MotorControl
*
motorControlIn
){
uartControl
.
directionControl
=
directionControlIn
;
uartControl
.
motorControl
=
motorControlIn
;
Serial
.
begin
(
9600
);
// for serial communication
while
(
!
Serial
){;}
//wait for serial to be available
Serial
.
println
(
"Beginning Serial Transmission"
);
Serial
.
flush
();
//wait for outgoing serial transmission to end
}
void
handleSerialInput
(){
uartControl
.
intCallbackCount
=
sizeof
(
callbackFunc
)
/
sizeof
(
callbackFunc
[
0
]);
if
(
Serial
.
available
()
>
0
){
char
c
=
Serial
.
read
();
if
(
c
!=
CMD_END
&&
uartControl
.
bufferPos
<
BUFFER_SIZE
)
{
uartControl
.
inputBuffer
[
uartControl
.
bufferPos
++
]
=
c
;
}
else
{
//Serial.println(byteBuffer);
for
(
int
i
=
0
;
i
<
uartControl
.
intCallbackCount
;
i
++
)
{
if
(
strncmp
(
callbackName
[
i
],
uartControl
.
inputBuffer
,
strlen
(
callbackName
[
i
]))
==
0
)
{
double
num
=
atof
(
uartControl
.
inputBuffer
+
strlen
(
callbackName
[
i
]));
callbackFunc
[
i
](
num
);
break
;
}