]> defiant.homedns.org Git - ros_wild_thumper.git/blob - avr/motor_ctrl/main.c
Transision from 4x TLE5205 to 2x VNH2SP30
[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  * free
18  * 0x10 Hall 1 MSB
19  * 0x11 Hall 1 LSB
20  * 0x12 Hall 2 MSB
21  * 0x13 Hall 2 LSB
22  * 0x14 Hall 3 MSB
23  * 0x15 Hall 3 LSB
24  * 0x16 Hall 4 MSB
25  * 0x17 Hall 4 LSB
26  * free
27  * 0x20 Motor 1 speed wish MSB
28  * 0x21 Motor 1 speed wish LSB
29  * 0x22 Motor 2 speed wish MSB
30  * 0x23 Motor 2 speed wish LSB
31  * 0x28 Left speed wish (m/s) MSB
32  * 0x29 Left speed wish (m/s)
33  * 0x2A Left speed wish (m/s)
34  * 0x2B Left speed wish (m/s) LSB
35  * 0x2C Right speed wish (m/s) MSB
36  * 0x2D Right speed wish (m/s)
37  * 0x2E Right speed wish (m/s)
38  * 0x2F Right speed wish (m/s) LSB
39  * 0x30 Motor 1 speed MSB
40  * 0x31 Motor 1 speed LSB
41  * 0x32 Motor 2 speed MSB
42  * 0x33 Motor 2 speed LSB
43  * 0x38 Speed (m/s) MSB
44  * 0x39 Speed (m/s)
45  * 0x3A Speed (m/s)
46  * 0x3B Speed (m/s) LSB
47  * 0x3C Angle (rad/s) MSB
48  * 0x3D Angle (rad/s)
49  * 0x3E Angle (rad/s)
50  * 0x3F Angle (rad/s) LSB
51  * 0x40 Position x (m) MSB
52  * 0x41 Position x (m)
53  * 0x42 Position x (m)
54  * 0x43 Position x (m) LSB
55  * 0x44 Position y (m) MSB
56  * 0x45 Position y (m)
57  * 0x46 Position y (m)
58  * 0x47 Position y (m) LSB
59  * 0x48 Position angle MSB
60  * 0x49 Position angle
61  * 0x4A Position angle
62  * 0x4B Position angle LSB
63  * free
64  * 0x50 speed wish (m/s) MSB
65  * 0x51 speed wish (m/s)
66  * 0x52 speed wish (m/s)
67  * 0x53 speed wish (m/s) LSB
68  * 0x54 angle wish (rad/s) MSB
69  * 0x55 angle wish (rad/s)
70  * 0x56 angle wish (rad/s)
71  * 0x57 angle wish (rad/s) LSB
72  * free
73  * 0x90 Motor 1 switch
74  * 0x91 Motor 2 switch
75  * free
76  * 0xA0 Reset reason
77  * 0xA1 Error status
78  * 0xA2 count test
79  * free
80  * 0xff Bootloader
81  */
82
83
84 #define TWI_ACK         TWCR = (1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
85 #define TWI_RESET       TWCR &= ~((1 << TWSTO) | (1 << TWEN)); TWI_ACK
86 #define TWI_NAK         TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
87
88 #define KP 0.045
89 #define KI 2.298
90 #define KD 0.0004
91 #define PID_T 0.01
92 // wheel diameter=12cm, encoder=48cpr, gear ratio=1:34, real wheel diameter: 0.12454m
93 #define STEP_PER_M_AVG 4171.4
94 #define STEP_PER_M_LEFT (STEP_PER_M_AVG)
95 #define STEP_PER_M_RIGHT (STEP_PER_M_AVG)
96 #define WHEEL_DIST 0.36923 // Real: 0.252
97
98 enum mode {
99         MOTOR_MANUAL,
100         MOTOR_PID
101 };
102
103 typedef union {
104         float f;
105         uint32_t i;
106 } ufloat_t;
107
108 static volatile struct {
109         float speed;
110         float angle;
111         uint8_t bUpdate;
112 } cmd_vel = {0, 0, 0};
113
114 static volatile uint8_t ireg=0;
115 static volatile uint8_t bootloader=0;
116 static volatile int16_t motor1=0; // -255..+255
117 static volatile int16_t motor2=0;
118 static volatile int16_t pos1=0; // step
119 static volatile int16_t pos2=0;
120 static volatile int16_t pos3=0;
121 static volatile int16_t pos4=0;
122 static volatile enum mode motor1_mode=MOTOR_MANUAL;
123 static volatile enum mode motor2_mode=MOTOR_MANUAL;
124 static volatile uint8_t motor1_switch=0;
125 static volatile uint8_t motor2_switch=1;
126 static volatile int16_t speed1_wish=0; // step/s
127 static volatile int16_t speed2_wish=0;
128 static volatile uint8_t run_update=0;
129 static volatile int16_t speed1=0; // step/s
130 static volatile int16_t speed2=0;
131 static volatile int16_t speed3=0;
132 static volatile int16_t speed4=0;
133 static volatile int16_t speed_l=0;
134 static volatile int16_t speed_r=0;
135 static volatile ufloat_t pos_x={0.0};
136 static volatile ufloat_t pos_y={0.0};
137 static volatile ufloat_t angle={0.0};
138 static volatile float cur_speed_lin=0;
139 static volatile float cur_speed_rot=0;
140 static volatile uint8_t count_test=0;
141 static volatile uint8_t error_state=0;
142
143 ISR(TWI_vect)
144 {
145         static uint8_t tmp=0;
146         static int16_t tmp16=0;
147         static ufloat_t tmp_speed;
148         static ufloat_t tmp_angle;
149
150         switch (TWSR & 0xF8)
151         {  
152                 case 0x60: // start write
153                         TWI_ACK;
154                         ireg = 0;
155                         break;
156                 case 0x80: // write
157                         switch(ireg) {
158                                 case 0x00: // register select
159                                         ireg = TWDR;
160                                         ireg--; // because we do ireg++ below
161                                         TWI_ACK;
162                                         break;
163                                 case 0x01: // Motor 1 MSB
164                                         tmp = TWDR;
165                                         TWI_ACK;
166                                         break;
167                                 case 0x02: // Motor 1 LSB
168                                         motor1 = tmp<<8 | TWDR;
169                                         motor1_mode = MOTOR_MANUAL;
170                                         TWI_ACK;
171                                         break;
172                                 case 0x03: // Motor 2 MSB
173                                         tmp = TWDR;
174                                         TWI_ACK;
175                                         break;
176                                 case 0x04: // Motor 2 LSB
177                                         motor2 = tmp<<8 | TWDR;
178                                         motor2_mode = MOTOR_MANUAL;
179                                         TWI_ACK;
180                                         break;
181                                 case 0x20: // Motor 1 speed wish MSB
182                                         tmp = TWDR;
183                                         TWI_ACK;
184                                         break;
185                                 case 0x21: // Motor 1 speed wish LSB
186                                         speed1_wish = tmp<<8 | TWDR;
187                                         motor1_mode = MOTOR_PID;
188                                         TWI_ACK;
189                                         break;
190                                 case 0x22: // Motor 2 speed wish MSB
191                                         tmp = TWDR;
192                                         TWI_ACK;
193                                         break;
194                                 case 0x23: // Motor 2 speed wish LSB
195                                         speed2_wish = tmp<<8 | TWDR;
196                                         motor2_mode = MOTOR_PID;
197                                         TWI_ACK;
198                                         break;
199                                 case 0x28: // Left speed wish MSB
200                                         tmp_speed.i = TWDR;
201                                         TWI_ACK;
202                                         break;
203                                 case 0x29: // Left speed wish
204                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
205                                         TWI_ACK;
206                                         break;
207                                 case 0x2A: // Left speed wish
208                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
209                                         TWI_ACK;
210                                         break;
211                                 case 0x2B: // Left speed wish LSB
212                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
213                                         speed1_wish = tmp_speed.f*STEP_PER_M_LEFT;
214                                         speed2_wish = tmp_speed.f*STEP_PER_M_LEFT;
215                                         motor1_mode = MOTOR_PID;
216                                         motor2_mode = MOTOR_PID;
217                                         TWI_ACK;
218                                         break;
219                                 case 0x2C: // Right speed wish MSB
220                                         tmp_speed.i = TWDR;
221                                         TWI_ACK;
222                                         break;
223                                 case 0x2D: // Right speed wish
224                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
225                                         TWI_ACK;
226                                         break;
227                                 case 0x2E: // Right speed wish
228                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
229                                         TWI_ACK;
230                                         break;
231                                 case 0x2F: // Right speed wish LSB
232                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
233                                         speed1_wish = tmp_speed.f*STEP_PER_M_RIGHT;
234                                         speed2_wish = tmp_speed.f*STEP_PER_M_RIGHT;
235                                         motor1_mode = MOTOR_PID;
236                                         motor2_mode = MOTOR_PID;
237                                         TWI_ACK;
238                                         break;
239                                 case 0x50: // speed wish MSB
240                                         tmp_speed.i = TWDR;
241                                         TWI_ACK;
242                                         break;
243                                 case 0x51: // speed wish
244                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
245                                         TWI_ACK;
246                                         break;
247                                 case 0x52: // speed wish
248                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
249                                         TWI_ACK;
250                                         break;
251                                 case 0x53: // speed wish LSB
252                                         tmp_speed.i = tmp_speed.i << 8 | TWDR;
253                                         cmd_vel.speed = tmp_speed.f;
254                                         TWI_ACK;
255                                         break;
256                                 case 0x54: // angle wish MSB
257                                         tmp_angle.i = TWDR;
258                                         TWI_ACK;
259                                         break;
260                                 case 0x55: // angle wish
261                                         tmp_angle.i = tmp_angle.i << 8 | TWDR;
262                                         TWI_ACK;
263                                         break;
264                                 case 0x56: // angle wish
265                                         tmp_angle.i = tmp_angle.i << 8 | TWDR;
266                                         TWI_ACK;
267                                         break;
268                                 case 0x57: // angle wish LSB
269                                         tmp_angle.i = tmp_angle.i << 8 | TWDR;
270                                         cmd_vel.angle = tmp_angle.f;
271                                         cmd_vel.bUpdate = 1;
272                                         TWI_ACK;
273                                         break;
274                                 case 0x90: // Motor 1 switch
275                                         motor1_switch = TWDR;
276                                         TWI_ACK;
277                                         break;
278                                 case 0x91: // Motor 2 switch
279                                         motor2_switch = TWDR;
280                                         TWI_ACK;
281                                         break;
282                                 case 0xff: // bootloader
283                                         bootloader = TWDR;
284                                 default:
285                                         TWI_NAK;
286                         }
287                         ireg++;
288                         break;
289                 case 0xA8: // start read
290                 case 0xB8: // read
291                         switch(ireg) {
292                                 case 0x02: // Motor 1 PWM
293                                         TWDR = OCR1A;
294                                         TWI_ACK;
295                                         break;
296                                 case 0x04: // Motor 2 PWM
297                                         TWDR = OCR1B;
298                                         TWI_ACK;
299                                         break;
300                                 case 0x10: // Hall 1 MSB
301                                         tmp16 = pos1;
302                                         TWDR = tmp16>>8;
303                                         TWI_ACK;
304                                         break;
305                                 case 0x11: // Hall 1 LSB
306                                         TWDR = tmp16;
307                                         TWI_ACK;
308                                         break;
309                                 case 0x12: // Hall 2 MSB
310                                         tmp16 = pos2;
311                                         TWDR = tmp16>>8;
312                                         TWI_ACK;
313                                         break;
314                                 case 0x13: // Hall 2 LSB
315                                         TWDR = tmp16;
316                                         TWI_ACK;
317                                         break;
318                                 case 0x14: // Hall 3 MSB
319                                         tmp16 = pos3;
320                                         TWDR = tmp16>>8;
321                                         TWI_ACK;
322                                         break;
323                                 case 0x15: // Hall 3 LSB
324                                         TWDR = tmp16;
325                                         TWI_ACK;
326                                         break;
327                                 case 0x16: // Hall 4 MSB
328                                         tmp16 = pos4;
329                                         TWDR = tmp16>>8;
330                                         TWI_ACK;
331                                         break;
332                                 case 0x17: // Hall 4 LSB
333                                         TWDR = tmp16;
334                                         TWI_ACK;
335                                         break;
336                                 case 0x20: // Motor 1 speed wish MSB
337                                         TWDR = speed1_wish>>8;
338                                         TWI_ACK;
339                                         break;
340                                 case 0x21: // Motor 1 speed wish LSB
341                                         TWDR = speed1_wish;
342                                         TWI_ACK;
343                                         break;
344                                 case 0x22: // Motor 2 speed wish MSB
345                                         TWDR = speed2_wish>>8;
346                                         TWI_ACK;
347                                         break;
348                                 case 0x23: // Motor 2 speed wish LSB
349                                         TWDR = speed2_wish;
350                                         TWI_ACK;
351                                         break;
352                                 case 0x30: // Motor 1 speed MSB
353                                         TWDR = speed1>>8;
354                                         TWI_ACK;
355                                         break;
356                                 case 0x31: // Motor 1 speed LSB
357                                         TWDR = speed1;
358                                         TWI_ACK;
359                                         break;
360                                 case 0x32: // Motor 2 speed MSB
361                                         TWDR = speed2>>8;
362                                         TWI_ACK;
363                                         break;
364                                 case 0x33: // Motor 2 speed LSB
365                                         TWDR = speed2;
366                                         TWI_ACK;
367                                         break;
368                                 case 0x34: // Motor 3 speed MSB
369                                         TWDR = speed3>>8;
370                                         TWI_ACK;
371                                         break;
372                                 case 0x35: // Motor 3 speed LSB
373                                         TWDR = speed3;
374                                         TWI_ACK;
375                                         break;
376                                 case 0x36: // Motor 4 speed MSB
377                                         TWDR = speed4>>8;
378                                         TWI_ACK;
379                                         break;
380                                 case 0x37: // Motor 4 speed LSB
381                                         TWDR = speed4;
382                                         TWI_ACK;
383                                         break;
384                                 case 0x38: // speed MSB
385                                         tmp_speed.f = cur_speed_lin;
386                                         TWDR = tmp_speed.i>>24;
387                                         TWI_ACK;
388                                         break;
389                                 case 0x39: // speed
390                                         TWDR = tmp_speed.i>>16;
391                                         TWI_ACK;
392                                         break;
393                                 case 0x3A: // speed
394                                         TWDR = tmp_speed.i>>8;
395                                         TWI_ACK;
396                                         break;
397                                 case 0x3B: // speed LSB
398                                         TWDR = tmp_speed.i;
399                                         TWI_ACK;
400                                         break;
401                                 case 0x3C: // angle MSB
402                                         tmp_angle.f = cur_speed_rot;
403                                         TWDR = tmp_angle.i>>24;
404                                         TWI_ACK;
405                                         break;
406                                 case 0x3D: // angle
407                                         TWDR = tmp_angle.i>>16;
408                                         TWI_ACK;
409                                         break;
410                                 case 0x3E: // angle
411                                         TWDR = tmp_angle.i>>8;
412                                         TWI_ACK;
413                                         break;
414                                 case 0x3F: // angle LSB
415                                         TWDR = angle.i;
416                                         TWI_ACK;
417                                         break;
418                                 case 0x40: // Position x MSB
419                                         TWDR = pos_x.i>>24;
420                                         TWI_ACK;
421                                         break;
422                                 case 0x41: // Position x
423                                         TWDR = pos_x.i>>16;
424                                         TWI_ACK;
425                                         break;
426                                 case 0x42: // Position x
427                                         TWDR = pos_x.i>>8;
428                                         TWI_ACK;
429                                         break;
430                                 case 0x43: // Position x LSB
431                                         TWDR = pos_x.i;
432                                         TWI_ACK;
433                                         break;
434                                 case 0x44: // Position y MSB
435                                         TWDR = pos_y.i>>24;
436                                         TWI_ACK;
437                                         break;
438                                 case 0x45: // Position y
439                                         TWDR = pos_y.i>>16;
440                                         TWI_ACK;
441                                         break;
442                                 case 0x46: // Position y
443                                         TWDR = pos_y.i>>8;
444                                         TWI_ACK;
445                                         break;
446                                 case 0x47: // Position y LSB
447                                         TWDR = pos_y.i;
448                                         TWI_ACK;
449                                         break;
450                                 case 0x48: // Position angle MSB
451                                         TWDR = angle.i>>24;
452                                         TWI_ACK;
453                                         break;
454                                 case 0x49: // Position angle
455                                         TWDR = angle.i>>16;
456                                         TWI_ACK;
457                                         break;
458                                 case 0x4A: // Position angle
459                                         TWDR = angle.i>>8;
460                                         TWI_ACK;
461                                         break;
462                                 case 0x4B: // Position angle LSB
463                                         TWDR = angle.i;
464                                         TWI_ACK;
465                                         break;
466                                 case 0xA0: // Reset reason
467                                         TWDR = MCUCSR & 0x0f;
468                                         MCUCSR = 0x0;
469                                         TWI_ACK;
470                                         break;
471                                 case 0xA1: // TLE Error status
472                                         TWDR = error_state;
473                                         TWI_ACK;
474                                         break;
475                                 case 0xA2: // count test
476                                         TWDR = count_test;
477                                         TWI_ACK;
478                                 default:
479                                         TWDR = 0;
480                                         TWI_NAK;
481                         }
482                         ireg++;
483                         break;
484                 default:
485                         TWI_RESET;
486         }
487 }
488
489
490 static void update_hall1(void) {
491         unsigned char status = (PINA >> 0) & 0x3;
492         static unsigned char oldstatus=0;
493         unsigned char diff, new;
494
495         new = 0;
496         if (status & 0x1)
497                 new = 0x3;
498         if (status & 0x2)
499                 new ^= 0x1;                                     // convert gray to binary
500         diff = oldstatus - new;                         // difference last - new
501         if (diff & 0x1) {                               // bit 0 = value (1)
502                 oldstatus = new;                                        // store new as next last
503                 if (motor1_switch) pos1 -= (diff & 2) - 1;              // bit 1 = direction (+/-)
504                 else pos1 += (diff & 2) - 1;
505         }
506 }
507
508
509 static void update_hall2(void) {
510         unsigned char status = (PINA >> 4) & 0x3;
511         static unsigned char oldstatus=0;
512         unsigned char diff, new;
513
514         new = 0;
515         if (status & 0x1)
516                 new = 0x3;
517         if (status & 0x2)
518                 new ^= 0x1;                                     // convert gray to binary
519         diff = oldstatus - new;                         // difference last - new
520         if (diff & 0x1) {                               // bit 0 = value (1)
521                 oldstatus = new;                                        // store new as next last
522                 if (motor1_switch) pos2 -= (diff & 2) - 1;              // bit 1 = direction (+/-)
523                 else pos2 += (diff & 2) - 1;
524         }
525 }
526
527
528 static void update_hall3(void) {
529         unsigned char status = (PINA >> 2) & 0x3;
530         static unsigned char oldstatus=0;
531         unsigned char diff, new;
532
533         new = 0;
534         if (status & 0x1)
535                 new = 0x3;
536         if (status & 0x2)
537                 new ^= 0x1;                                     // convert gray to binary
538         diff = oldstatus - new;                         // difference last - new
539         if (diff & 0x1) {                               // bit 0 = value (1)
540                 oldstatus = new;                                        // store new as next last
541                 if (motor2_switch) pos3 += (diff & 2) - 1;              // bit 1 = direction (+/-)
542                 else pos3 -= (diff & 2) - 1;
543         }
544 }
545
546
547 static void update_hall4(void) {
548         unsigned char status = (PINA >> 6) & 0x3;
549         static unsigned char oldstatus=0;
550         unsigned char diff, new;
551
552         new = 0;
553         if (status & 0x1)
554                 new = 0x3;
555         if (status & 0x2)
556                 new ^= 0x1;                                     // convert gray to binary
557         diff = oldstatus - new;                         // difference last - new
558         if (diff & 0x1) {                               // bit 0 = value (1)
559                 oldstatus = new;                                        // store new as next last
560                 if (motor2_switch) pos4 -= (diff & 2) - 1;              // bit 1 = direction (+/-)
561                 else pos4 += (diff & 2) - 1;
562         }
563 }
564
565
566 static void update_motor(void) {
567         static int16_t m1_old=SHRT_MIN;
568         static int16_t m2_old=SHRT_MIN;
569
570         error_state = ~(PINB & 0x03);
571
572         if (motor1_mode == MOTOR_PID && bit_is_set(error_state, 0)) {
573                 // if error and running: stop
574                 if (m1_old != 0) motor1 = 0;
575                 // if we start motor in error state: start with full power
576                 else if (motor1 > 0) motor1 = 255;
577                 else if (motor1 < 0) motor1 = -255;
578         }
579         if (motor2_mode == MOTOR_PID && bit_is_set(error_state, 1)) {
580                 // if error and running: stop
581                 if (m2_old != 0) motor2 = 0;
582                 // if we start motor in error state: start with full power
583                 else if (motor2 > 0) motor2 = 255;
584                 else if (motor2 < 0) motor2 = -255;
585         }
586
587         if (m1_old != motor1) { // update only when changed
588                 if (motor1 == 0) {
589                         // stop
590                         PORTC &= ~(1 << 3) & ~(1 << 2);
591                 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
592                         // forward
593                         PORTC |=  (1 << 2);
594                         PORTC &= ~(1 << 3);
595                 } else { // motor1 < 0
596                         // backward
597                         PORTC &= ~(1 << 2);
598                         PORTC |=  (1 << 3);
599                 }
600
601                 m1_old = motor1;
602                 OCR1A = abs(motor1);
603         }
604
605         if (m2_old != motor2) { // update only when changed
606                 if (motor2 == 0) {
607                         // stop
608                         PORTC &= ~(1 << 5) & ~(1 << 4);
609                 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
610                         // forward
611                         PORTC |=  (1 << 4);
612                         PORTC &= ~(1 << 5);
613                 } else { // motor2 < 0
614                         // backward
615                         PORTC &= ~(1 << 4);
616                         PORTC |=  (1 << 5);
617                 }
618
619                 m2_old = motor2;
620                 OCR1B = abs(motor2);
621         }
622 }
623
624
625 static void update_pos(void) {
626         static int16_t pos1_last=0;
627         static int16_t pos2_last=0;
628         static int16_t pos3_last=0;
629         static int16_t pos4_last=0;
630         int16_t pos1_diff; // steps
631         int16_t pos2_diff;
632         int16_t pos3_diff;
633         int16_t pos4_diff;
634         float diff_left_m, diff_right_m, angle_diff, translation;
635         float pos_x_new, pos_y_new, angle_new;
636         float tmp_speed_lin, tmp_speed_rot;
637         int16_t cur_pos1, cur_pos2, cur_pos3, cur_pos4;
638         int16_t new_speed1, new_speed2, new_speed3, new_speed4;
639
640         // copy to tmp
641         cli();
642         cur_pos1 = pos1;
643         cur_pos2 = pos2;
644         cur_pos3 = pos3;
645         cur_pos4 = pos4;
646         sei();
647
648         pos1_diff = cur_pos1 - pos1_last;
649         pos2_diff = cur_pos2 - pos2_last;
650         pos3_diff = cur_pos3 - pos3_last;
651         pos4_diff = cur_pos4 - pos4_last;
652
653         new_speed1 = pos1_diff/PID_T;
654         new_speed2 = pos2_diff/PID_T;
655         new_speed3 = pos3_diff/PID_T;
656         new_speed4 = pos4_diff/PID_T;
657
658         diff_left_m = (pos1_diff + pos2_diff)/(2*STEP_PER_M_LEFT);
659         diff_right_m = (pos3_diff + pos4_diff)/(2*STEP_PER_M_RIGHT);
660         angle_diff = (diff_right_m - diff_left_m) / WHEEL_DIST;
661
662         angle_new = angle.f + angle_diff;
663         if (angle_new > 2*M_PI) angle_new-=2*M_PI;
664         else if (angle_new < -2*M_PI) angle_new+=2*M_PI;
665
666         translation = (diff_left_m + diff_right_m)/2.0;
667         pos_x_new = pos_x.f + cos(angle_new)*translation;
668         pos_y_new = pos_y.f + sin(angle_new)*translation;
669
670         speed_l = (new_speed1+new_speed2)/2;
671         speed_r = (new_speed3+new_speed4)/2;
672         tmp_speed_lin = (speed_l + speed_r)/(2.0*STEP_PER_M_AVG);
673         tmp_speed_rot = (speed_r - speed_l)/(M_PI*WHEEL_DIST*STEP_PER_M_AVG);
674
675         // copy from tmp
676         cli();
677         angle.f = angle_new;
678         pos_x.f = pos_x_new;
679         pos_y.f = pos_y_new;
680         speed1 = new_speed1;
681         speed2 = new_speed2;
682         speed3 = new_speed3;
683         speed4 = new_speed4;
684         cur_speed_lin = tmp_speed_lin;
685         cur_speed_rot = tmp_speed_rot;
686         sei();
687
688         pos1_last = cur_pos1;
689         pos2_last = cur_pos2;
690         pos3_last = cur_pos3;
691         pos4_last = cur_pos4;
692 }
693
694
695 static void update_pid(void) {
696         static int16_t eold1=0;
697         static int16_t eold2=0;
698         static int32_t esum1=0;
699         static int32_t esum2=0;
700
701         if (motor1_mode == MOTOR_PID) {
702                 if (speed1_wish == 0) {
703                         motor1 = 0;
704                         eold1 = 0;
705                         esum1 = 0;
706                 } else {
707                         int16_t e = speed1_wish - speed_l;
708                         esum1+=e;
709                         motor1 = KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
710                         eold1 = e;
711
712                         if (motor1 > 255) motor1 = 255;
713                         else if (motor1 < -255) motor1 = -255;
714                 }
715         }
716         if (motor2_mode == MOTOR_PID) {
717                 if (speed2_wish == 0) {
718                         motor2 = 0;
719                         eold2 = 0;
720                         esum2 = 0;
721                 } else {
722                         int16_t e = speed2_wish - speed_r;
723                         esum2+=e;
724                         motor2 = KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
725                         eold2 = e;
726
727                         if (motor2 > 255) motor2 = 255;
728                         else if (motor2 < -255) motor2 = -255;
729                 }
730         }
731 }
732
733
734 ISR(TIMER1_OVF_vect) {
735         update_hall1();
736         update_hall2();
737         update_hall3();
738         update_hall4();
739         
740         run_update++;
741 }
742
743
744 int main(void) {
745         // Outputs
746         DDRB = (1 << 3);
747         DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
748         DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
749
750         bootloader = 0x00;
751         setup_uart(9600);
752         uart_setup_stdout();
753
754         // I2C
755         TWAR = 0x50;
756         TWI_RESET;
757
758         // Motor 1 & 2
759         // Timer 1: Fast PWM non-inverting mode, Top=256 => 15.625kHz
760         // Prescaler=1
761         TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10);
762         TCCR1B = (1 << WGM12) | (1 << CS10);
763         OCR1A = 0;
764         OCR1B = 0;
765
766         printf("\r\nStart\r\n");
767
768         set_sleep_mode(SLEEP_MODE_IDLE);
769         // Enable Timer 1 Overflow Interrupt
770         TIMSK = (1 << TOIE1);
771         sei();
772
773         while(1) {
774                 switch(ireg) {
775                         case 0xff: // Magic reg that starts the bootloader
776                                 if (bootloader == 0xa5) {
777                                         cli();
778                                         {
779                                                 void (*start)(void) = (void*)0x1800;
780                                                 start();
781                                         }
782                                 }
783                                 break;
784                 }
785
786                 if (cmd_vel.bUpdate) {
787                         float speed_wish_right, speed_wish_left;
788                         float speed, angle;
789
790                         cli();
791                         speed = cmd_vel.speed;
792                         angle = cmd_vel.angle;
793                         cmd_vel.bUpdate = 0;
794                         sei();
795
796                         speed_wish_right = angle*M_PI*WHEEL_DIST/2 + speed;
797                         speed_wish_left = speed*2-speed_wish_right;
798
799                         speed_wish_left*=STEP_PER_M_LEFT;
800                         speed_wish_right*=STEP_PER_M_RIGHT;
801
802                         speed1_wish = speed_wish_left;
803                         speed2_wish = speed_wish_right;
804                         motor1_mode = MOTOR_PID;
805                         motor2_mode = MOTOR_PID;
806                 }
807
808                 if (run_update >= 156) { // ~100Hz
809                         run_update=0;
810
811                         update_pos();
812                         update_pid();
813                         update_motor();
814                         count_test++;
815                 }
816
817                 sleep_mode();
818         }
819
820         return 0;
821 }