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