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