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 @@
#include
"Arduino.h"
//The following function needs modifications
DirectionControl
::
DirectionControl
(
int
servoPinIn
,
directionParameterSet
directionParameters
,
pidParameterSet
pidParameters
)
:
controller
(
pidParameters
){
}
...
...
@@ -13,19 +13,46 @@ void DirectionControl::setup(){
DirectionControl
::
directionServo
.
attach
(
DirectionControl
::
servoPin
);
// initialize servodirectionServo.attach(pin_servo); // initialize
}
//The following function needs modifications
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
()
{
}
//The following function needs modifications
void
DirectionControl
::
updateDirection
(){
}
//The following function needs modifications
void
DirectionControl
::
testServo
()
{
}
...
...
Sketch/DirectionControl.h
View file @
d1fa300d
/////////////////////////////////////////////////////////
///// This file does not need any modification, /////////
///// if needed it can be extended //////////////////////
/////////////////////////////////////////////////////////
#pragma once
#include
"PID.h"
...
...
@@ -13,12 +17,19 @@ class DirectionControl{
~
DirectionControl
();
int
getDirection
();
void
updateDirection
();
void
updateController
(
double
);
double
getControllerP
();
double
getControllerI
();
double
getControllerD
();
void
setControllerP
(
double
parameterP
);
void
setControllerI
(
double
parameterI
);
void
setControllerD
(
double
parameterD
);
void
testServo
();
void
setup
();
Servo
directionServo
;
Servo
directionServo
;
private:
PID
controller
;
...
...
Sketch/PID.cpp
View file @
d1fa300d
...
...
@@ -13,6 +13,7 @@ PID::PID(pidParameterSet parameterIn) {
PID
::~
PID
()
{}
//The following function needs modifications
double
PID
::
calcOutput
(
double
input
)
{
return
out
;
...
...
@@ -22,6 +23,35 @@ double PID::getBase() {
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
()
{
integral
=
0
;
}
...
...
Sketch/PID.h
View file @
d1fa300d
/////////////////////////////////////////////////////////
///// This file does not need any modification, /////////
///// if needed it can be extended //////////////////////
/////////////////////////////////////////////////////////
#pragma once
struct
pidParameterSet
{
...
...
@@ -14,6 +18,13 @@ class PID {
void
resetIntegrator
();
double
calcOutput
(
double
input
);
double
getBase
();
double
getP
();
double
getI
();
double
getD
();
void
setP
(
double
parameterP
);
void
setI
(
double
parameterI
);
void
setD
(
double
parameterD
);
private:
double
lastError
,
integral
;
...
...
Sketch/README.md
View file @
d1fa300d
...
...
@@ -2,4 +2,13 @@
https://www.arduino.cc/en/Main/Software
### Documentation ###
https://www.arduino.cc/en/Reference/HomePage
\ No newline at end of file
https://www.arduino.cc/en/Reference/HomePage
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
"DirectionControl.h"
#include
"SpeedSensor.h"
#include
"motorControl.h"
#include
"uart.h"
// pin declaration
// do NOT use DigitalOut 0 & 1 (reserved for Serial)
...
...
@@ -13,46 +14,42 @@ 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
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
});
MotorControl
motorControl
(
pin_deadManSwitch
,
pin_motor
,{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
});
SpeedSensor
speedSens
(
pin_speedSensor
);
//The following function needs modifications
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
();
initUart
(
&
directionControl
,
&
motorControl
);
}
/**
* Main loop
*/
//The following function needs modifications
void
loop
()
{
handleSerialInput
();
}
//The following function needs modifications
void
speedInterrupt
()
{
}
//The following function needs modifications
int
getAverage
(
int
pin
)
{
}
...
...
Sketch/SpeedSensor.cpp
View file @
d1fa300d
...
...
@@ -2,7 +2,7 @@
#include
"Arduino.h"
SpeedSensor
::
SpeedSensor
(
int
speedSendPinIn
){
SpeedSensor
::
speedSendPin
=
speedSendPinIn
;
}
SpeedSensor
::~
SpeedSensor
()
{
...
...
@@ -10,18 +10,21 @@ SpeedSensor::~SpeedSensor() {
void
SpeedSensor
::
setup
(){
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
)
{
}
//The following function needs modifications
bool
SpeedSensor
::
getState
()
{
}
//The following function needs modifications
double
SpeedSensor
::
getSpeed
()
{
}
Sketch/SpeedSensor.h
View file @
d1fa300d
/////////////////////////////////////////////////////////
///// This file does not need any modification, /////////
///// if needed it can be extended //////////////////////
/////////////////////////////////////////////////////////
#pragma once
extern
void
speedInterrupt
();
...
...
Sketch/motorControl.cpp
View file @
d1fa300d
...
...
@@ -2,28 +2,27 @@
#include
"Arduino.h"
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
(){
pinMode
(
MotorControl
::
motorPin
,
OUTPUT
);
MotorControl
::
setState
(
MOTOR_STATES
::
STOP
);
}
//The following function needs modifications
void
MotorControl
::
setSpeed
(
double
speedSetpoint
){
}
//The following function needs modifications
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
);
...
...
@@ -32,9 +31,6 @@ void MotorControl::updateMotor(void){
}
}
/**
* This function must not be changed
*/
void
MotorControl
::
setState
(
MOTOR_STATES
stateIn
){
if
(
MotorControl
::
motorState
!=
stateIn
){
MotorControl
::
motorState
=
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
#include
"PID.h"
...
...
@@ -14,6 +18,12 @@ class MotorControl {
void
setState
(
MOTOR_STATES
);
void
updateController
(
double
);
void
setup
();
double
getControllerP
();
double
getControllerI
();
double
getControllerD
();
void
setControllerP
(
double
parameterP
);
void
setControllerI
(
double
parameterI
);
void
setControllerD
(
double
parameterD
);
private:
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
;
}
}
memset
(
uartControl
.
inputBuffer
,
0
,
BUFFER_SIZE
);
uartControl
.
bufferPos
=
0
;
}
}
}
Sketch/uart.h
0 → 100644
View file @
d1fa300d
/////////////////////////////////////////////////////////
///// This file does not need any modification, /////////
///// if needed it can be extended //////////////////////
/////////////////////////////////////////////////////////
#pragma once
#include
"Arduino.h"
#include
"DirectionControl.h"
#include
"motorControl.h"
#define BUFFER_SIZE 50
#define CMD_END ';'
struct
{
char
inputBuffer
[
BUFFER_SIZE
];
uint8_t
bufferPos
;
uint8_t
voidCallbackCount
;
//number of callback function (will be auto initiated)
uint8_t
intCallbackCount
;
//number of callback function (will be auto initiated)
DirectionControl
*
directionControl
;
MotorControl
*
motorControl
;
}
uartControl
=
{{
0
},
0
,
0
};
void
initUart
(
DirectionControl
*
,
MotorControl
*
);
void
handleSerialInput
(
void
);
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment