Skip to content

Commit 0eb7cb0

Browse files
rpoiselRainer Poisel
authored andcommitted
Major cleanup
1 parent 8267af7 commit 0eb7cb0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+64
-78411
lines changed

.gitmodules

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
[submodule "Tick"]
2-
path = Tick
3-
url = https://github.com/geekfactory/Tick.git
4-
[submodule "SPI"]
5-
path = SPI
6-
url = https://github.com/geekfactory/SPI.git

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cmake_minimum_required(VERSION 3.6)
2+
project(PID LANGUAGES C VERSION 1.0.0)
3+
4+
add_library(PID STATIC
5+
PID.c
6+
)
7+
8+
set_target_properties(PID PROPERTIES
9+
POSITION_INDEPENDENT_CODE TRUE
10+
)

PID.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Author e-mail: ruben at geekfactory dot mx
1919
*/
2020
#include "PID.h"
21+
#include "PID_Support.h"
2122

2223
pid_t pid_create(pid_t pid, float* in, float* out, float* set, float kp, float ki, float kd)
2324
{
@@ -28,28 +29,32 @@ pid_t pid_create(pid_t pid, float* in, float* out, float* set, float kp, float k
2829

2930
pid_limits(pid, 0, 255);
3031

32+
pid->tickspersecond = pid_ticks_per_second();
33+
3134
// Set default sample time to 100 ms
32-
pid->sampletime = 100 * (TICK_SECOND / 1000);
35+
pid->sampletime = 100 * (pid->tickspersecond / 1000);
3336

3437
pid_direction(pid, E_PID_DIRECT);
3538
pid_tune(pid, kp, ki, kd);
3639

37-
pid->lasttime = tick_get() - pid->sampletime;
40+
pid->lasttime = pid_ticks_get() - pid->sampletime;
3841

3942
return pid;
4043
}
4144

4245
bool pid_need_compute(pid_t pid)
4346
{
4447
// Check if the PID period has elapsed
45-
return(tick_get() - pid->lasttime >= pid->sampletime) ? true : false;
48+
return(pid_ticks_get() - pid->lasttime >= pid->sampletime) ? true : false;
4649
}
4750

4851
void pid_compute(pid_t pid)
4952
{
5053
// Check if control is enabled
5154
if (!pid->automode)
52-
return false;
55+
{
56+
return;
57+
}
5358

5459
float in = *(pid->input);
5560
// Compute error
@@ -73,7 +78,7 @@ void pid_compute(pid_t pid)
7378
(*pid->output) = out;
7479
// Keep track of some variables for next execution
7580
pid->lastin = in;
76-
pid->lasttime = tick_get();;
81+
pid->lasttime = pid_ticks_get();;
7782
}
7883

7984
void pid_tune(pid_t pid, float kp, float ki, float kd)
@@ -83,7 +88,7 @@ void pid_tune(pid_t pid, float kp, float ki, float kd)
8388
return;
8489

8590
//Compute sample time in seconds
86-
float ssec = ((float) pid->sampletime) / ((float) TICK_SECOND);
91+
float ssec = ((float) pid->sampletime) / ((float) pid->tickspersecond);
8792

8893
pid->Kp = kp;
8994
pid->Ki = ki * ssec;
@@ -99,10 +104,10 @@ void pid_tune(pid_t pid, float kp, float ki, float kd)
99104
void pid_sample(pid_t pid, uint32_t time)
100105
{
101106
if (time > 0) {
102-
float ratio = (float) (time * (TICK_SECOND / 1000)) / (float) pid->sampletime;
107+
float ratio = (float) (time * (pid->tickspersecond / 1000)) / (float) pid->sampletime;
103108
pid->Ki *= ratio;
104109
pid->Kd /= ratio;
105-
pid->sampletime = time * (TICK_SECOND / 1000);
110+
pid->sampletime = time * (pid->tickspersecond / 1000);
106111
}
107112
}
108113

PID.h

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,10 @@
1919
*/
2020
#ifndef PID_H
2121
#define PID_H
22-
/*-------------------------------------------------------------*/
23-
/* Includes and dependencies */
24-
/*-------------------------------------------------------------*/
25-
#include "Tick/Tick.h"
22+
2623
#include <stdbool.h>
2724
#include <stdint.h>
2825

29-
/*-------------------------------------------------------------*/
30-
/* Macros and definitions */
31-
/*-------------------------------------------------------------*/
32-
33-
/*-------------------------------------------------------------*/
34-
/* Typedefs enums & structs */
35-
/*-------------------------------------------------------------*/
36-
37-
/**
38-
* Defines if the controler is direct or reverse
39-
*/
4026
enum pid_control_directions {
4127
E_PID_DIRECT,
4228
E_PID_REVERSE,
@@ -47,36 +33,34 @@ enum pid_control_directions {
4733
* posible using different structures for each controller
4834
*/
4935
struct pid_controller {
50-
// Input, output and setpoint
51-
float * input; //!< Current Process Value
52-
float * output; //!< Corrective Output from PID Controller
53-
float * setpoint; //!< Controller Setpoint
54-
// Tuning parameters
55-
float Kp; //!< Stores the gain for the Proportional term
56-
float Ki; //!< Stores the gain for the Integral term
57-
float Kd; //!< Stores the gain for the Derivative term
58-
// Output minimum and maximum values
59-
float omin; //!< Maximum value allowed at the output
60-
float omax; //!< Minimum value allowed at the output
61-
// Variables for PID algorithm
62-
float iterm; //!< Accumulator for integral term
63-
float lastin; //!< Last input value for differential term
64-
// Time related
65-
uint32_t lasttime; //!< Stores the time when the control loop ran last time
66-
uint32_t sampletime; //!< Defines the PID sample time
67-
// Operation mode
68-
uint8_t automode; //!< Defines if the PID controller is enabled or disabled
36+
/* Input, output and setpoint */
37+
float * input; /* Current Process Value */
38+
float * output; /* Corrective Output from PID Controller */
39+
float * setpoint; /* Controller Setpoint */
40+
/* Tuning parameters */
41+
float Kp; /* Stores the gain for the Proportional term */
42+
float Ki; /* Stores the gain for the Integral term */
43+
float Kd; /* Stores the gain for the Derivative term */
44+
/* Output minimum and maximum values */
45+
float omin; /* Maximum value allowed at the output */
46+
float omax; /* Minimum value allowed at the output */
47+
/* Variables for PID algorithm */
48+
float iterm; /* Accumulator for integral term */
49+
float lastin; /* Last input value for differential term */
50+
/* Time related */
51+
uint32_t tickspersecond;
52+
uint32_t lasttime; /* Stores the time when the control loop ran last time */
53+
uint32_t sampletime; /* Defines the PID sample time */
54+
/* Operation mode */
55+
uint8_t automode; /* Defines if the PID controller is enabled or disabled */
6956
enum pid_control_directions direction;
7057
};
7158

7259
typedef struct pid_controller * pid_t;
7360

74-
/*-------------------------------------------------------------*/
75-
/* Function prototypes */
76-
/*-------------------------------------------------------------*/
7761
#ifdef __cplusplus
7862
extern "C" {
79-
#endif
63+
#endif /* __cplusplus */
8064
/**
8165
* @brief Creates a new PID controller
8266
*
@@ -184,7 +168,6 @@ extern "C" {
184168

185169
#ifdef __cplusplus
186170
}
187-
#endif
171+
#endif /* __cplusplus */
188172

189-
#endif
190-
// End of Header file
173+
#endif /* PID_H */

PID_Support.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef PID_SUPPORT_H
2+
#define PID_SUPPORT_H
3+
4+
#include <stdint.h>
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif /* __cplusplus */
9+
10+
uint32_t pid_ticks_per_second(void); /* tbd by code using this library */
11+
uint32_t pid_ticks_get(void); /* tbd by code using this library */
12+
13+
#ifdef __cplusplus
14+
}
15+
#endif /* __cplusplus */
16+
17+
#endif /* PID_SUPPORT_H */

SPI

Lines changed: 0 additions & 1 deletion
This file was deleted.

Tick

Lines changed: 0 additions & 1 deletion
This file was deleted.

pid-demo-pic18.X/Makefile

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)