Micros() returning bad data in interrupt


i'm developing code motor controller we've constructed scratch. in order motor controller operate correctly, need calculate realtime velocity of motor. done recording time between ticks of encoders.

during operation, velocity seems on place. narrowed down issue times being returned micros() function in interrupt being triggered encoders.

in effort debug, created following code output of times being recorded when interrupt fired.

code: [select]

#define max_power 255

//
// pin definitions
//
#define encoder_a 2
#define encoder_b 3

#define motor_current_1 a0
#define motor_current_2 a1
#define motor_voltage   a2

#define motor_direction 10
#define motor_pwm       11
#define motor_fault     12
#define motor_enable    13

//
// buffer
//
volatile uint16_t = 0;
const uint16_t max_records = 200;
volatile unsigned long time[max_records];

void setup(){
  serial.begin(115200);

  // pin setup
  pinmode(motor_direction, output);
  pinmode(motor_pwm      , output);
  pinmode(motor_enable   , output);
  pinmode(motor_fault    , input );
  pinmode(encoder_a      , input );
  pinmode(encoder_b      , input );

  attachinterrupt(0, tick, rising);
 
  digitalwrite(motor_enable, high);
  analogwrite(motor_pwm, max_power);
}

void tick(){
  if(i == max_records) return;

  time[i++] = micros();
}

void loop(){
  if(i == max_records){
    while(i--) serial.println(time[i]);
  }
}


below snippet of resulting output. you'll notice that, though times trending upwards, it's not straight linear progression. i'm using latest version of arduino ide (1.0.5) on osx 10.8.4. idea what's going on here, or, more importantly, how fix it? suffice say, extremely frustrating.

code: [select]

12696792
12695156
12693524
12691880
12698436
12690224
12688580
12700092
12686940
12685308
12683664
12701736
12682008
12680364
12703368
12678728
12705004
12677096
12706648
12675452
12708308
12673796
12709948
12672152
12711580
12670516
12713216
12668880
12714860
12667240
12716516
12665580
12718160
12663936
12719792
12662300
12721428
12723076
12660668
12724732
12659024
12726376
12657368
12728008
12655724
12729644
12654084
12731288
12652452
12732944
12650808
12734588
12649152
12736220
12647508
12737856
12645872
12739504
12644240
12741160
12642596
12742804
12640940
12744436
12639296
12746072
12747716
12637660
12749372
12636028
12751016
12634384
12752648
12632728
12754284
12631080
12755932
12629444
12757588
12627812
12759232
12626168
12760864
12624512
12762500
12622868
12764144
12621232
12765800
12619600
12767444
12617956
12769076
12616300
12770716
12772364
12614656
12774016
12613016
12775660
12611384
12777292
12609740
12778928
12608084
12780572
12606440
12782228
12604804
12783876
12603172
12785504
12601528
12787140
12599872
12788784
12598228
12790440
12596592
12792084
12594956
12793716
12795356
12593312
12797000
12591656
12798656
12590012
12800300
12588376
12801932
12586740
12803568
12585096
12805212
12583440
12806868
12581796
12808512
12580160
12810144
12578524
12811784
12576880
12813428
12575224
12815084
12573580
12816728
12571944
12818360
12820000
12570312
12821644
12568664
12823296
12567008
12824944
12565364
12826576
12563728
12828212
12562092
12829860
12560448
12831512
12558792
12833156
12557148
12834788
12555512
12836428
12553880
12838072
12552232
12839728
12550576
12841372
12548932
12843004
12844640
12547296
12846288
12545664
12847944
12544016
12849588
12542360
12851220
12540716
12852856
12539080
12854500
12537448
12856156
12535804
12857800
12534148
12859440
12532500
12861072
12530864
12862716
12529232
12864372
12527588
12866016
12525932
12867648
12869288
12524284
12870932
12522648
12872588
12521016
12874232
12519372
12875864
12517716
12877504
12516068
12879148
12514432
12880804
12512800
12882448
12511156
12884080
12509500
12885720
12507856
12887364
12506216
12889020
12504584
12890664
12502940
12892296
12893932
12501284
12895576
12499640
12897232
12498004
12898880
12496372
12900512
12494728
12902148
12493072
12903792
12491424
12905448
12489788
12907092
12488156
12908724
12486512
12910360
12484856
12912004
12483212
12913660
12481576
12915308
12479944
12916940
12918576
12478300
12920220
12476644
12921876
12475000
12923520
12473360
12925152
12471732
12926792
12470088
12928436
12468432
12930092
12466788
12931736
12465152
12933368
12289412
12935004
12287756
12936648
12286112
12938304
12284476
12939948
12282844
12941584
12943220
12281200
12944864
12279544
12946520
12277900
12948164
12276264
12949796
12274632
12951432
12272988
12953076
12271332
12954732
12269688
12956376
12268052
12958008
12266420
12959648
12264776
12961292
12263120
12962948
12261476
12964592
12259840
12966224
12967860
12258208
12969504
12256564
12971160
12254908
12972804
12253264
12974436
12251628
12976072
12249996
12977716
12248352
12979376
12246696
12981016

printing deltas might make easier see going on.
posting schematic might help.


Arduino Forum > Using Arduino > Programming Questions > Micros() returning bad data in interrupt


arduino

Comments

Popular posts from this blog

Rasperry Wifi LAN Controller - Raspberry Pi Forums

Convierte tu Raspberry en un NAS. Firmware fvdw-sl 15.3 - Raspberry Pi Forums

Unlock car with bluetooth and car key