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