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