block_beckhoff_el7031.c 21.4 KB
Newer Older
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
/*
 * Copyright (c) 2012,
 *      Daan Vreeken <Daan @ Vitsch . nl> - Vitsch Electronics
 *      All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

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

#include <controller/controller_block.h>

#include "ec.h"
#include "esc.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
37
38
39
#include "esc_esi.h"
#include "esc_coe.h"
#include "esc_id.h"
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
#include "log.h"

struct controller_block_private {
	unsigned char		*rx_buffer;
	unsigned char		*tx_buffer;

	struct ec_dgram_addr	addr;
	struct canopen_dev	*can_dev;
	int			fail_cnt;
	
	int32_t			unwound_cnt;
	int32_t			pos_out;
	int32_t			calib_pos;
	uint16_t		last_cnt;
	bool			state_ok;
	
	bool			*enable;
	bool			*calib;
	int16_t			*speed;
};

struct rxpdo_t {
	// PDO 0x1600
	uint8_t			enc_ctrl[4];
	// PDO 0x1602
	uint8_t			stm_ctrl[2];
	// PDO 0x1604
	int16_t			stm_velocity;
} __packed;

struct txpdo_t {
	// PDO 0x1a00
	uint8_t			status_compact[2];
	uint16_t		counter_val;
	uint16_t		latch_val;
	// PDO 0x1a03
	int8_t			full_status[2];
} __packed;

#define FULL_STATUS_FLAG_READY	(1 << 1)

#define MAX_FAIL_CNT		50


// een paar definities voor de leesbaarheid van de code
#define OUTPUT(name)		(priv->name)
#define INPUT(name)		(*priv->name)
#define VAR(name)		(priv->name)


// see:
// http://infosys.beckhoff.com/espanol.php?content=../content/1034/el70x1/html/bt_el7031_objectdescription.htm&id=7476

#define REG_ACTUAL_POS		0x6020, 0x11

#define REG_OUTP_ENABLE		0x7010, 1
#define REG_OUTP_RESET		0x7010, 2
#define REG_OUTP_REDUCE_TORQUE	0x7010, 3
#define REG_OUTP_POSITION	0x7010, 0x11
#define REG_OUTP_VELOCITY	0x7010, 0x21

#define REG_MAX_CURRENT		0x8010, 1	// *1mA
#define REG_REDUCED_CURRENT	0x8010, 2	// *1mA
#define REG_NOMINAL_VOLTAGE	0x8010, 3	// *1mV, max=24000
#define REG_MOT_COIL_RESISTANCE	0x8010, 4	// *.01 Ohm
#define REG_MOT_EMF		0x8010, 5	// ? 1mV / 1000 digits
#define REG_MOT_FULL_STEPS	0x8010, 6	// steps / revolution
#define REG_MAX_START_FREQ	0x8010, 9
#define REG_DRIVE_ON_DELAY	0x8010, 0x10
#define REG_DRIVE_OFF_DELAY	0x8010, 0x11

#define REG_OPERATING_MODE	0x8012, 1
#define OPMODE_DIRECT_VELOCITY			1

#define REG_ACCEL_POS		0x8020, 0x03
#define REG_ACCEL_NEG		0x8020, 0x04
#define REG_DECEL_POS		0x8020, 0x05
#define REG_DECEL_NEG		0x8020, 0x06
#define REG_CALIB_POS		0x8020, 0x08

#define REG_STATUS_WORD		0x9010, 1

// at this moment, the motor's specs are hard-coded for the Nanotech
// ST4118L0804 :
#define MOTOR_VOLTAGE		7400		// mV
#define MOTOR_MAX_CURRENT	800		// mA (avg=0.8A. should be OK)
#define MOTOR_REDUCED_CURRENT	300		// mA
#define MOTOR_COIL_RESISTANCE	9300		// mOhm
#define MOTOR_EMF		0		// ? PDF says '16 koeien per wei bij 300 U/min'
#define MOTOR_STEPS_PER_360DEG	200





void el7031_unhang(struct controller_block_private *priv)
{
	int		state;
	struct timespec timeout = { 1, 0 };	
	
	//TODO: handle this in a better (non-blocking?) way
	printf("EL7031 not ready!\n");
	
	state = esc_al_state_get(&priv->addr);
	printf(">>>>> state: %d\n", state);

//	if (esc_al_state_set(&priv->addr, ESC_AL_STATE_INIT, &timeout) < 0)
//		printf("Could not go to state init\n");
	
	if (esc_al_state_set(&priv->addr, ESC_AL_STATE_PRE_OPERATIONAL, &timeout) < 0)
		printf("Could not go to state safe operational\n");

	
//	if (state == ESC_AL_STATE_PRE_OPERATIONAL) {
		if (esc_al_state_set(&priv->addr,
		    ESC_AL_STATE_SAFE_OPERATIONAL, &timeout) < 0) {
			printf("Could not go to state safe operational\n");
			state = ESC_AL_STATE_SAFE_OPERATIONAL;
		}

		if (state == ESC_AL_STATE_SAFE_OPERATIONAL) {
			if (esc_al_state_set(&priv->addr,
			    ESC_AL_STATE_OPERATIONAL, &timeout) < 0)
				printf("Could not go to state operational\n");
		}
		state = esc_al_state_get(&priv->addr);
		printf(">>>>>> state nu: %d\n", state);
//	}


}


#if 0
int		z = 0;
#endif


static void calculate(struct controller_block *block)
{
	struct controller_block_private	*priv;
	struct rxpdo_t			*rxpdo;
	struct txpdo_t			*txpdo;
	uint16_t			curr_cnt;
	int32_t				unwound_cnt;
	uint8_t				flags = 0;


	priv = block->private;
	
	rxpdo = (struct rxpdo_t *)priv->tx_buffer;
	txpdo = (struct txpdo_t *)priv->rx_buffer;
	ec_rx_pdo(controller_samplenr);

	if (0) {
		int		cnt;
		uint8_t		*ptr;
		
		// default TxPDO's:
		// 1a00 followed by 1a03
		// 1a00 is 6 bytes in length
		// 1a03 is 2 bytes in length
		
		printf("TxPDO data received:");
		ptr = priv->rx_buffer;
		for (cnt = 0; cnt < 8; cnt++) {
			printf(" %d:%02x", cnt, ptr[cnt]);
		}
		printf("\n");
	}

	//// handle data in TxPDO
	//

	// check 'ready' flag
	if (txpdo->full_status[0] & FULL_STATUS_FLAG_READY) {
		priv->state_ok = true;
		priv->fail_cnt = 0;
		//printf("mot ready\n");
	} else {
		priv->state_ok = false;
		if (INPUT(enable) != true)
			priv->fail_cnt = 0;
		
		if (priv->fail_cnt++ > MAX_FAIL_CNT) {
			// TODO: this is a kludge to detect that the
			// EL7031 has stalled.. can we get out of this
			// state in a non-blocking way??
			el7031_unhang(priv);
			priv->state_ok = true;
			priv->fail_cnt = 0;
		}
	}

	// update our long version of encoder value with the 16-bits in the
	// PDO, wrapping around where needed
	unwound_cnt = priv->unwound_cnt;
	curr_cnt = le16toh(txpdo->counter_val);
	unwound_cnt += curr_cnt - priv->last_cnt;
	
	
	if ((curr_cnt & 32768) ^ (priv->last_cnt & 32768)) {
		// highest bit flipped.. see if we wrapped
		// around zero
		if ((priv->last_cnt < 16384) && (curr_cnt >= 32768 + 16384))
			unwound_cnt -= 65536;
		
		if ((priv->last_cnt >= 32768 + 16384) && (curr_cnt < 16384))
			unwound_cnt += 65536;
	}
	priv->last_cnt = curr_cnt;
	priv->unwound_cnt = unwound_cnt;

	//// construct RxPDO
	//
#if 1
	// 1600 ENC RxPDO-Map Control compact - length: 4 bytes
	rxpdo->enc_ctrl[0] = 0;
	rxpdo->enc_ctrl[1] = 0;
	rxpdo->enc_ctrl[2] = 0;
	rxpdo->enc_ctrl[3] = 0;
#endif
	
	// 1602 STM RxPDO-Map Control - length: 2 bytes
#define FLAG_STM_OUTPUT_ENABLE		(1 << 0)
#define FLAG_STM_RESET			(1 << 1)
#define FLAG_STM_REDUCE_TORQUE		(1 << 2)

	rxpdo->stm_ctrl[0] = (0 * FLAG_STM_RESET) |
	    (0 * FLAG_STM_REDUCE_TORQUE);
	
	if (INPUT(enable) == true)
		flags |= FLAG_STM_OUTPUT_ENABLE;

	if (INPUT(calib) == true) {
		// enable lower current mode during calibration
		flags |= FLAG_STM_REDUCE_TORQUE;
		// reset the current position to '0'
		priv->calib_pos = unwound_cnt;
	}

	rxpdo->stm_ctrl[0] = flags;
	rxpdo->stm_ctrl[1] = 0;

	// 1604 STM RxPDO-Map Velocity - length: 2 bytes
#if 1
	rxpdo->stm_velocity = INPUT(speed);
#else
	if (z++ & 128)
		rxpdo->stm_velocity = 130; //INPUT(speed);
	else
		rxpdo->stm_velocity = -130; //INPUT(speed);
#endif

	// jankende tandwielen motor = speed 4500 .. 6000

	priv->pos_out = unwound_cnt - priv->calib_pos;
	
	ec_tx_pdo(controller_samplenr);
}


static struct controller_block_outterm_list outterms[] = {
	{ "state_ok", CONTROLLER_BLOCK_TERM_BOOL,   offsetof(struct controller_block_private, state_ok) },
	{ "position", CONTROLLER_BLOCK_TERM_SINT32, offsetof(struct controller_block_private, pos_out)  },
	{ NULL },
};

static struct controller_block_interm_list interms[] = {
	{ "enable",    CONTROLLER_BLOCK_TERM_BOOL,   offsetof(struct controller_block_private, enable) },
	{ "calibrate", CONTROLLER_BLOCK_TERM_BOOL,   offsetof(struct controller_block_private, calib)  },
	{ "speed",     CONTROLLER_BLOCK_TERM_SINT16, offsetof(struct controller_block_private, speed)  },
	{ NULL },
};


struct controller_block * block_beckhoff_el7031_create(char *name, va_list ap)
{
	struct controller_block		*block;
	struct ec_dgram_addr		addr;
	struct esc_syncmanager_info	pdo_rx, pdo_tx;
	int				slaves;
	int				i;
	bool				found;
	int				devno;
	int				ret;
		
	struct esc_mailbox		*mb;
	struct esc_syncmanager_info	mb_rd, mb_wr;
	struct canopen_dev		*can_dev;
	struct timespec			timeout = { 1, 0 };
	

	va_arg(ap, char *);
	devno = va_arg(ap, int);
	
	slaves = ec_slave_count();
	found = false;
	for (i = 0; i < slaves; i++) {
		struct ec_dgram_addr devaddr;
		uint32_t vendor, product;

		ec_addr_set_auto_inc_nr(&devaddr, i);
		vendor = esc_esi_vendorid_get(&devaddr);
		product = esc_esi_productcode_get(&devaddr);
		
		printf("scan: vendor=%x, product=%x, i=%d, devno=%d\n",
		    vendor, product, i, devno);
		
		if (vendor == ESC_ESI_VENDORID_BECKHOFF &&
		    product == ESC_ESI_PRODUCTCODE_BECKHOFF_EL7031) {
			printf("found the device at #%d (looking for #%d)\n",
			    i, devno);
			if (!devno || devno == i) {
			//if (!found) {
				printf("found=true :)\n");
				found = true;
				addr = devaddr;
			}
		}
	}
	if (!found) {
		printf("el7031: Device not found\n");
		return NULL;
	}
	
	block = controller_block_alloc("beckhoff_el7031", name,
	    sizeof(struct controller_block_private));
	if (block == NULL)
		return NULL;

	block->private->addr = addr;
	esc_init(&addr);

	// This is a Can-open Ethercat device (CoE). It offers registers (with
	// sub-indexes) that can be read from / written to. An overview of
	// all CoE registers it offers can be found here:
	// http://infosys.beckhoff.com/content/1034/el70x1/html/bt_el7031_objectdescription.htm
	// (or in a less readable form in the device's XML file)
	//
	// Before we can read/write to/from these CoE registers, we'll have to
	// setup the CoE mailbox. Before the device can be switched to
	// 'safe operational' or 'operational' mode, we'll also have to setup
	// the device's RX/TW PDO mailboxes.
	//
	// The specification of all these mailboxes can be found in the XML
	// file of the device. For this device, the XML snippet defining the
	// mailboxes is the following :
	//  <Sm MinSize="64" MaxSize="128" DefaultSize="128"
	//   StartAddress="#x1000" ControlByte="#x26" Enable="1">MBoxOut</Sm>
	//  <Sm MinSize="64" MaxSize="128" DefaultSize="128"
	//   StartAddress="#x1080" ControlByte="#x22" Enable="1">MBoxIn</Sm>
	//  <Sm DefaultSize="8" StartAddress="#x1100" ControlByte="#x24"
	//   Enable="1">Outputs</Sm>
	//  <Sm DefaultSize="8" StartAddress="#x1180" ControlByte="#x20"
	//   Enable="1">Inputs</Sm> 
	//
	// Every mailbox has an accompanying 'sync manager', with it's own
	// sync manager number. The sync manager number corrosponds to the
	// order in which the <sm>-entries are listed in the XML file.
	// for example: 'MBoxOut' is listed first, so it uses sync manager 0

	// setup RX/TX pdo's
	pdo_tx.start = 0x1100;
	pdo_tx.len = 8;
	pdo_tx.sm = 2;

	pdo_rx.start = 0x1180;
	pdo_rx.len = 8;
	pdo_rx.sm = 3;
	
	esc_pdo_tx_set(&addr, &pdo_tx, &block->private->tx_buffer, false);
	esc_pdo_rx_set(&addr, &pdo_rx, &block->private->rx_buffer);

	if (esc_al_state_set(&addr, ESC_AL_STATE_INIT, &timeout) < 0)
		printf("Could not go to state init\n");

	/* mailbox write (master->slave) @0x1000 */
	mb_wr.start = 0x1000;
	mb_wr.len = 128;
	mb_wr.sm = 0;

	/* mailbox read (slave->master) @0x1080 */
	mb_rd.start = 0x1080;
	mb_rd.len = 128;
	mb_rd.sm = 1;
	
	mb = esc_mailbox_create(&addr, &mb_rd, &mb_wr);
	can_dev = esc_coe_create(mb);
	block->private->can_dev = can_dev;

	if (esc_al_state_set(&addr, ESC_AL_STATE_PRE_OPERATIONAL, &timeout) < 0)
		printf("Could not go to state pre operational\n");





if (1) {
	uint16_t		volt, curr, full_steps, accel, decel;
	
	ret = canopen_read_param(can_dev, REG_NOMINAL_VOLTAGE, &volt, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to read REG_NOMINAL_VOLTAGE");

	printf("nominal voltage: %dmV\n", le16toh(volt));
	
	volt = MOTOR_VOLTAGE;
	ret = canopen_write_param(can_dev, REG_NOMINAL_VOLTAGE, &volt, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to write REG_NOMINAL_VOLTAGE");

	ret = canopen_read_param(can_dev, REG_NOMINAL_VOLTAGE, &volt, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to read REG_NOMINAL_VOLTAGE");
	
	printf("nominal voltage: %dmV\n", le16toh(volt));

	curr = htole16(MOTOR_MAX_CURRENT);
	ret = canopen_write_param(can_dev, REG_MAX_CURRENT, &curr, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to write REG_MAX_CURRENT");
	
	curr = htole16(MOTOR_REDUCED_CURRENT);
	curr = htole16(MOTOR_MAX_CURRENT);
	ret = canopen_write_param(can_dev, REG_REDUCED_CURRENT, &curr, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to write REG_REDUCED_CURRENT");
	
	printf("max current: %dmA\n", le16toh(curr));

if (0) {
	uint32_t		p;
	
	p = htole32(0);
	ret = canopen_write_param(can_dev, REG_CALIB_POS, &p, 4);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to write REG_CALIB_POS");
	
	printf("max current: %dmA\n", le16toh(curr));
}

	ret = canopen_read_param(can_dev, REG_MAX_CURRENT, &curr, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to read REG_MAX_CURRENT");
	
	printf("max current: %dmA\n", le16toh(curr));

	ret = canopen_read_param(can_dev, REG_MOT_FULL_STEPS, &full_steps, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to read REG_MOT_FULL_STEPS");
	
	printf("motor full steps: %d steps/revolution\n",
	    le16toh(full_steps));


	// Acceleration / deceleration
	accel = htole16(1);
	ret = canopen_write_param(can_dev, REG_ACCEL_POS, &accel, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to write REG_ACCEL_POS");

	ret = canopen_write_param(can_dev, REG_ACCEL_NEG, &accel, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to write REG_ACCEL_NEG");

	decel = htole16(1);
	ret = canopen_write_param(can_dev, REG_DECEL_POS, &decel, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to write REG_DECEL_POS");

	ret = canopen_write_param(can_dev, REG_DECEL_NEG, &decel, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to write REG_DECEL_NEG");
	
	printf("max pos/neg accel=%d decel=%d\n", accel, decel);

}

#if 0

if (1) {
	uint16_t		stat;
	int			cnt;
	uint8_t			booltje, opmode;
	
	ret = canopen_read_param(can_dev, REG_STATUS_WORD, &stat, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to read REG_STATUS_WORD");

	printf("status word: %x\n", stat);

	for (cnt = 1; cnt < 11; cnt++) {
		ret = canopen_read_param(can_dev, 0xa010, cnt, &booltje, 1);
		if (ret != 1)
			log_send(LOG_T_ERROR, "failed to read 0xa010:%d", cnt);
		
		printf("a010:%x = %x\n", cnt, booltje);
	}

	// get actual op-mode
	ret = canopen_read_param(can_dev, 0xa010, 0x11, &opmode, 1);
	if (ret != 1)
		log_send(LOG_T_ERROR, "failed to read 0xa010:0x11", cnt);
	
	printf("actual opmode: %x\n", opmode);
}

if (1) {
	uint32_t		pos;
	
	ret = canopen_read_param(can_dev, REG_ACTUAL_POS, &pos, 4);
	if (ret != 4)
		log_send(LOG_T_ERROR, "failed to read REG_ACTUAL_POS");
	
	printf("actual pos: %d\n", pos);
}

if (1) {
	uint16_t		arg = htole16(0x8000);
	
#define REG_STM_REQUEST		0xfb00, 1

	ret = canopen_write_param(can_dev, REG_STM_REQUEST, &arg, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to write REG_STM_REQUEST");
}

if (1) {
	uint8_t			reset;

	reset = 0;
	ret = canopen_read_param(can_dev, REG_OUTP_RESET, &reset, 1);
	if (ret != 1)
		log_send(LOG_T_ERROR, "failed to read REG_OUTP_RESET: %d", ret);

	reset = 0;
	ret = canopen_read_param(can_dev, REG_OUTP_ENABLE, &reset, 1);
	if (ret != 1)
		log_send(LOG_T_ERROR, "failed to read REG_OUTP_RESET: %d", ret);
}


if (1) {
	uint16_t		stat;
	int			cnt;
	uint8_t			booltje;
	
	ret = canopen_read_param(can_dev, REG_STATUS_WORD, &stat, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to read REG_STATUS_WORD");

	for (cnt = 1; cnt < 11; cnt++) {
		ret = canopen_read_param(can_dev, 0xa010, cnt, &booltje, 1);
		if (ret != 1)
			log_send(LOG_T_ERROR, "failed to read 0xa010:%d", cnt);
		
		printf("a010:%x = %x\n", cnt, booltje);
	}
	
	printf("status word: %x\n", stat);
}

if (1) {
	int			cnt;
	uint8_t			val;
	
	for (cnt = 1; cnt <= 7; cnt++) {
		ret = canopen_read_param(can_dev, 0x6010, cnt, &val, 1);
		if (ret != 1)
			log_send(LOG_T_ERROR, "failed to read 0x6010:%d", cnt);
		
		printf("6010:%x = %x\n", cnt, val);
	}
	for (cnt = 0xc; cnt <= 0xe; cnt++) {
		ret = canopen_read_param(can_dev, 0x6010, cnt, &val, 1);
		if (ret != 1)
			log_send(LOG_T_ERROR, "failed to read 0x6010:%d", cnt);
		
		printf("6010:%x = %x\n", cnt, val);
	}
}

if (1) {
	int			cnt;
	uint8_t			val;
	
	for (cnt = 1; cnt <= 7; cnt++) {
		ret = canopen_read_param(can_dev, 0x6020, cnt, &val, 1);
		if (ret != 1)
			log_send(LOG_T_ERROR, "failed to read 0x6020:%d", cnt);
		
		printf("6020:%x = %x\n", cnt, val);
	}
	for (cnt = 0x11; cnt <= 0x11; cnt++) {
		ret = canopen_read_param(can_dev, 0x6020, cnt, &val, 1);
		if (ret != 1)
			log_send(LOG_T_ERROR, "failed to read 0x6020:%d", cnt);
		
		printf("6020:%x = %x\n", cnt, val);
	}
	for (cnt = 0x21; cnt <= 0x22; cnt++) {
		ret = canopen_read_param(can_dev, 0x6020, cnt, &val, 1);
		if (ret != 1)
			log_send(LOG_T_ERROR, "failed to read 0x6020:%d", cnt);
		
		printf("6020:%x = %x\n", cnt, val);
	}
}

#endif

	// by default the RxPDO (PLC -> motor controller) is assigned to:
	// 0x1600, 0x1602, 0x1604
	// this can be changed by writing to reg 0x1c12:1..3 (RxPDO assign)

if (1) {
	uint8_t			num_assigned;
	uint16_t		pdo_index;
	
	num_assigned = 0;
	ret = canopen_write_param(can_dev, 0x1c12, 0, &num_assigned, 1);
	if (ret != 1)
		log_send(LOG_T_ERROR, "failed to write RxPDO index reg 0");

	// assign reg 1600 (STM RxPDO-Map Control) as first part of the PDO
	pdo_index = htole16(0x1600);
	ret = canopen_write_param(can_dev, 0x1c12, 1, &pdo_index, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to write RxPDO index reg 1");

	// assign reg 1602 (STM RxPDO-Map Control) as first part of the PDO
	pdo_index = htole16(0x1602);
	ret = canopen_write_param(can_dev, 0x1c12, 2, &pdo_index, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to write RxPDO index reg 1");

	// assign reg 1604 (STM RxPDO-Map Velocity) as second part of the PDO
	pdo_index = htole16(0x1604);
	ret = canopen_write_param(can_dev, 0x1c12, 3, &pdo_index, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to write RxPDO index reg 2");

	// .. and enable just this part of the PDO
	num_assigned = 3;
	ret = canopen_write_param(can_dev, 0x1c12, 0, &num_assigned, 1);
	if (ret != 1)
		log_send(LOG_T_ERROR, "failed to write RxPDO index reg 0 to 1");

}

if (0) {
	// try to request RxPDO 1602, 1603
	uint8_t			num_assigned;
	uint16_t		pdo_index;
	
	num_assigned = 0;
	ret = canopen_write_param(can_dev, 0x1c12, 0, &num_assigned, 1);
	if (ret != 1)
		log_send(LOG_T_ERROR, "failed to write RxPDO index reg 0");

	// assign reg 1602 (STM RxPDO-Map Control) as first part of the PDO
	pdo_index = htole16(0x1602);
	ret = canopen_write_param(can_dev, 0x1c12, 1, &pdo_index, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to write RxPDO index reg 1");

	// assign reg 1603 (STM RxPDO-Map Control) as second part of the PDO
	pdo_index = htole16(0x1603);
	ret = canopen_write_param(can_dev, 0x1c12, 2, &pdo_index, 2);
	if (ret != 2)
		log_send(LOG_T_ERROR, "failed to write RxPDO index reg 2");

	// .. and enable this part of the PDO
	num_assigned = 1;
	ret = canopen_write_param(can_dev, 0x1c12, 0, &num_assigned, 1);
	if (ret != 1)
		log_send(LOG_T_ERROR, "failed to write RxPDO index reg 0 to 2");

}

	// show RxPDO assignments (motor controller -> PLC)
if (1) {
	uint8_t			num_assigned = 0;
	uint16_t		pdo_index;
	int			cnt;
	
	ret = canopen_read_param(can_dev, 0x1c12, 0, &num_assigned, 1);
	if (ret != 1)
		log_send(LOG_T_ERROR, "failed to write RxPDO index reg 0");
	printf("RxPDO's assigned: %d\n", num_assigned);
	
	for (cnt = 1; cnt <=3; cnt++) {
		ret = canopen_read_param(can_dev, 0x1c12, cnt, &pdo_index, 2);
		if (ret != 2)
			log_send(LOG_T_ERROR, "failed to read RxPDO index reg %d", cnt);
		
		printf("RxPDO [%d] index=%x\n", cnt, pdo_index);
	}
}
	usleep(200 * 1000);


	// show TxPDO assignments (motor controller -> PLC)
if (1) {
	uint8_t			num_assigned = 0;
	uint16_t		pdo_index;
	int			cnt;
	
	ret = canopen_read_param(can_dev, 0x1c13, 0, &num_assigned, 1);
	if (ret != 1)
		log_send(LOG_T_ERROR, "failed to write TxPDO index reg 0");
	printf("TxPDO's assigned: %d\n", num_assigned);
	
	for (cnt = 1; cnt <=2; cnt++) {
		ret = canopen_read_param(can_dev, 0x1c13, cnt, &pdo_index, 2);
		if (ret != 2)
			log_send(LOG_T_ERROR, "failed to write TxPDO index reg %d", cnt);
		
		printf("TxPDO [%d] index=%x\n", cnt, pdo_index);
	}
}




	if (esc_al_state_set(&addr, ESC_AL_STATE_SAFE_OPERATIONAL, &timeout) < 0)
		printf("Could not go to state safe operational\n");


	if (esc_al_state_set(&addr, ESC_AL_STATE_OPERATIONAL, &timeout) < 0)
		printf("Could not go to state operational\n");

//	exit(0);


	if (controller_block_outterm_list_init(block, outterms))
		goto err_outputs;	
	if (controller_block_interm_list_init(block, interms))
		goto err_inputs;	

	block->private->state_ok = false;
	block->private->unwound_cnt = 0;
	block->private->last_cnt = 0;
	block->private->calib_pos = 0;
	
	block->calculate = calculate;
	block->param_get = NULL;
	block->param_set = NULL;

	controller_block_add(block);
	return block;

err_outputs:
err_inputs:
	controller_block_free(block);
	return NULL;
}