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