-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
166 lines (144 loc) · 3.64 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "fonts.h"
#include "math.h"
#include "my_clock.h"
#include "my_encoder.h"
#include "my_gpio.h"
#include "my_pid.h"
#include "my_systick.h"
#include "my_timer.h"
#include "remote.h"
#include "st7735.h"
#include "stm32f10x.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
/*PIN CONFIGURATION
A->PA9
B->PA8
PWM PA7
IN1 PC0
IN2 PC1
*/
#define IN1 PC0
#define IN2 PC1
// #define REMOTE_PIN PA10
uint16_t delay_calc;
uint16_t display_counter;
int16_t set_point = -1500;
char string[100];
PID_Controller pid;
Encoder enc;
remote_controller remote;
int16_t input_num;
bool key_pressed = false;
void remote_cmd(const remote_controller *remote);
int main()
{
SystemInit();
setCLK(S72MHZ);
// setCLK(S8MHZ);
//@param format for pid_controller, add output limits as no load speed limit
pid.kp =0.08;//0.00038;//0.001; //0.2;
pid.ki =0.05;//0.005;//0.000001;//0.00001;//0.000001; //0.02;
pid.kd =0;//0.000001;//0.0002;
enc.sensorA_pin = PA8;
enc.sensorB_pin = PA9;
enc.PPR = 100; // 100;
enc.max_RPM = 6000;
pid_init(&pid, 0.01, 0.0079577, 100, 8); //* 10-5 ms
init_encoder(&enc, T1, TIM4_CH4); //@param PWM at 2KHz/0.5 miliseconds
outMODE(IN1);
outMODE(IN2);
pinOFF(IN1);
pinOFF(IN2);
ST7735_Init(72); // 8
ST7735_FillScreen(ST7735_COLOR_WHITE);
sprintf(string, "PID control Set point: %d RPM Cur point: %d RPM", set_point,
(int)enc.RPM);
ST7735_WriteString(0, 0, string, Font_11x18, ST7735_COLOR_BLACK, ST7735_COLOR_WHITE);
delay_calc = 0;
display_counter = 0;
remote.pin = PA10;
remote_init(&remote);
startTIMER(T1);
// startTIMER(T3);
startTIMER(T4);
systickCONFIG(72); // 8
startTICK();
timerINT_50MS(T2, 10);
startTIMER(T2);
while (true)
{
if (delay_calc == 10) //*every 20 ms
{
enc.prev_pulse_count = enc.pulse_count;
enc.pulse_count = TIM1->CNT;
update_RPM(&enc, 10);
pid_update(&pid, set_point, enc.filtered_RPM);
controller_dir(&pid, &enc, IN1, IN2);
TIM4->CCR4 = fabs(round(pid.output));
// TIM4->CCR4=20;//5ms
delay_calc = 0;
}
if (display_counter >= 15000) //*every 500 msec
{
set_point+=200;
if(set_point>=1500)
set_point=-1500;
/*sprintf(string, "PID control Set point: %d RPM Cur point: %d RPM", set_point,
(int)(enc.filtered_RPM));
ST7735_WriteString(0, 0, string, Font_11x18, ST7735_COLOR_BLACK, ST7735_COLOR_WHITE);*/
display_counter = 0;
}
if (key_pressed)
{
remote_cmd(&remote);
key_pressed = false;
}
}
return 0;
}
void TIM2_IRQHandler(void)
{
TIM2->SR &= ~TIM_SR_UIF;
delay_calc = 10;
display_counter += 10;
}
void EXTI15_10_IRQHandler(void)
{
update_remote_key(&remote, NEC);
key_pressed = true;
// while(!pinR(remote.pin));
EXTI->PR |= EXTI_PR_PR10;
}
void remote_cmd(const remote_controller *remote)
{
if (remote->key == '\0')
return;
switch (remote->key)
{
case '*':
case '#':
input_num = 0;
break;
case '>':
input_num *= input_num < 0 ? -1 : 1;
break;
case '<':
input_num *= input_num > 0 ? -1 : 1;
break;
case '^':
input_num++;
break;
case 'D':
input_num--;
break;
case 'k':
set_point = input_num;
break;
default:
input_num *= 10;
input_num += (remote->key - '0');
break;
}
}