block_bridge_pwm.c 4.55 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2011

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Jeroen Vreeken's avatar
Jeroen Vreeken committed
23
#include <controller/controller_block.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
24
25
26
27
28
29
30
31
32
33
34
35

/*
       inputs      outputs
       nr name     nr name

     ----------------------
     |                    |
  ---| 0  in       0  a   |----
     |	                  |
     |             1  b   |----
     |	                  |
     ----------------------
36
37
38
39
40
41
42

	This block generates an PWM signal based on the input value
	which can be used to control a H-bridge.
	
	 0.0 <= in < 1.0	a = PWM modulated, b = false
	-1.0 <= in < 0.0	a = false, b = PWM modulated
	 0.0 == in		a = false, b = false;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
     
 */

struct controller_block_private {
	float *in;
	bool   a;
	bool   b;
	int    divider;
	int    continuous;
	
	int    cnt;
};

static void calculate(struct controller_block *bridge)
{
	bool a, b, pwm;
	struct controller_block_private *priv = bridge->private;
	int cnt = priv->cnt;
	float value;
	float in = *priv->in;
	int divider = priv->divider;
	
	if (in > 0.0) {
		a = true;
		b = false;
	} else if (in < 0.0) {
		a = false;
		b = true;
	} else {
		a = false;
		b = false;
	}

	cnt++;
	cnt = cnt % divider;
	priv->cnt = cnt;
	
	value = (fabsf(in) * divider) + 0.5;
	if (cnt > value) {
		pwm = false;
	} else {
		if (!priv->continuous && cnt == divider-1) {
			pwm = false;
		} else {
			pwm = true;
		}
	}
	
	priv->a = a & pwm;
	priv->b = b & pwm;
}

static struct controller_block_param_list params[] = {
96
97
98
	/* Divider between sample frequency and PWM frequency. 
	   This also influences the available number of PWM steps.
	 */
Jeroen Vreeken's avatar
Jeroen Vreeken committed
99
	{ "divider",       false },
100
101
102
103
	/* Are we allowed to switch an output continuously on?
	   Set this to false if the driver needs a switching output 
	   for generating proper gate voltage.
	 */
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
	{ "continuous_on", false },
	{ NULL },
};

static void param_get(struct controller_block *bridge, int param, void *val)
{
	switch (param) {
		case 0:
			*(int*)val = bridge->private->divider;
			break;
		case 1:
			*(int*)val = bridge->private->continuous;
			break;
	}
}

static void param_set(struct controller_block *bridge, int param, va_list val)
{
	switch (param) {
		case 0:
			bridge->private->divider = va_arg(val, int);
			break;
		case 1:
			bridge->private->continuous = va_arg(val, int);
			break;
	}
}

struct controller_block * block_bridge_pwm_create(char *name)
{
	struct controller_block *bridge;
	
	bridge = malloc(sizeof(struct controller_block));
	if (!bridge)
		return NULL;

	bridge->type = "bridge_pwm";
	bridge->name = malloc(strlen(name)+1);
	if (!bridge->name)
		goto err_bridge;
	strcpy(bridge->name, name);

	bridge->private = malloc(sizeof(struct controller_block_private));
	if (!bridge->private)
		goto err_name;
	bridge->private->a = false;
	bridge->private->b = false;
	bridge->private->divider = 1;
	bridge->private->continuous = true;

	bridge->inputs = 1;
	bridge->input = malloc(sizeof(struct controller_block_interm));
	if (!bridge->input)
		goto err_private;
	bridge->input[0].name = "in";
	bridge->input[0].type = CONTROLLER_BLOCK_TERM_FLOAT;
	bridge->input[0].value.f = &bridge->private->in;
	bridge->input[0].ghostof = NULL;

	bridge->outputs = 2;
	bridge->output = malloc(sizeof(struct controller_block_outterm) * 2);
	if (!bridge->output)
		goto err_input;
	bridge->output[0].name = "a";
	bridge->output[0].type = CONTROLLER_BLOCK_TERM_BOOL;
	bridge->output[0].value.b = &bridge->private->a;
	bridge->output[0].source = bridge;
	bridge->output[1].name = "b";
	bridge->output[1].type = CONTROLLER_BLOCK_TERM_BOOL;
	bridge->output[1].value.b = &bridge->private->b;
	bridge->output[1].source = bridge;

	bridge->calculate = calculate;

	if (controller_block_param_list_init(bridge, params))
		goto err_output;

	bridge->param_get = param_get;
	bridge->param_set = param_set;

	controller_block_add(bridge);
	return bridge;

err_output:
	free(bridge->output);
err_input:
	free(bridge->input);
err_private:
	free(bridge->private);
err_name:
	free(bridge->name);
err_bridge:
	free(bridge);
	return NULL;
}