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