ec_stoeber.c 9.16 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
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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2008, 2009
	Copyright Stichting C.A. Muller Radioastronomiestation, 2008, 2009

	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 <math.h>

#include "esc.h"
#include "ec_stoeber.h"

int ec_stoeber_init(struct ec_stoeber *stbr)
{
	int status;
	uint32_t leval32;
	uint16_t leval16;
	uint8_t val8;
	struct esc_mailbox *mb;
	struct esc_syncmanager_info mb_rd, mb_wr, pdo_tx, pdo_rx;
	struct canopen_dev *canopen_dev;
	uint32_t vendorid, productcode, serialno;
	
//	if (ec_init(stbr->dev))
//		return -1;
	
	vendorid = esc_esi_vendorid_get(&stbr->addr);
	productcode = esc_esi_productcode_get(&stbr->addr);
	serialno = esc_esi_serialno_get(&stbr->addr);
	printf("VendorId: 0x%04x, ProductCode: 0x%04x, SerialNo: 0x%04x\n",
	    vendorid, productcode, serialno);
	/* TODO: check values */
	
	if (esc_init(&stbr->addr))
		return -1;
	
	status = esc_al_status_code_get(&stbr->addr);
	if (status != 0) {
		printf("status code: 0x%x\n", status);
		esc_al_error_ack(&stbr->addr);
	}
	
	if (esc_al_state_set(&stbr->addr, ESC_AL_STATE_INIT) < 0)
		return -1;
	
	printf("Initializing mailbox\n");
	
	
	esc_dc_init(&stbr->addr);
	esc_dc_sync0_cycle_time(&stbr->addr, 4000*1000);


	stbr->addr.addr.position.off = 0x0000;
	
	
	/* mailbox read (slave->master) @0x18f6 */
	mb_rd.start = 0x18f6;
	mb_rd.len = 0x0040;
	mb_rd.sm = 1;
	
	/* mailbox write (master->slave) @0x1800 */
	mb_wr.start = 0x1800;
	mb_wr.len = 0x0040;
	mb_wr.sm = 0;
	
	mb = esc_mailbox_create(&stbr->addr, &mb_rd, &mb_wr);
	canopen_dev = esc_coe_create(mb);
	
	printf("Going to state pre-operational\n");
	
	if (esc_al_state_set(&stbr->addr, ESC_AL_STATE_PRE_OPERATIONAL) < 0)
		return -1;

	/* A180 device control, disable it for now */
	val8 = 0x00;
	canopen_write_param(canopen_dev, 0x20b4, 0x0, &val8, 1);

	/* A61 fault reset source to parameter */
	val8 = 2;
	canopen_write_param(canopen_dev, 0x203d, 0x0, &val8, 1);
	usleep(3000);
	/* A180 device control, reset faults */
	val8 = 0x02;
	canopen_write_param(canopen_dev, 0x20b4, 0x0, &val8, 1);
	usleep(50000);
	/* A180 device control, reset faults */
	val8 = 0x00;
	canopen_write_param(canopen_dev, 0x20b4, 0x0, &val8, 1);

	/* A21 brake resistor */
	leval16 = htole16(stbr->brake_resistor * 10.0);
	canopen_write_param(canopen_dev, 0x2015, 0x0, &leval16, 2);
	/* A22 brake resistor power */
	leval16 = htole16(stbr->brake_power);
	canopen_write_param(canopen_dev, 0x2016, 0x0, &leval16, 2);

	/* A29 fault quickstop */
	val8 = 1;
	canopen_write_param(canopen_dev, 0x201d, 0x0, &val8, 1);

	/* A34 auto start */
	val8 = 1;
	canopen_write_param(canopen_dev, 0x2022, 0x0, &val8, 1);
	
	/* A45*/
	val8 = 1;
	canopen_write_param(canopen_dev, 0x202d, 0x0, &val8, 1);
	
	/* A60*/
	val8 = 1;
	canopen_write_param(canopen_dev, 0x203c, 0x0, &val8, 1);
	
	/* A150 cycle time */
	val8 = 0x06;
	canopen_write_param(canopen_dev, 0x2096, 0x0, &val8, 1);

	/* A258 set PDO timeout */
	leval16 = htole16(1000);
	canopen_write_param(canopen_dev, 0x2102, 0x0, &leval16, 2);

	/* G90 activate PLL */
	val8 = 1;
	canopen_write_param(canopen_dev, 0x2c5a, 0x0, &val8, 1);

	/* G91 pll phase offset */
	leval32 = htole32(0);
	canopen_write_param(canopen_dev, 0x2c5b, 0x0, &leval32, 4);

	/* G92 pll gain pk */
	leval32 = htole32(10);
	canopen_write_param(canopen_dev, 0x2c5c, 0x0, &leval32, 4);

	/* G93 pll low pass */
	leval32 = htole32(999);
	canopen_write_param(canopen_dev, 0x2c5d, 0x0, &leval32, 4);

	/* G98 cycle to 4ms */
	leval16 = htole16(4000);
	canopen_write_param(canopen_dev, 0x2c62, 0x0, &leval16, 2);

	/* Set I06 to 2 decimal digits */
	val8 = 2;
	canopen_write_param(canopen_dev, 0x3006, 0x0, &val8, 1);

	/* C32 (motor integrator) to 3000 ms */
	leval16 = htole16(32000);
	canopen_write_param(canopen_dev, 0x2420, 0x0, &leval16, 2);

	/* C33 low pass reference speed 1 ms */
	leval32 = htole32(10);
	canopen_write_param(canopen_dev, 0x2421, 0x0, &leval32, 4);

	/* C130 torque limit source to parameter */
	val8 = 4;
	canopen_write_param(canopen_dev, 0x2482, 0x0, &val8, 1);

	/* Set I10 to maximum speed */
	leval32 = htole32(stbr->max_speed / (2*M_PI) * 360 * 100);
//	leval32 = htole32(3000 * 360 / 60 * 100);
	canopen_write_param(canopen_dev, 0x300a, 0x0, &leval32, 4);

	/* Set I11 to maximum acceleration */
//	leval32 = htole32(2000000 * 100);
	leval32 = htole32(stbr->max_accel * 100);
	canopen_write_param(canopen_dev, 0x300b, 0x0, &leval32, 4);
		
	/* Set I25 to 100% speed ff */
	leval16 = htole16(100);
	canopen_write_param(canopen_dev, 0x3019, 0x0, &leval16, 2);

	/* Set I30 to 1 (encoder signal 0) */
	val8 = 1;
	canopen_write_param(canopen_dev, 0x301e, 0x0, &val8, 1);

	/* Set I37 to 1 (active) */
	val8 = 1;
	canopen_write_param(canopen_dev, 0x3025, 0x0, &val8, 1);

	/* Set I100 to parameter (execute source) */
	val8 = 2;
	canopen_write_param(canopen_dev, 0x3064, 0x0, &val8, 1);
		
	/* B17 Torque M0 */
	canopen_read_param(canopen_dev, 0x2211, 0x0, &leval32, 4);
	printf("B17: %08x %d\n", le32toh(leval32), le32toh(leval32));
	stbr->standstill_torque = (float)(le32toh(leval32)) / 1000.0;
	printf("Standstill torque: %f\n", stbr->standstill_torque);

	/* C03 max pos torque 150% of standstill */
	leval32 = htole32(150);
	canopen_write_param(canopen_dev, 0x2403, 0x0, &leval32, 4);

	/* C05 max neg torque -150% of standstill */
	leval32 = htole32(-150);
	canopen_write_param(canopen_dev, 0x2405, 0x0, &leval32, 4);

//	/* A180 device control, enable it to make sure we stay
//	   at the same position */
//	val8 = 0x01;
//	canopen_write_param(canopen_dev, 0x20b4, 0x0, &val8, 1);

	/* I211 command byte to 4 (move_velocity) */
	val8 = 0x04;
	canopen_write_param(canopen_dev, 0x30d3, 0x0, &val8, 1);
	/* I215 v to 0 */
	leval16 = htole16(0);
	canopen_write_param(canopen_dev, 0x30d7, 0x0, &leval16, 2);
	/* I210 latch command */
	val8 = 0x00;
	canopen_write_param(canopen_dev, 0x30d2, 0x0, &val8, 1);
	val8 = 0x01;
	canopen_write_param(canopen_dev, 0x30d2, 0x0, &val8, 1);

	/* Transmit pdo: */

	/* I80 (4bytes) */
	leval32 = STOEBER_PDO_MAP(0x3050, 0, 32);
	canopen_write_param(canopen_dev, 0x20e9, 0x0, &leval32, 4);
		
	/* E91 (4bytes) */
	leval32 = STOEBER_PDO_MAP(0x285b, 0, 32);
	canopen_write_param(canopen_dev, 0x20e9, 0x1, &leval32, 4);

	/* E90 (2bytes) */
	leval32 = STOEBER_PDO_MAP(0x285a, 0, 16);
	canopen_write_param(canopen_dev, 0x20e9, 0x2, &leval32, 4);
		
	/* E19 (2bytes) */
	leval32 = STOEBER_PDO_MAP(0x2813, 0, 16);
	canopen_write_param(canopen_dev, 0x20e9, 0x3, &leval32, 4);

	/* E10 (2bytes) */
	leval32 = STOEBER_PDO_MAP(0x280a, 0, 16);
	canopen_write_param(canopen_dev, 0x20e9, 0x4, &leval32, 4);

	leval32 = 0;
	canopen_write_param(canopen_dev, 0x20e9, 0x5, &leval32, 4);

	/* Receive pdo: */

	/* A180 (1byte) */
	leval32 = STOEBER_PDO_MAP(0x20b4, 0, 32);
	canopen_write_param(canopen_dev, 0x20e1, 0x0, &leval32, 4);
		
	/* F210 (1byte) */
	leval32 = STOEBER_PDO_MAP(0x2ad2, 0, 32);
	canopen_write_param(canopen_dev, 0x20e1, 0x1, &leval32, 4);

	/* I210 (2bytes) */
	leval32 = STOEBER_PDO_MAP(0x30d2, 0, 16);
	canopen_write_param(canopen_dev, 0x20e1, 0x2, &leval32, 4);
		
	/* I213 (4bytes) */
	leval32 = STOEBER_PDO_MAP(0x30d5, 0, 16);
	canopen_write_param(canopen_dev, 0x20e1, 0x3, &leval32, 4);

	/* I215 (2bytes) */
	leval32 = STOEBER_PDO_MAP(0x30d7, 0, 16);
	canopen_write_param(canopen_dev, 0x20e1, 0x4, &leval32, 4);

	/* C230 (2bytes) */
	leval32 = STOEBER_PDO_MAP(0x24e6, 0, 16);
	canopen_write_param(canopen_dev, 0x20e1, 0x5, &leval32, 4);

	esc_coe_destroy(canopen_dev);
	esc_mailbox_destroy(mb);
	

	printf("Setup PDOs\n");

	/* sm2 tx pdo (master->slave) @0x1600 */

	pdo_tx.start = 0x1600;
	pdo_tx.len = 0x000c;
	pdo_tx.sm = 2;

	/* sm3 rx pdo (slave->master) @0x1a00 */

	pdo_rx.start = 0x1a00;
	pdo_rx.len = 0x000e;
	pdo_rx.sm = 3;
		
	esc_pdo_tx_set(&stbr->addr, &pdo_tx, &stbr->tx_buffer, false);
	esc_pdo_rx_set(&stbr->addr, &pdo_rx, &stbr->rx_buffer);
	
	printf("going to safe operational\n");
	
	if (esc_al_state_set(&stbr->addr, ESC_AL_STATE_SAFE_OPERATIONAL) < 0)
		return -1;

	stbr->rx_I80 = (uint32_t*)(stbr->rx_buffer + STOEBER_PDO_I80);
	stbr->rx_E91 = (uint32_t*)(stbr->rx_buffer + STOEBER_PDO_E91);
	stbr->rx_E90 = (uint16_t*)(stbr->rx_buffer + STOEBER_PDO_E90);
	stbr->rx_E19 = (uint16_t*)(stbr->rx_buffer + STOEBER_PDO_E19);
	stbr->rx_E10 = (uint16_t*)(stbr->rx_buffer + STOEBER_PDO_E10);
	
	stbr->tx_A180 = (uint8_t*)(stbr->tx_buffer + STOEBER_PDO_A180);
	stbr->tx_F210 = (uint8_t*)(stbr->tx_buffer + STOEBER_PDO_F210);
	stbr->tx_I210 = (uint16_t*)(stbr->tx_buffer + STOEBER_PDO_I210);
	stbr->tx_I213 = (uint32_t*)(stbr->tx_buffer + STOEBER_PDO_I213);
	stbr->tx_I215 = (uint16_t*)(stbr->tx_buffer + STOEBER_PDO_I215);
	stbr->tx_C230 = (uint16_t*)(stbr->tx_buffer + STOEBER_PDO_C230);

	printf("Done\n");
	return 0;

}