]> defiant.homedns.org Git - ros_wild_thumper.git/blob - avr/motor_ctrl/main.c
Merge branch 'master' of ssh://vontaene/home/erik_alt/git/ros_wild_thumper
[ros_wild_thumper.git] / avr / motor_ctrl / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <math.h>
5 #include <avr/io.h>
6 #include <avr/interrupt.h>
7 #include <avr/sleep.h>
8 #include "uart.h"
9
10 /*
11  * I2C Register Map (8 Bit)
12  * 0x00 Register select
13  * 0x01 Motor 1 PWM MSB
14  * 0x02 Motor 1 PWM LSB
15  * 0x03 Motor 2 PWM MSB
16  * 0x04 Motor 2 PWM LSB
17  * 0x05 Motor 3 PWM MSB
18  * 0x06 Motor 3 PWM LSB
19  * 0x07 Motor 4 PWM MSB
20  * 0x08 Motor 4 PWM LSB
21  * free
22  * 0x10 Hall 1 MSB
23  * 0x11 Hall 1 LSB
24  * 0x12 Hall 2 MSB
25  * 0x13 Hall 2 LSB
26  * 0x14 Hall 3 MSB
27  * 0x15 Hall 3 LSB
28  * 0x16 Hall 4 MSB
29  * 0x17 Hall 4 LSB
30  * free
31  * 0x20 Motor 1 speed wish MSB
32  * 0x21 Motor 1 speed wish LSB
33  * 0x22 Motor 2 speed wish MSB
34  * 0x23 Motor 2 speed wish LSB
35  * 0x24 Motor 3 speed wish MSB
36  * 0x25 Motor 3 speed wish LSB
37  * 0x26 Motor 4 speed wish MSB
38  * 0x27 Motor 4 speed wish LSB
39  * 0x28 Left speed wish (m/s) MSB
40  * 0x29 Left speed wish (m/s)
41  * 0x2A Left speed wish (m/s)
42  * 0x2B Left speed wish (m/s) LSB
43  * 0x2C Right speed wish (m/s) MSB
44  * 0x2D Right speed wish (m/s)
45  * 0x2E Right speed wish (m/s)
46  * 0x2F Right speed wish (m/s) LSB
47  * 0x30 Motor 1 speed MSB
48  * 0x31 Motor 1 speed LSB
49  * 0x32 Motor 2 speed MSB
50  * 0x33 Motor 2 speed LSB
51  * 0x34 Motor 3 speed MSB
52  * 0x35 Motor 3 speed LSB
53  * 0x36 Motor 4 speed MSB
54  * 0x37 Motor 4 speed LSB
55  * 0x38 Speed (m/s) MSB
56  * 0x39 Speed (m/s)
57  * 0x3A Speed (m/s)
58  * 0x3B Speed (m/s) LSB
59  * 0x3C Angle (rad/s) MSB
60  * 0x3D Angle (rad/s)
61  * 0x3E Angle (rad/s)
62  * 0x3F Angle (rad/s) LSB
63  * 0x40 Position x (m) MSB
64  * 0x41 Position x (m)
65  * 0x42 Position x (m)
66  * 0x43 Position x (m) LSB
67  * 0x44 Position y (m) MSB
68  * 0x45 Position y (m)
69  * 0x46 Position y (m)
70  * 0x47 Position y (m) LSB
71  * 0x48 Position angle MSB
72  * 0x49 Position angle
73  * 0x4A Position angle
74  * 0x4B Position angle LSB
75  * free
76  * 0x50 speed wish (m/s) MSB
77  * 0x51 speed wish (m/s)
78  * 0x52 speed wish (m/s)
79  * 0x53 speed wish (m/s) LSB
80  * 0x54 angle wish (rad/s) MSB
81  * 0x55 angle wish (rad/s)
82  * 0x56 angle wish (rad/s)
83  * 0x57 angle wish (rad/s) LSB
84  * free
85  * 0x90 Motor 1 switch
86  * 0x91 Motor 2 switch
87  * 0x92 Motor 3 switch
88  * 0x93 Motor 4 switch
89  * 0x94 Front Handicap
90  * 0x95 Aft Handicap
91  * free
92  * 0xA0 Reset reason
93  * 0xA1 Error status
94  * 0xA2 count test
95  * free
96  * 0xff Bootloader
97  */
98
99
100 #define TWI_ACK         TWCR = (1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
101 #define TWI_RESET       TWCR &= ~((1 << TWSTO) | (1 << TWEN)); TWI_ACK
102 #define TWI_NAK         TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
103
104 #define KP 0.061
105 #define KI 0.12
106 #define KD 0.0
107 #define PID_T 0.01
108 // wheel diameter=12cm, encoder=48cpr, gear ratio=1:47
109 // STEP_PER_M = 48*47/(d*pi)
110 // Left real diameter: 0.12808, Right real diameter: 0.121
111 #define STEP_PER_M 5770.8
112 #define STEP_PER_M_LEFT (STEP_PER_M)
113 #define STEP_PER_M_RIGHT (STEP_PER_M)
114 #define WHEEL_DIST 0.39912 // Measured: 0.252
115 #define PWM_BREAK INT16_MIN
116
117 enum mode {
118         MOTOR_MANUAL,
119         MOTOR_PID
120 };
121
122 typedef union {
123         float f;
124         uint32_t i;
125 } ufloat_t;
126
127 static volatile struct {
128         float speed;
129         float angle;
130         uint8_t bUpdate;
131 } cmd_vel = {0, 0, 0};
132
133 static volatile uint8_t ireg=0;
134 static volatile uint8_t bootloader=0;
135 static volatile int16_t motor1=0; // -255..+255
136 static volatile int16_t motor2=0;
137 static volatile int16_t motor3=0;
138 static volatile int16_t motor4=0;
139 static volatile int16_t pos1=0; // step
140 static volatile int16_t pos2=0;
141 static volatile int16_t pos3=0;
142 static volatile int16_t pos4=0;
143 static volatile enum mode motor1_mode=MOTOR_MANUAL;
144 static volatile enum mode motor2_mode=MOTOR_MANUAL;
145 static volatile enum mode motor3_mode=MOTOR_MANUAL;
146 static volatile enum mode motor4_mode=MOTOR_MANUAL;
147 static volatile uint8_t motor1_switch=1;
148 static volatile uint8_t motor2_switch=1;
149 static volatile uint8_t motor3_switch=0;
150 static volatile uint8_t motor4_switch=0;
151 static volatile int16_t speed1_wish=0; // step/s
152 static volatile int16_t speed2_wish=0;
153 static volatile int16_t speed3_wish=0;
154 static volatile int16_t speed4_wish=0;
155 static volatile int16_t speed1_wish_old=0;
156 static volatile int16_t speed2_wish_old=0;
157 static volatile int16_t speed3_wish_old=0;
158 static volatile int16_t speed4_wish_old=0;
159 static volatile uint8_t run_update=0;
160 static volatile int16_t speed1=0; // step/s
161 static volatile int16_t speed2=0;
162 static volatile int16_t speed3=0;
163 static volatile int16_t speed4=0;
164 static volatile ufloat_t pos_x={0.0};
165 static volatile ufloat_t pos_y={0.0};
166 static volatile ufloat_t angle={0.0};
167 static volatile float cur_speed_lin=0;
168 static volatile float cur_speed_rot=0;
169 static volatile uint8_t count_test=0;
170 static volatile uint8_t front_handicap=0;
171 static volatile uint8_t aft_handicap=0;
172 static volatile uint8_t error_state=0;
173
174 ISR(TWI_vect)
175 {
176         static uint8_t tmp=0;
177         static int16_t tmp16=0;
178         static ufloat_t tmp_speed;
179         static ufloat_t tmp_angle;
180
181         switch (TWSR & 0xF8)
182         {
183                 case 0x60: // start write
184                         TWI_ACK;
185                         ireg = 0;
186                         break;
187                 case 0x80: // write
188                         switch(ireg) {
189                                 case 0x00: // register select
190                                         ireg = TWDR;
191                                         ireg--; // because we do ireg++ below
192                                         TWI_ACK;
193                                         break;
194                                 case 0x01: // Motor 1 MSB
195                                         tmp = TWDR;
196                                         TWI_ACK;
197                                         break;
198                                 case 0x02: // Motor 1 LSB
199                                         motor1 = tmp<<8 | TWDR;
200                                         motor1_mode = MOTOR_MANUAL;
201                                         TWI_ACK;
202                                         break;
203                                 case 0x03: // Motor 2 MSB
204                                         tmp = TWDR;
205                                         TWI_ACK;
206                                         break;
207                                 case 0x04: // Motor 2 LSB
208                                         motor2 = tmp<<8 | TWDR;
209                                         motor2_mode = MOTOR_MANUAL;
210                                         TWI_ACK;
211                                         break;
212                                 case 0x05: // Motor 3 MSB
213                                         tmp = TWDR;
214                                         TWI_ACK;
215                                         break;
216                                 case 0x06: // Motor 3 LSB
217                                         motor3 = tmp<<8 | TWDR;
218                                         motor3_mode = MOTOR_MANUAL;
219                                         TWI_ACK;
220                                         break;
221                                 case 0x07: // Motor 4 MSB
222                                         tmp = TWDR;
223                                         TWI_ACK;
224                                         break;
225                                 case 0x08: // Motor 4 LSB
226                                         motor4 = tmp<<8 | TWDR;
227                                         motor4_mode = MOTOR_MANUAL;
228                                         TWI_ACK;
229                                         break;
230                                 case 0x20: // Motor 1 speed wish MSB
231                                         tmp = TWDR;
232                                         TWI_ACK;
233                                         break;
234                                 case 0x21: // Motor 1 speed wish LSB
235                                         speed1_wish = tmp<<8 | TWDR;
236                                         motor1_mode = MOTOR_PID;
237                                         TWI_ACK;
238                                         break;
239                                 case 0x22: // Motor 2 speed wish MSB
240                                         tmp = TWDR;
241                                         TWI_ACK;
242                                         break;
243                                 case 0x23: // Motor 2 speed wish LSB
244                                         speed2_wish = tmp<<8 | TWDR;
245                                         motor2_mode = MOTOR_PID;
246                                         TWI_ACK;
247                                         break;
248                                 case 0x24: // Motor 3 speed wish MSB
249                                         tmp = TWDR;
250                                         TWI_ACK;
251                                         break;
252                                 case 0x25: // Motor 3 speed wish LSB
253                                         speed3_wish = tmp<<8 | TWDR;
254                                         motor3_mode = MOTOR_PID;
255                                         TWI_ACK;
256                                         break;
257                                 case 0x26: // Motor 4 speed wish MSB
258                                         tmp = TWDR;
259                                         TWI_ACK;
260                                         break;
261                                 case 0x27: // Motor 4 speed wish LSB
262                                         speed4_wish = tmp<<8 | TWDR;
263                                         motor4_mode = MOTOR_PID;
264                                         TWI_ACK;
265                                         break;
266                                 case 0x28: // Left speed wish MSB
267                                         tmp_speed.i = TWDR;
268                                         TWI_ACK;
269                                         break;
270                                 case 0x29: // Left speed wish
271                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
272                                         TWI_ACK;
273                                         break;
274                                 case 0x2A: // Left speed wish
275                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
276                                         TWI_ACK;
277                                         break;
278                                 case 0x2B: // Left speed wish LSB
279                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
280                                         speed1_wish = tmp_speed.f*STEP_PER_M_LEFT;
281                                         speed2_wish = tmp_speed.f*STEP_PER_M_LEFT;
282                                         motor1_mode = MOTOR_PID;
283                                         motor2_mode = MOTOR_PID;
284                                         TWI_ACK;
285                                         break;
286                                 case 0x2C: // Right speed wish MSB
287                                         tmp_speed.i = TWDR;
288                                         TWI_ACK;
289                                         break;
290                                 case 0x2D: // Right speed wish
291                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
292                                         TWI_ACK;
293                                         break;
294                                 case 0x2E: // Right speed wish
295                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
296                                         TWI_ACK;
297                                         break;
298                                 case 0x2F: // Right speed wish LSB
299                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
300                                         speed1_wish = tmp_speed.f*STEP_PER_M_RIGHT;
301                                         speed2_wish = tmp_speed.f*STEP_PER_M_RIGHT;
302                                         motor1_mode = MOTOR_PID;
303                                         motor2_mode = MOTOR_PID;
304                                         TWI_ACK;
305                                         break;
306                                 case 0x50: // speed wish MSB
307                                         tmp_speed.i = TWDR;
308                                         TWI_ACK;
309                                         break;
310                                 case 0x51: // speed wish
311                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
312                                         TWI_ACK;
313                                         break;
314                                 case 0x52: // speed wish
315                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
316                                         TWI_ACK;
317                                         break;
318                                 case 0x53: // speed wish LSB
319                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
320                                         cmd_vel.speed = tmp_speed.f;
321                                         TWI_ACK;
322                                         break;
323                                 case 0x54: // angle wish MSB
324                                         tmp_angle.i = TWDR;
325                                         TWI_ACK;
326                                         break;
327                                 case 0x55: // angle wish
328                                         tmp_angle.i = tmp_angle.i << 8 | TWDR;
329                                         TWI_ACK;
330                                         break;
331                                 case 0x56: // angle wish
332                                         tmp_angle.i = tmp_angle.i << 8 | TWDR;
333                                         TWI_ACK;
334                                         break;
335                                 case 0x57: // angle wish LSB
336                                         tmp_angle.i = tmp_angle.i << 8 | TWDR;
337                                         cmd_vel.angle = tmp_angle.f;
338                                         cmd_vel.bUpdate = 1;
339                                         TWI_ACK;
340                                         break;
341                                 case 0x90: // Motor 1 switch
342                                         motor1_switch = TWDR;
343                                         TWI_ACK;
344                                         break;
345                                 case 0x91: // Motor 2 switch
346                                         motor2_switch = TWDR;
347                                         TWI_ACK;
348                                         break;
349                                 case 0x92: // Motor 3 switch
350                                         motor3_switch = TWDR;
351                                         TWI_ACK;
352                                         break;
353                                 case 0x93: // Motor 4 switch
354                                         motor4_switch = TWDR;
355                                         TWI_ACK;
356                                         break;
357                                 case 0x94: // Front Handicap
358                                         front_handicap = TWDR;
359                                         cmd_vel.bUpdate = 1;
360                                         TWI_ACK;
361                                         break;
362                                 case 0x95: // Aft Handicap
363                                         aft_handicap = TWDR;
364                                         cmd_vel.bUpdate = 1;
365                                         TWI_ACK;
366                                         break;
367                                 case 0xff: // bootloader
368                                         bootloader = TWDR;
369                                 default:
370                                         TWI_NAK;
371                         }
372                         ireg++;
373                         break;
374                 case 0xA8: // start read
375                 case 0xB8: // read
376                         switch(ireg) {
377                                 case 0x02: // Motor 1 PWM
378                                         TWDR = OCR1A;
379                                         TWI_ACK;
380                                         break;
381                                 case 0x03: // Dummy to allow continous read
382                                         TWDR = 0;
383                                         TWI_ACK;
384                                         break;
385                                 case 0x04: // Motor 2 PWM
386                                         TWDR = OCR1B;
387                                         TWI_ACK;
388                                         break;
389                                 case 0x05: // Dummy to allow continous read
390                                         TWDR = 0;
391                                         TWI_ACK;
392                                         break;
393                                 case 0x06: // Motor 3 PWM
394                                         TWDR = OCR2;
395                                         TWI_ACK;
396                                         break;
397                                 case 0x07: // Dummy to allow continous read
398                                         TWDR = 0;
399                                         TWI_ACK;
400                                         break;
401                                 case 0x08: // Motor 4 PWM
402                                         TWDR = OCR0;
403                                         TWI_ACK;
404                                         break;
405                                 case 0x09: // Dummy to allow continous read
406                                         TWDR = 0;
407                                         TWI_ACK;
408                                         break;
409                                 case 0x10: // Hall 1 MSB
410                                         tmp16 = pos1;
411                                         TWDR = tmp16>>8;
412                                         TWI_ACK;
413                                         break;
414                                 case 0x11: // Hall 1 LSB
415                                         TWDR = tmp16;
416                                         TWI_ACK;
417                                         break;
418                                 case 0x12: // Hall 2 MSB
419                                         tmp16 = pos2;
420                                         TWDR = tmp16>>8;
421                                         TWI_ACK;
422                                         break;
423                                 case 0x13: // Hall 2 LSB
424                                         TWDR = tmp16;
425                                         TWI_ACK;
426                                         break;
427                                 case 0x14: // Hall 3 MSB
428                                         tmp16 = pos3;
429                                         TWDR = tmp16>>8;
430                                         TWI_ACK;
431                                         break;
432                                 case 0x15: // Hall 3 LSB
433                                         TWDR = tmp16;
434                                         TWI_ACK;
435                                         break;
436                                 case 0x16: // Hall 4 MSB
437                                         tmp16 = pos4;
438                                         TWDR = tmp16>>8;
439                                         TWI_ACK;
440                                         break;
441                                 case 0x17: // Hall 4 LSB
442                                         TWDR = tmp16;
443                                         TWI_ACK;
444                                         break;
445                                 case 0x20: // Motor 1 speed wish MSB
446                                         TWDR = speed1_wish>>8;
447                                         TWI_ACK;
448                                         break;
449                                 case 0x21: // Motor 1 speed wish LSB
450                                         TWDR = speed1_wish;
451                                         TWI_ACK;
452                                         break;
453                                 case 0x22: // Motor 2 speed wish MSB
454                                         TWDR = speed2_wish>>8;
455                                         TWI_ACK;
456                                         break;
457                                 case 0x23: // Motor 2 speed wish LSB
458                                         TWDR = speed2_wish;
459                                         TWI_ACK;
460                                         break;
461                                 case 0x24: // Motor 3 speed wish MSB
462                                         TWDR = speed3_wish>>8;
463                                         TWI_ACK;
464                                         break;
465                                 case 0x25: // Motor 3 speed wish LSB
466                                         TWDR = speed3_wish;
467                                         TWI_ACK;
468                                         break;
469                                 case 0x26: // Motor 4 speed wish MSB
470                                         TWDR = speed4_wish>>8;
471                                         TWI_ACK;
472                                         break;
473                                 case 0x27: // Motor 4 speed wish LSB
474                                         TWDR = speed4_wish;
475                                         TWI_ACK;
476                                         break;
477                                 case 0x30: // Motor 1 speed MSB
478                                         TWDR = speed1>>8;
479                                         TWI_ACK;
480                                         break;
481                                 case 0x31: // Motor 1 speed LSB
482                                         TWDR = speed1;
483                                         TWI_ACK;
484                                         break;
485                                 case 0x32: // Motor 2 speed MSB
486                                         TWDR = speed2>>8;
487                                         TWI_ACK;
488                                         break;
489                                 case 0x33: // Motor 2 speed LSB
490                                         TWDR = speed2;
491                                         TWI_ACK;
492                                         break;
493                                 case 0x34: // Motor 3 speed MSB
494                                         TWDR = speed3>>8;
495                                         TWI_ACK;
496                                         break;
497                                 case 0x35: // Motor 3 speed LSB
498                                         TWDR = speed3;
499                                         TWI_ACK;
500                                         break;
501                                 case 0x36: // Motor 4 speed MSB
502                                         TWDR = speed4>>8;
503                                         TWI_ACK;
504                                         break;
505                                 case 0x37: // Motor 4 speed LSB
506                                         TWDR = speed4;
507                                         TWI_ACK;
508                                         break;
509                                 case 0x38: // speed MSB
510                                         tmp_speed.f = cur_speed_lin;
511                                         TWDR = tmp_speed.i>>24;
512                                         TWI_ACK;
513                                         break;
514                                 case 0x39: // speed
515                                         TWDR = tmp_speed.i>>16;
516                                         TWI_ACK;
517                                         break;
518                                 case 0x3A: // speed
519                                         TWDR = tmp_speed.i>>8;
520                                         TWI_ACK;
521                                         break;
522                                 case 0x3B: // speed LSB
523                                         TWDR = tmp_speed.i;
524                                         TWI_ACK;
525                                         break;
526                                 case 0x3C: // angle MSB
527                                         tmp_angle.f = cur_speed_rot;
528                                         TWDR = tmp_angle.i>>24;
529                                         TWI_ACK;
530                                         break;
531                                 case 0x3D: // angle
532                                         TWDR = tmp_angle.i>>16;
533                                         TWI_ACK;
534                                         break;
535                                 case 0x3E: // angle
536                                         TWDR = tmp_angle.i>>8;
537                                         TWI_ACK;
538                                         break;
539                                 case 0x3F: // angle LSB
540                                         TWDR = angle.i;
541                                         TWI_ACK;
542                                         break;
543                                 case 0x40: // Position x MSB
544                                         TWDR = pos_x.i>>24;
545                                         TWI_ACK;
546                                         break;
547                                 case 0x41: // Position x
548                                         TWDR = pos_x.i>>16;
549                                         TWI_ACK;
550                                         break;
551                                 case 0x42: // Position x
552                                         TWDR = pos_x.i>>8;
553                                         TWI_ACK;
554                                         break;
555                                 case 0x43: // Position x LSB
556                                         TWDR = pos_x.i;
557                                         TWI_ACK;
558                                         break;
559                                 case 0x44: // Position y MSB
560                                         TWDR = pos_y.i>>24;
561                                         TWI_ACK;
562                                         break;
563                                 case 0x45: // Position y
564                                         TWDR = pos_y.i>>16;
565                                         TWI_ACK;
566                                         break;
567                                 case 0x46: // Position y
568                                         TWDR = pos_y.i>>8;
569                                         TWI_ACK;
570                                         break;
571                                 case 0x47: // Position y LSB
572                                         TWDR = pos_y.i;
573                                         TWI_ACK;
574                                         break;
575                                 case 0x48: // Position angle MSB
576                                         TWDR = angle.i>>24;
577                                         TWI_ACK;
578                                         break;
579                                 case 0x49: // Position angle
580                                         TWDR = angle.i>>16;
581                                         TWI_ACK;
582                                         break;
583                                 case 0x4A: // Position angle
584                                         TWDR = angle.i>>8;
585                                         TWI_ACK;
586                                         break;
587                                 case 0x4B: // Position angle LSB
588                                         TWDR = angle.i;
589                                         TWI_ACK;
590                                         break;
591                                 case 0xA0: // Reset reason
592                                         TWDR = MCUCSR & 0x0f;
593                                         MCUCSR = 0x0;
594                                         TWI_ACK;
595                                         break;
596                                 case 0xA1: // Error status
597                                         TWDR = error_state;
598                                         TWI_ACK;
599                                         break;
600                                 case 0xA2: // count test
601                                         TWDR = count_test;
602                                         TWI_ACK;
603                                 default:
604                                         TWDR = 0;
605                                         TWI_NAK;
606                         }
607                         ireg++;
608                         break;
609                 default:
610                         TWI_RESET;
611         }
612 }
613
614
615 static void update_hall1(void) {
616         unsigned char status = (PINA >> 0) & 0x3;
617         static unsigned char oldstatus=0;
618         unsigned char diff, new;
619
620         new = 0;
621         if (status & 0x1)
622                 new = 0x3;
623         if (status & 0x2)
624                 new ^= 0x1;                                     // convert gray to binary
625         diff = oldstatus - new;                         // difference last - new
626         if (diff & 0x1) {                               // bit 0 = value (1)
627                 oldstatus = new;                                        // store new as next last
628                 if (motor1_switch) pos1 += (diff & 2) - 1;              // bit 1 = direction (+/-)
629                 else pos1 -= (diff & 2) - 1;
630         }
631 }
632
633
634 static void update_hall2(void) {
635         unsigned char status = (PINA >> 4) & 0x3;
636         static unsigned char oldstatus=0;
637         unsigned char diff, new;
638
639         new = 0;
640         if (status & 0x1)
641                 new = 0x3;
642         if (status & 0x2)
643                 new ^= 0x1;                                     // convert gray to binary
644         diff = oldstatus - new;                         // difference last - new
645         if (diff & 0x1) {                               // bit 0 = value (1)
646                 oldstatus = new;                                        // store new as next last
647                 if (motor2_switch) pos2 -= (diff & 2) - 1;              // bit 1 = direction (+/-)
648                 else pos2 += (diff & 2) - 1;
649         }
650 }
651
652
653 static void update_hall3(void) {
654         unsigned char status = (PINA >> 2) & 0x3;
655         static unsigned char oldstatus=0;
656         unsigned char diff, new;
657
658         new = 0;
659         if (status & 0x1)
660                 new = 0x3;
661         if (status & 0x2)
662                 new ^= 0x1;                                     // convert gray to binary
663         diff = oldstatus - new;                         // difference last - new
664         if (diff & 0x1) {                               // bit 0 = value (1)
665                 oldstatus = new;                                        // store new as next last
666                 if (motor3_switch) pos3 -= (diff & 2) - 1;              // bit 1 = direction (+/-)
667                 else pos3 += (diff & 2) - 1;
668         }
669 }
670
671
672 static void update_hall4(void) {
673         unsigned char status = (PINA >> 6) & 0x3;
674         static unsigned char oldstatus=0;
675         unsigned char diff, new;
676
677         new = 0;
678         if (status & 0x1)
679                 new = 0x3;
680         if (status & 0x2)
681                 new ^= 0x1;                                     // convert gray to binary
682         diff = oldstatus - new;                         // difference last - new
683         if (diff & 0x1) {                               // bit 0 = value (1)
684                 oldstatus = new;                                        // store new as next last
685                 if (motor4_switch) pos4 += (diff & 2) - 1;              // bit 1 = direction (+/-)
686                 else pos4 -= (diff & 2) - 1;
687         }
688 }
689
690
691 static void update_motor(void) {
692         static int16_t m1_old=SHRT_MIN;
693         static int16_t m2_old=SHRT_MIN;
694         static int16_t m3_old=SHRT_MIN;
695         static int16_t m4_old=SHRT_MIN;
696
697         error_state &= 0xf0; // clear lower bits
698         error_state |= ~((PIND & 0x40)>>3 | (PINB & 0x07)) & 0xf;
699
700         if (m1_old != motor1) { // update only when changed
701                 if (motor1 == 0) {
702                         // stop
703                         PORTC &= ~(1 << 3) & ~(1 << 2);
704                 } else if (motor1 == PWM_BREAK) {
705                         PORTC |= (1 << 3) | (1 << 2);
706                 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
707                         // forward
708                         uint8_t tmp=PORTC;
709                         tmp &= ~(1 << 3);
710                         tmp |=  (1 << 2);
711                         PORTC = tmp;
712                 } else { // motor1 < 0
713                         // backward
714                         uint8_t tmp=PORTC;
715                         tmp &= ~(1 << 2);
716                         tmp |=  (1 << 3);
717                         PORTC = tmp;
718                 }
719
720                 m1_old = motor1;
721                 OCR1A = abs(motor1);
722         }
723
724         if (m2_old != motor2) { // update only when changed
725                 if (motor2 == 0) {
726                         // stop
727                         PORTC &= ~(1 << 5) & ~(1 << 4);
728                 } else if (motor2 == PWM_BREAK) {
729                         PORTC |= (1 << 5) | (1 << 4);
730                 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
731                         // forward
732                         uint8_t tmp=PORTC;
733                         tmp &= ~(1 << 5);
734                         tmp |=  (1 << 4);
735                         PORTC = tmp;
736                 } else { // motor2 < 0
737                         // backward
738                         uint8_t tmp=PORTC;
739                         tmp &= ~(1 << 4);
740                         tmp |=  (1 << 5);
741                         PORTC = tmp;
742                 }
743
744                 m2_old = motor2;
745                 OCR1B = abs(motor2);
746         }
747
748         if (m3_old != motor3) { // update only when changed
749                 if (motor3 == 0) {
750                         // stop
751                         PORTC &= ~(1 << 7) & ~(1 << 6);
752                 } else if (motor3 == PWM_BREAK) {
753                         PORTC |= (1 << 7) | (1 << 6);
754                 } else if ((!motor3_switch && motor3 > 0) || (motor3_switch && motor3 < 0)) {
755                         // forward
756                         uint8_t tmp=PORTC;
757                         tmp &= ~(1 << 7);
758                         tmp |=  (1 << 6);
759                         PORTC = tmp;
760                 } else { // motor3 < 0
761                         // backward
762                         uint8_t tmp=PORTC;
763                         tmp &= ~(1 << 6);
764                         tmp |=  (1 << 7);
765                         PORTC = tmp;
766                 }
767
768                 m3_old = motor3;
769                 OCR2 = abs(motor3);
770         }
771
772         if (m4_old != motor4) { // update only when changed
773                 if (motor4 == 0) {
774                         // stop
775                         PORTD &= ~(1 << 3) & ~(1 << 2);
776                 } else if (motor4 == PWM_BREAK) {
777                         PORTD |= (1 << 3) | (1 << 2);
778                 } else if ((!motor4_switch && motor4 > 0) || (motor4_switch && motor4 < 0)) {
779                         // forward
780                         uint8_t tmp=PORTD;
781                         tmp &= ~(1 << 3);
782                         tmp |=  (1 << 2);
783                         PORTD = tmp;
784                 } else { // motor4 < 0
785                         // backward
786                         uint8_t tmp=PORTD;
787                         tmp &= ~(1 << 2);
788                         tmp |=  (1 << 3);
789                         PORTD = tmp;
790                 }
791
792                 m4_old = motor4;
793                 OCR0 = abs(motor4);
794         }
795 }
796
797
798 static void update_pos(void) {
799         static int16_t pos1_last=0;
800         static int16_t pos2_last=0;
801         static int16_t pos3_last=0;
802         static int16_t pos4_last=0;
803         int16_t pos1_diff; // steps
804         int16_t pos2_diff;
805         int16_t pos3_diff;
806         int16_t pos4_diff;
807         float diff_left_m, diff_right_m, angle_diff, translation;
808         float pos_x_new, pos_y_new, angle_new;
809         float tmp_speed_lin, tmp_speed_rot;
810         int16_t cur_pos1, cur_pos2, cur_pos3, cur_pos4;
811         int16_t new_speed1, new_speed2, new_speed3, new_speed4;
812
813         // copy to tmp
814         cli();
815         cur_pos1 = pos1;
816         cur_pos2 = pos2;
817         cur_pos3 = pos3;
818         cur_pos4 = pos4;
819         sei();
820
821         pos1_diff = cur_pos1 - pos1_last;
822         pos2_diff = cur_pos2 - pos2_last;
823         pos3_diff = cur_pos3 - pos3_last;
824         pos4_diff = cur_pos4 - pos4_last;
825
826         new_speed1 = pos1_diff/PID_T;
827         new_speed2 = pos2_diff/PID_T;
828         new_speed3 = pos3_diff/PID_T;
829         new_speed4 = pos4_diff/PID_T;
830
831         diff_left_m = (pos1_diff + pos2_diff)/(2*STEP_PER_M_LEFT);
832         diff_right_m = (pos3_diff + pos4_diff)/(2*STEP_PER_M_RIGHT);
833         angle_diff = (diff_right_m - diff_left_m) / WHEEL_DIST;
834
835         angle_new = angle.f + angle_diff;
836         if (angle_new > 2*M_PI) angle_new-=2*M_PI;
837         else if (angle_new < -2*M_PI) angle_new+=2*M_PI;
838
839         translation = (diff_left_m + diff_right_m)/2.0;
840         pos_x_new = pos_x.f + cos(angle_new)*translation;
841         pos_y_new = pos_y.f + sin(angle_new)*translation;
842
843         tmp_speed_lin = translation/PID_T;
844         tmp_speed_rot = angle_diff/PID_T;
845
846         // copy from tmp
847         cli();
848         angle.f = angle_new;
849         pos_x.f = pos_x_new;
850         pos_y.f = pos_y_new;
851         speed1 = new_speed1;
852         speed2 = new_speed2;
853         speed3 = new_speed3;
854         speed4 = new_speed4;
855         cur_speed_lin = tmp_speed_lin;
856         cur_speed_rot = tmp_speed_rot;
857         sei();
858
859         pos1_last = cur_pos1;
860         pos2_last = cur_pos2;
861         pos3_last = cur_pos3;
862         pos4_last = cur_pos4;
863 }
864
865
866 static void update_pid(void) {
867         static int16_t eold1=0;
868         static int16_t eold2=0;
869         static int16_t eold3=0;
870         static int16_t eold4=0;
871         static int32_t esum1=0;
872         static int32_t esum2=0;
873         static int32_t esum3=0;
874         static int32_t esum4=0;
875
876         // protect motors from damage if stalling
877         if (labs(esum1) > 140000 && speed1 == 0) {
878                 motor1 = 0;
879                 motor1_mode = MOTOR_MANUAL;
880                 error_state |= (1<<4);
881                 esum1 = 0;
882         }       
883         if (labs(esum2) > 140000 && speed2 == 0) {
884                 motor2 = 0;
885                 motor2_mode = MOTOR_MANUAL;
886                 error_state |= (1<<5);
887                 esum2 = 0;
888         }       
889         if (labs(esum3) > 140000 && speed3 == 0) {
890                 motor3 = 0;
891                 motor3_mode = MOTOR_MANUAL;
892                 error_state |= (1<<6);
893                 esum3 = 0;
894         }       
895         // protect motors from damage if stalling
896         if (labs(esum4) > 140000 && speed4 == 0) {
897                 motor4 = 0;
898                 motor4_mode = MOTOR_MANUAL;
899                 error_state |= (1<<7);
900                 esum4 = 0;
901         }       
902
903         if (motor1_mode == MOTOR_PID) {
904                 if (speed1_wish != speed1_wish_old) {
905                         if (abs(speed1_wish - speed1_wish_old) > 500) esum1 = 0;
906                         speed1_wish_old = speed1_wish;
907                 }
908
909                 if (speed1_wish == 0) {
910                         motor1 = 0;
911                         eold1 = 0;
912                         error_state &= ~(1<<4);
913                 } else {
914                         int16_t e = speed1_wish - speed1;
915                         esum1+=e;
916                         motor1 = KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
917                         eold1 = e;
918
919                         if (motor1 > 0 && speed1_wish < 0) motor1=PWM_BREAK;
920                         else if (motor1 < 0 && speed1_wish > 0) motor1=PWM_BREAK;
921                         else if (motor1 > 255) motor1 = 255;
922                         else if (motor1 < -255) motor1 = -255;
923                 }
924         }
925         if (motor2_mode == MOTOR_PID) {
926                 if (speed2_wish != speed2_wish_old) {
927                         if (abs(speed2_wish - speed2_wish_old) > 500) esum2 = 0;
928                         speed2_wish_old = speed2_wish;
929                 }
930
931                 if (speed2_wish == 0) {
932                         motor2 = 0;
933                         eold2 = 0;
934                         error_state &= ~(1<<5);
935                 } else {
936                         int16_t e = speed2_wish - speed2;
937                         esum2+=e;
938                         motor2 = KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
939                         eold2 = e;
940
941                         if (motor2 > 0 && speed2_wish < 0) motor2=PWM_BREAK;
942                         else if (motor2 < 0 && speed2_wish > 0) motor2=PWM_BREAK;
943                         else if (motor2 > 255) motor2 = 255;
944                         else if (motor2 < -255) motor2 = -255;
945                 }
946         }
947         if (motor3_mode == MOTOR_PID) {
948                 if (speed3_wish != speed3_wish_old) {
949                         if (abs(speed3_wish - speed3_wish_old) > 500) esum3 = 0;
950                         speed3_wish_old = speed3_wish;
951                 }
952
953                 if (speed3_wish == 0) {
954                         motor3 = 0;
955                         eold3 = 0;
956                         error_state &= ~(1<<6);
957                 } else {
958                         int16_t e = speed3_wish - speed3;
959                         esum3+=e;
960                         motor3 = KP*e + KI*PID_T*esum3 + KD/PID_T*(e - eold3);
961                         eold3 = e;
962
963                         if (motor3 > 0 && speed3_wish < 0) motor3=PWM_BREAK;
964                         else if (motor3 < 0 && speed3_wish > 0) motor3=PWM_BREAK;
965                         else if (motor3 > 255) motor3 = 255;
966                         else if (motor3 < -255) motor3 = -255;
967                 }
968         }
969         if (motor4_mode == MOTOR_PID) {
970                 if (speed4_wish != speed4_wish_old) {
971                         if (abs(speed4_wish - speed4_wish_old) > 500) esum4 = 0;
972                         speed4_wish_old = speed4_wish;
973                 }
974
975                 if (speed4_wish == 0) {
976                         motor4 = 0;
977                         eold4 = 0;
978                         error_state &= ~(1<<7);
979                 } else {
980                         int16_t e = speed4_wish - speed4;
981                         esum4+=e;
982                         motor4 = KP*e + KI*PID_T*esum4 + KD/PID_T*(e - eold4);
983                         eold4 = e;
984
985                         if (motor4 > 0 && speed4_wish < 0) motor4=PWM_BREAK;
986                         else if (motor4 < 0 && speed4_wish > 0) motor4=PWM_BREAK;
987                         else if (motor4 > 255) motor4 = 255;
988                         else if (motor4 < -255) motor4 = -255;
989                 }
990         }
991 }
992
993
994 ISR(TIMER1_OVF_vect) {
995         update_hall1();
996         update_hall2();
997         update_hall3();
998         update_hall4();
999         
1000         run_update++;
1001 }
1002
1003
1004 int main(void) {
1005         // Outputs
1006         DDRB = (1 << 3);
1007         DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
1008         DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
1009         // Pullup Diag/Enable
1010         PORTB = (1 << 0) | (1 << 1) | (1 << 2);
1011         PORTD = (1 << 6);
1012
1013         bootloader = 0x00;
1014         setup_uart(9600);
1015         uart_setup_stdout();
1016
1017         // I2C
1018         TWAR = 0x50;
1019         TWI_RESET;
1020
1021         // Motor 1 & 2
1022         // Timer 1: Fast PWM non-inverting mode, Top=255 => 15.625kHz
1023         // Prescaler=1
1024         TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10);
1025         TCCR1B = (1 << WGM12) | (1 << CS10);
1026         OCR1A = 0;
1027         OCR1B = 0;
1028
1029         // Motor 3
1030         // Timer 2: Fast PWM non-inverting mode, Top=255
1031         // Prescaler=1
1032         TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << COM21) | (1 << CS20);
1033         OCR2 = 0;
1034
1035         // Motor 4
1036         // Timer 0: Fast PWM non-inverting mode, Top=255
1037         // Prescaler=1
1038         TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << COM01) | (1 << CS00);
1039         OCR0 = 0;
1040
1041         printf("\r\nStart\r\n");
1042
1043         set_sleep_mode(SLEEP_MODE_IDLE);
1044         // Enable Timer 1 Overflow Interrupt
1045         TIMSK = (1 << TOIE1);
1046         sei();
1047
1048         while(1) {
1049                 switch(ireg) {
1050                         case 0xff: // Magic reg that starts the bootloader
1051                                 if (bootloader == 0xa5) {
1052                                         cli();
1053                                         {
1054                                                 void (*start)(void) = (void*)0x1800;
1055                                                 start();
1056                                         }
1057                                 }
1058                                 break;
1059                 }
1060
1061                 if (cmd_vel.bUpdate) {
1062                         float speed_wish_right, speed_wish_left;
1063                         float speed, angle;
1064
1065                         cli();
1066                         speed = cmd_vel.speed;
1067                         angle = cmd_vel.angle;
1068                         cmd_vel.bUpdate = 0;
1069                         sei();
1070
1071                         speed_wish_right = (angle*WHEEL_DIST)/2 + speed;
1072                         speed_wish_left = speed*2-speed_wish_right;
1073
1074                         speed_wish_left*=STEP_PER_M_LEFT;
1075                         speed_wish_right*=STEP_PER_M_RIGHT;
1076
1077                         if (aft_handicap > 0) {
1078                                 speed1_wish = speed_wish_left * (100-aft_handicap)/100.0;
1079                                 speed3_wish = speed_wish_right * (100-aft_handicap)/100.0;
1080                         } else {
1081                                 speed1_wish = speed_wish_left;
1082                                 speed3_wish = speed_wish_right;
1083                         }
1084                         if (front_handicap > 0) {
1085                                 speed2_wish = speed_wish_left * (100-front_handicap)/100.0;
1086                                 speed4_wish = speed_wish_right * (100-front_handicap)/100.0;
1087                         } else {
1088                                 speed2_wish = speed_wish_left;
1089                                 speed4_wish = speed_wish_right;
1090                         }
1091                         motor1_mode = MOTOR_PID;
1092                         motor2_mode = MOTOR_PID;
1093                         motor3_mode = MOTOR_PID;
1094                         motor4_mode = MOTOR_PID;
1095                 }
1096
1097                 if (run_update >= 156) { // ~100Hz
1098                         run_update=0;
1099
1100                         update_pos();
1101                         update_pid();
1102                         update_motor();
1103                         count_test++;
1104                 }
1105
1106                 sleep_mode();
1107         }
1108
1109         return 0;
1110 }