]> defiant.homedns.org Git - ros_wild_thumper.git/blob - avr/motor_ctrl/main.c
motor_ctrl: handle tle error state
[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 TLE 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.009
105 #define KI 0.051429
106 #define KD 0.000378
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=0;
145 static volatile uint8_t motor2_switch=0;
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 0x04: // Motor 2 PWM
375                                         TWDR = OCR1B;
376                                         TWI_ACK;
377                                         break;
378                                 case 0x06: // Motor 3 PWM
379                                         TWDR = OCR2;
380                                         TWI_ACK;
381                                         break;
382                                 case 0x08: // Motor 4 PWM
383                                         TWDR = OCR0;
384                                         TWI_ACK;
385                                         break;
386                                 case 0x10: // Hall 1 MSB
387                                         tmp16 = pos1;
388                                         TWDR = tmp16>>8;
389                                         TWI_ACK;
390                                         break;
391                                 case 0x11: // Hall 1 LSB
392                                         TWDR = tmp16;
393                                         TWI_ACK;
394                                         break;
395                                 case 0x12: // Hall 2 MSB
396                                         tmp16 = pos2;
397                                         TWDR = tmp16>>8;
398                                         TWI_ACK;
399                                         break;
400                                 case 0x13: // Hall 2 LSB
401                                         TWDR = tmp16;
402                                         TWI_ACK;
403                                         break;
404                                 case 0x14: // Hall 3 MSB
405                                         tmp16 = pos3;
406                                         TWDR = tmp16>>8;
407                                         TWI_ACK;
408                                         break;
409                                 case 0x15: // Hall 3 LSB
410                                         TWDR = tmp16;
411                                         TWI_ACK;
412                                         break;
413                                 case 0x16: // Hall 4 MSB
414                                         tmp16 = pos4;
415                                         TWDR = tmp16>>8;
416                                         TWI_ACK;
417                                         break;
418                                 case 0x17: // Hall 4 LSB
419                                         TWDR = tmp16;
420                                         TWI_ACK;
421                                         break;
422                                 case 0x20: // Motor 1 speed wish MSB
423                                         TWDR = speed1_wish>>8;
424                                         TWI_ACK;
425                                         break;
426                                 case 0x21: // Motor 1 speed wish LSB
427                                         TWDR = speed1_wish;
428                                         TWI_ACK;
429                                         break;
430                                 case 0x22: // Motor 2 speed wish MSB
431                                         TWDR = speed2_wish>>8;
432                                         TWI_ACK;
433                                         break;
434                                 case 0x23: // Motor 2 speed wish LSB
435                                         TWDR = speed2_wish;
436                                         TWI_ACK;
437                                         break;
438                                 case 0x24: // Motor 3 speed wish MSB
439                                         TWDR = speed3_wish>>8;
440                                         TWI_ACK;
441                                         break;
442                                 case 0x25: // Motor 3 speed wish LSB
443                                         TWDR = speed3_wish;
444                                         TWI_ACK;
445                                         break;
446                                 case 0x26: // Motor 4 speed wish MSB
447                                         TWDR = speed4_wish>>8;
448                                         TWI_ACK;
449                                         break;
450                                 case 0x27: // Motor 4 speed wish LSB
451                                         TWDR = speed4_wish;
452                                         TWI_ACK;
453                                         break;
454                                 case 0x30: // Motor 1 speed MSB
455                                         TWDR = speed1>>8;
456                                         TWI_ACK;
457                                         break;
458                                 case 0x31: // Motor 1 speed LSB
459                                         TWDR = speed1;
460                                         TWI_ACK;
461                                         break;
462                                 case 0x32: // Motor 2 speed MSB
463                                         TWDR = speed2>>8;
464                                         TWI_ACK;
465                                         break;
466                                 case 0x33: // Motor 2 speed LSB
467                                         TWDR = speed2;
468                                         TWI_ACK;
469                                         break;
470                                 case 0x34: // Motor 3 speed MSB
471                                         TWDR = speed3>>8;
472                                         TWI_ACK;
473                                         break;
474                                 case 0x35: // Motor 3 speed LSB
475                                         TWDR = speed3;
476                                         TWI_ACK;
477                                         break;
478                                 case 0x36: // Motor 4 speed MSB
479                                         TWDR = speed4>>8;
480                                         TWI_ACK;
481                                         break;
482                                 case 0x37: // Motor 4 speed LSB
483                                         TWDR = speed4;
484                                         TWI_ACK;
485                                         break;
486                                 case 0x38: // speed MSB
487                                         tmp_speed.f = cur_speed_lin;
488                                         TWDR = tmp_speed.i>>24;
489                                         TWI_ACK;
490                                         break;
491                                 case 0x39: // speed
492                                         TWDR = tmp_speed.i>>16;
493                                         TWI_ACK;
494                                         break;
495                                 case 0x3A: // speed
496                                         TWDR = tmp_speed.i>>8;
497                                         TWI_ACK;
498                                         break;
499                                 case 0x3B: // speed LSB
500                                         TWDR = tmp_speed.i;
501                                         TWI_ACK;
502                                         break;
503                                 case 0x3C: // angle MSB
504                                         tmp_angle.f = cur_speed_rot;
505                                         TWDR = tmp_angle.i>>24;
506                                         TWI_ACK;
507                                         break;
508                                 case 0x3D: // angle
509                                         TWDR = tmp_angle.i>>16;
510                                         TWI_ACK;
511                                         break;
512                                 case 0x3E: // angle
513                                         TWDR = tmp_angle.i>>8;
514                                         TWI_ACK;
515                                         break;
516                                 case 0x3F: // angle LSB
517                                         TWDR = angle.i;
518                                         TWI_ACK;
519                                         break;
520                                 case 0x40: // Position x MSB
521                                         TWDR = pos_x.i>>24;
522                                         TWI_ACK;
523                                         break;
524                                 case 0x41: // Position x
525                                         TWDR = pos_x.i>>16;
526                                         TWI_ACK;
527                                         break;
528                                 case 0x42: // Position x
529                                         TWDR = pos_x.i>>8;
530                                         TWI_ACK;
531                                         break;
532                                 case 0x43: // Position x LSB
533                                         TWDR = pos_x.i;
534                                         TWI_ACK;
535                                         break;
536                                 case 0x44: // Position y MSB
537                                         TWDR = pos_y.i>>24;
538                                         TWI_ACK;
539                                         break;
540                                 case 0x45: // Position y
541                                         TWDR = pos_y.i>>16;
542                                         TWI_ACK;
543                                         break;
544                                 case 0x46: // Position y
545                                         TWDR = pos_y.i>>8;
546                                         TWI_ACK;
547                                         break;
548                                 case 0x47: // Position y LSB
549                                         TWDR = pos_y.i;
550                                         TWI_ACK;
551                                         break;
552                                 case 0x48: // Position angle MSB
553                                         TWDR = angle.i>>24;
554                                         TWI_ACK;
555                                         break;
556                                 case 0x49: // Position angle
557                                         TWDR = angle.i>>16;
558                                         TWI_ACK;
559                                         break;
560                                 case 0x4A: // Position angle
561                                         TWDR = angle.i>>8;
562                                         TWI_ACK;
563                                         break;
564                                 case 0x4B: // Position angle LSB
565                                         TWDR = angle.i;
566                                         TWI_ACK;
567                                         break;
568                                 case 0xA0: // Reset reason
569                                         TWDR = MCUCSR & 0x0f;
570                                         MCUCSR = 0x0;
571                                         TWI_ACK;
572                                         break;
573                                 case 0xA1: // TLE Error status
574                                         TWDR = error_state;
575                                         TWI_ACK;
576                                         break;
577                                 case 0xA2: // count test
578                                         TWDR = count_test;
579                                         TWI_ACK;
580                                 default:
581                                         TWDR = 0;
582                                         TWI_NAK;
583                         }
584                         ireg++;
585                         break;
586                 default:
587                         TWI_RESET;
588         }
589 }
590
591
592 static void update_hall1(void) {
593         unsigned char status = (PINA >> 0) & 0x3;
594         static unsigned char oldstatus=0;
595         unsigned char diff, new;
596
597         new = 0;
598         if (status & 0x1)
599                 new = 0x3;
600         if (status & 0x2)
601                 new ^= 0x1;                                     // convert gray to binary
602         diff = oldstatus - new;                         // difference last - new
603         if (diff & 0x1) {                               // bit 0 = value (1)
604                 oldstatus = new;                                        // store new as next last
605                 if (motor1_switch) pos1 += (diff & 2) - 1;              // bit 1 = direction (+/-)
606                 else pos1 -= (diff & 2) - 1;
607         }
608 }
609
610
611 static void update_hall2(void) {
612         unsigned char status = (PINA >> 4) & 0x3;
613         static unsigned char oldstatus=0;
614         unsigned char diff, new;
615
616         new = 0;
617         if (status & 0x1)
618                 new = 0x3;
619         if (status & 0x2)
620                 new ^= 0x1;                                     // convert gray to binary
621         diff = oldstatus - new;                         // difference last - new
622         if (diff & 0x1) {                               // bit 0 = value (1)
623                 oldstatus = new;                                        // store new as next last
624                 if (motor2_switch) pos2 -= (diff & 2) - 1;              // bit 1 = direction (+/-)
625                 else pos2 += (diff & 2) - 1;
626         }
627 }
628
629
630 static void update_hall3(void) {
631         unsigned char status = (PINA >> 2) & 0x3;
632         static unsigned char oldstatus=0;
633         unsigned char diff, new;
634
635         new = 0;
636         if (status & 0x1)
637                 new = 0x3;
638         if (status & 0x2)
639                 new ^= 0x1;                                     // convert gray to binary
640         diff = oldstatus - new;                         // difference last - new
641         if (diff & 0x1) {                               // bit 0 = value (1)
642                 oldstatus = new;                                        // store new as next last
643                 if (motor3_switch) pos3 -= (diff & 2) - 1;              // bit 1 = direction (+/-)
644                 else pos3 += (diff & 2) - 1;
645         }
646 }
647
648
649 static void update_hall4(void) {
650         unsigned char status = (PINA >> 6) & 0x3;
651         static unsigned char oldstatus=0;
652         unsigned char diff, new;
653
654         new = 0;
655         if (status & 0x1)
656                 new = 0x3;
657         if (status & 0x2)
658                 new ^= 0x1;                                     // convert gray to binary
659         diff = oldstatus - new;                         // difference last - new
660         if (diff & 0x1) {                               // bit 0 = value (1)
661                 oldstatus = new;                                        // store new as next last
662                 if (motor4_switch) pos4 += (diff & 2) - 1;              // bit 1 = direction (+/-)
663                 else pos4 -= (diff & 2) - 1;
664         }
665 }
666
667
668 static void update_motor(void) {
669         static int16_t m1_old=SHRT_MIN;
670         static int16_t m2_old=SHRT_MIN;
671         static int16_t m3_old=SHRT_MIN;
672         static int16_t m4_old=SHRT_MIN;
673
674         error_state = ~((PIND & 0x40)>>3 | (PINB & 0x07)) & 0xf;
675
676         // if error and running: stop
677         if (bit_is_set(error_state, 0) && m1_old != 0) motor1 = 0;
678         if (bit_is_set(error_state, 1) && m2_old != 0) motor2 = 0;
679         if (bit_is_set(error_state, 2) && m3_old != 0) motor3 = 0;
680         if (bit_is_set(error_state, 3) && m4_old != 0) motor4 = 0;
681
682         // if we start motor in error state: start with full power
683         if (bit_is_set(error_state, 0) && m1_old == 0 && motor1 != 0) motor1 = 255;
684         if (bit_is_set(error_state, 1) && m2_old == 0 && motor2 != 0) motor2 = 255;
685         if (bit_is_set(error_state, 2) && m3_old == 0 && motor3 != 0) motor3 = 255;
686         if (bit_is_set(error_state, 3) && m4_old == 0 && motor4 != 0) motor4 = 255;
687
688         if (m1_old != motor1) { // update only when changed
689                 if (motor1 == 0) {
690                         // stop
691                         PORTC |= (1 << 3) | (1 << 2);
692                 } else if ((!motor1_switch && motor1 > 0) || (motor1_switch && motor1 < 0)) {
693                         // forward
694                         PORTC &= ~(1 << 3) & ~(1 << 2);
695                 } else { // motor1 < 0
696                         // backward
697                         PORTC &= ~(1 << 2);
698                         PORTC |=  (1 << 3);
699                 }
700
701                 m1_old = motor1;
702                 OCR1A = abs(motor1);
703         }
704
705         if (m2_old != motor2) { // update only when changed
706                 if (motor2 == 0) {
707                         // stop
708                         PORTC |= (1 << 5) | (1 << 4);
709                 } else if ((!motor2_switch && motor2 > 0) || (motor2_switch && motor2 < 0)) {
710                         // forward
711                         PORTC &= ~(1 << 5) & ~(1 << 4);
712                 } else { // motor2 < 0
713                         // backward
714                         PORTC &= ~(1 << 4);
715                         PORTC |=  (1 << 5);
716                 }
717
718                 m2_old = motor2;
719                 OCR1B = abs(motor2);
720         }
721
722         if (m3_old != motor3) { // update only when changed
723                 if (motor3 == 0) {
724                         // stop
725                         PORTC |= (1 << 7) | (1 << 6);
726                 } else if ((!motor3_switch && motor3 > 0) || (motor3_switch && motor3 < 0)) {
727                         // forward
728                         PORTC &= ~(1 << 7) & ~(1 << 6);
729                 } else { // motor3 < 0
730                         // backward
731                         PORTC &= ~(1 << 6);
732                         PORTC |=  (1 << 7);
733                 }
734
735                 m3_old = motor3;
736                 OCR2 = abs(motor3);
737         }
738
739         if (m4_old != motor4) { // update only when changed
740                 if (motor4 == 0) {
741                         // stop
742                         PORTD |= (1 << 3) | (1 << 2);
743                 } else if ((!motor4_switch && motor4 > 0) || (motor4_switch && motor4 < 0)) {
744                         // forward
745                         PORTD &= ~(1 << 3) & ~(1 << 2);
746                 } else { // motor4 < 0
747                         // backward
748                         PORTD &= ~(1 << 2);
749                         PORTD |=  (1 << 3);
750                 }
751
752                 m4_old = motor4;
753                 OCR0 = abs(motor4);
754         }
755 }
756
757
758 static void update_pos(void) {
759         static int16_t pos1_last=0;
760         static int16_t pos2_last=0;
761         static int16_t pos3_last=0;
762         static int16_t pos4_last=0;
763         int16_t pos1_diff; // steps
764         int16_t pos2_diff;
765         int16_t pos3_diff;
766         int16_t pos4_diff;
767         float diff_left_m, diff_right_m, angle_diff, translation;
768         float pos_x_new, pos_y_new, angle_new;
769         int16_t speed_l, speed_r;
770         float tmp_speed_lin, tmp_speed_rot;
771         int16_t cur_pos1, cur_pos2, cur_pos3, cur_pos4;
772         int16_t new_speed1, new_speed2, new_speed3, new_speed4;
773
774         // copy to tmp
775         cli();
776         cur_pos1 = pos1;
777         cur_pos2 = pos2;
778         cur_pos3 = pos3;
779         cur_pos4 = pos4;
780         sei();
781
782         pos1_diff = cur_pos1 - pos1_last;
783         pos2_diff = cur_pos2 - pos2_last;
784         pos3_diff = cur_pos3 - pos3_last;
785         pos4_diff = cur_pos4 - pos4_last;
786
787         new_speed1 = pos1_diff/PID_T;
788         new_speed2 = pos2_diff/PID_T;
789         new_speed3 = pos3_diff/PID_T;
790         new_speed4 = pos4_diff/PID_T;
791
792         diff_left_m = (pos1_diff + pos2_diff)/(2*STEP_PER_M_LEFT);
793         diff_right_m = (pos3_diff + pos4_diff)/(2*STEP_PER_M_RIGHT);
794         angle_diff = (diff_right_m - diff_left_m) / WHEEL_DIST;
795
796         angle_new = angle.f + angle_diff;
797         if (angle_new > 2*M_PI) angle_new-=2*M_PI;
798         else if (angle_new < -2*M_PI) angle_new+=2*M_PI;
799
800         translation = (diff_left_m + diff_right_m)/2.0;
801         pos_x_new = pos_x.f + cos(angle_new)*translation;
802         pos_y_new = pos_y.f + sin(angle_new)*translation;
803
804         speed_l = (new_speed1+new_speed2)/2;
805         speed_r = (new_speed3+new_speed4)/2;
806         tmp_speed_lin = (speed_l + speed_r)/(2.0*STEP_PER_M_AVG);
807         tmp_speed_rot = (speed_r - speed_l)/(M_PI*WHEEL_DIST*STEP_PER_M_AVG);
808
809         // copy from tmp
810         cli();
811         angle.f = angle_new;
812         pos_x.f = pos_x_new;
813         pos_y.f = pos_y_new;
814         speed1 = new_speed1;
815         speed2 = new_speed2;
816         speed3 = new_speed3;
817         speed4 = new_speed4;
818         cur_speed_lin = tmp_speed_lin;
819         cur_speed_rot = tmp_speed_rot;
820         sei();
821
822         pos1_last = cur_pos1;
823         pos2_last = cur_pos2;
824         pos3_last = cur_pos3;
825         pos4_last = cur_pos4;
826 }
827
828
829 static void update_pid(void) {
830         static int16_t eold1=0;
831         static int16_t eold2=0;
832         static int16_t eold3=0;
833         static int16_t eold4=0;
834         static int32_t esum1=0;
835         static int32_t esum2=0;
836         static int32_t esum3=0;
837         static int32_t esum4=0;
838
839         if (motor1_mode == MOTOR_PID) {
840                 if (speed1_wish == 0) {
841                         motor1 = 0;
842                         eold1 = 0;
843                         esum1 = 0;
844                 } else {
845                         int16_t e = speed1_wish - speed1;
846                         esum1+=e;
847                         motor1 += KP*e + KI*PID_T*esum1 + KD/PID_T*(e - eold1);
848                         eold1 = e;
849
850                         if (motor1 > 255) motor1 = 255;
851                         else if (motor1 < -255) motor1 = -255;
852                 }
853         }
854         if (motor2_mode == MOTOR_PID) {
855                 if (speed2_wish == 0) {
856                         motor2 = 0;
857                         eold2 = 0;
858                         esum2 = 0;
859                 } else {
860                         int16_t e = speed2_wish - speed2;
861                         esum2+=e;
862                         motor2 += KP*e + KI*PID_T*esum2 + KD/PID_T*(e - eold2);
863                         eold2 = e;
864
865                         if (motor2 > 255) motor2 = 255;
866                         else if (motor2 < -255) motor2 = -255;
867                 }
868         }
869         if (motor3_mode == MOTOR_PID) {
870                 if (speed3_wish == 0) {
871                         motor3 = 0;
872                         eold3 = 0;
873                         esum3 = 0;
874                 } else {
875                         int16_t e = speed3_wish - speed3;
876                         esum3+=e;
877                         motor3 += KP*e + KI*PID_T*esum3 + KD/PID_T*(e - eold3);
878                         eold3 = e;
879
880                         if (motor3 > 255) motor3 = 255;
881                         else if (motor3 < -255) motor3 = -255;
882                 }
883         }
884         if (motor4_mode == MOTOR_PID) {
885                 if (speed4_wish == 0) {
886                         motor4 = 0;
887                         eold4 = 0;
888                         esum4 = 0;
889                 } else {
890                         int16_t e = speed4_wish - speed4;
891                         esum4+=e;
892                         motor4 += KP*e + KI*PID_T*esum4 + KD/PID_T*(e - eold4);
893                         eold4 = e;
894
895                         if (motor4 > 255) motor4 = 255;
896                         else if (motor4 < -255) motor4 = -255;
897                 }
898         }
899 }
900
901
902 ISR(TIMER1_OVF_vect) {
903         update_hall1();
904         update_hall2();
905         update_hall3();
906         update_hall4();
907         
908         run_update++;
909 }
910
911
912 int main(void) {
913         // Outputs
914         DDRB = (1 << 3);
915         DDRC = (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
916         DDRD = (1 << 7) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
917         // Pullup TLEs EF
918         PORTB = (1 << 0) | (1 << 1) | (1 << 2);
919         PORTD = (1 << 6);
920
921         bootloader = 0x00;
922         setup_uart(9600);
923         uart_setup_stdout();
924
925         // I2C
926         TWAR = 0x50;
927         TWI_RESET;
928
929         // Motor 1 & 2
930         // Timer 1: Fast PWM inverting mode, Top=256 => 15.625kHz
931         // Prescaler=1
932         TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << COM1A0) | (1 << COM1B0) | (1 << WGM10);
933         TCCR1B = (1 << WGM12) | (1 << CS10);
934         OCR1A = 0;
935         OCR1B = 0;
936
937         // Motor 3
938         // Timer 2: Fast PWM inverting mode, Top=256
939         // Prescaler=1
940         TCCR2 = (1 << WGM21) | (1 << WGM20) | (1 << COM21) | (1 << COM20) | (1 << CS20);
941         OCR2 = 0;
942
943         // Motor 4
944         // Timer 0: Fast PWM inverting mode, Top=256
945         // Prescaler=1
946         TCCR0 = (1 << WGM01) | (1 << WGM00) | (1 << COM01) | (1 << COM00) | (1 << CS00);
947         OCR0 = 0;
948
949         printf("\r\nStart\r\n");
950
951         set_sleep_mode(SLEEP_MODE_IDLE);
952         // Enable Timer 1 Overflow Interrupt
953         TIMSK = (1 << TOIE1);
954         sei();
955
956         while(1) {
957                 switch(ireg) {
958                         case 0xff: // Magic reg that starts the bootloader
959                                 if (bootloader == 0xa5) {
960                                         cli();
961                                         {
962                                                 void (*start)(void) = (void*)0x1800;
963                                                 start();
964                                         }
965                                 }
966                                 break;
967                 }
968
969                 if (cmd_vel.bUpdate) {
970                         float speed_wish_right, speed_wish_left;
971                         float speed, angle;
972
973                         cli();
974                         speed = cmd_vel.speed;
975                         angle = cmd_vel.angle;
976                         cmd_vel.bUpdate = 0;
977                         sei();
978
979                         speed_wish_right = angle*M_PI*WHEEL_DIST/2 + speed;
980                         speed_wish_left = speed*2-speed_wish_right;
981
982                         speed_wish_left*=STEP_PER_M_LEFT;
983                         speed_wish_right*=STEP_PER_M_RIGHT;
984
985                         if (aft_handicap > 0) {
986                                 speed1_wish = speed_wish_left * (100-aft_handicap)/100.0;
987                                 speed4_wish = speed_wish_right * (100-aft_handicap)/100.0;
988                         } else {
989                                 speed1_wish = speed_wish_left;
990                                 speed4_wish = speed_wish_right;
991                         }
992                         if (front_handicap > 0) {
993                                 speed2_wish = speed_wish_left * (100-front_handicap)/100.0;
994                                 speed3_wish = speed_wish_right * (100-front_handicap)/100.0;
995                         } else {
996                                 speed2_wish = speed_wish_left;
997                                 speed3_wish = speed_wish_right;
998                         }
999                         motor1_mode = MOTOR_PID;
1000                         motor2_mode = MOTOR_PID;
1001                         motor3_mode = MOTOR_PID;
1002                         motor4_mode = MOTOR_PID;
1003                 }
1004
1005                 if (run_update >= 156) { // ~100Hz
1006                         run_update=0;
1007
1008                         update_pos();
1009                         update_pid();
1010                         update_motor();
1011                         count_test++;
1012                 }
1013
1014                 sleep_mode();
1015         }
1016
1017         return 0;
1018 }