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