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