Changeset 716

Show
Ignore:
Timestamp:
07/20/09 05:01:43 (4 years ago)
Author:
atorf
Message:

MotorControl20:

  • Improved detection of external STOP direct commands, cleaner control task exit
  • Blocking functions return true/false now to indicate whether they have been stopped externally
  • Orange emergency stop button now holds on and exits the program completely (restart / new matlab handle is required)
Location:
branches/atorf/NXC/MotorControl2
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/atorf/NXC/MotorControl2/ControllerCore.nxc

    r713 r716  
    5656 
    5757 
    58 safecall void _ADD_MOTORNAME(WaitUntilMotorStopped)(const byte &port) { 
     58safecall bool _ADD_MOTORNAME(WaitUntilMotorStopped)(const byte &port) { 
    5959/* 
     60RETURNS true if stopped by direct command 
     61 
    6062This function waits until a braked motor has come to a full stop. The "trick" is 
    6163that it's not only beeing waited until the motor has stopped for a very short 
     
    9092        }//end if 
    9193        lastPos = curPos; 
     94         
     95        // check if direct command stopped us (indicated by TachoLimit...) 
     96        if ((MotorTachoLimit(port) == 0) && (MotorPower(port) == 0)) { 
     97            //direct command must have stopped us! 
     98            #ifdef ENABLEDEBUGGING_LCD_SLOW_ANTIBUG 
     99                TextOut(0, DebugLinePos[port], "ABORTED(wait4Stop)        "); 
     100                Wait(300); 
     101            #endif 
     102            return true; 
     103        }//end if 
     104 
     105         
    92106 
    93107        if (CurrentTick() >= timeoutTick) { 
     
    96110    }//end while 
    97111 
     112    return false; 
    98113 
    99114}//end void 
     
    111126 
    112127// note how RunMotor2 doesn't need to be inline and takes 1 more argument... 
     128// RETURN is wether a direct command stopped us!!!! 
    113129#ifdef RUNMOTOR2_SYNCMODE 
    114 void        RunMotor2(const byte &port, const int &power, const long &tacholimit, const bool &speedreg, const bool &holdbrake, const bool &smoothstart, const byte &port2) { 
     130bool        RunMotor2(const byte &port, const int &power, const long &tacholimit, const bool &speedreg, const bool &holdbrake, const bool &smoothstart, const byte &port2) { 
    115131#else 
    116 inline void _ADD_MOTORNAME(RunMotor) (const byte &port, const int &power, const long &tacholimit, const bool &speedreg, const bool &holdbrake, const bool &smoothstart) { 
     132inline bool _ADD_MOTORNAME(RunMotor) (const byte &port, const int &power, const long &tacholimit, const bool &speedreg, const bool &holdbrake, const bool &smoothstart) { 
    117133#endif 
    118134 
     
    371387                Wait(300); 
    372388            #endif 
    373             return; 
     389            return true; 
    374390        }//end if 
    375391         
     
    702718                    Wait(300); 
    703719                #endif 
    704                 return; 
     720                return true; 
    705721            }//end if 
    706722             
     
    753769                        break; 
    754770                    }//end if 
     771                    // even for syncmode, only check first motor... 
     772                    if (MotorTachoLimit(port) == 0) { 
     773                        //direct command must have stopped us! 
     774                        #ifdef ENABLEDEBUGGING_LCD_SLOW_ANTIBUG 
     775                            TextOut(0, DebugLinePos[port], "ABORTED(endgame)        "); 
     776                            Wait(300); 
     777                        #endif 
     778                        return true; 
     779                    }//end if 
    755780                    #ifdef ENABLEDEBUGGING_LCD 
    756781                        endgameLoopCount++; 
     
    768793                        break; 
    769794                    }//end if 
     795                    // even for syncmode, only check first motor... 
     796                    if (MotorTachoLimit(port) == 0) { 
     797                        //direct command must have stopped us! 
     798                        #ifdef ENABLEDEBUGGING_LCD_SLOW_ANTIBUG 
     799                            TextOut(0, DebugLinePos[port], "ABORTED(endgame)        "); 
     800                            Wait(300); 
     801                        #endif 
     802                        return true; 
     803                    }//end if 
     804 
    770805                    #ifdef ENABLEDEBUGGING_LCD 
    771806                        endgameLoopCount++; 
     
    833868    #endif 
    834869 
    835     _ADD_MOTORNAME(WaitUntilMotorStopped)(port); 
     870    bool stoppedByDirectCmd = false; 
     871    stoppedByDirectCmd = _ADD_MOTORNAME(WaitUntilMotorStopped)(port); 
    836872 
    837873 
     
    900936        TextOut(0, DebugLinePos[port], "FINISHED        "); 
    901937    #endif 
     938     
     939     // return wether a direct command stopped us... 
     940    return stoppedByDirectCmd; 
     941     
    902942 
    903943}//end void 
  • branches/atorf/NXC/MotorControl2/MotorControl20.nxc

    r711 r716  
    148148    taskArunning = true; 
    149149     
    150     RunMotorA(OUT_A, motorParamsA.power, motorParamsA.tacholimit, motorParamsA.speedreg, motorParamsA.holdbrake, motorParamsA.smoothstart); 
     150    bool stoppedByDirectCmd; 
     151    stoppedByDirectCmd = RunMotorA(OUT_A, motorParamsA.power, motorParamsA.tacholimit, motorParamsA.speedreg, motorParamsA.holdbrake, motorParamsA.smoothstart); 
     152     
     153    // if we exited from external NXTMotor.Stop command, we might've overwritten 
     154    // the power value before exiting the main controller loop, so restore defined 
     155    // end state here again: 
     156    if (stoppedByDirectCmd) { 
     157        if (MotorRegulation(OUT_A) == OUT_REGMODE_SPEED) { 
     158            MotorBrake(OUT_A); 
     159        } else { 
     160            MotorOff(OUT_A); 
     161        }//end if 
     162    }//end if 
    151163     
    152164    taskArunning = false; 
     
    167179    #endif 
    168180 
    169     RunMotorB(OUT_B, motorParamsB.power, motorParamsB.tacholimit, motorParamsB.speedreg, motorParamsB.holdbrake, motorParamsB.smoothstart); 
    170  
     181    bool stoppedByDirectCmd; 
     182    stoppedByDirectCmd = RunMotorB(OUT_B, motorParamsB.power, motorParamsB.tacholimit, motorParamsB.speedreg, motorParamsB.holdbrake, motorParamsB.smoothstart); 
     183 
     184    // if we exited from external NXTMotor.Stop command, we might've overwritten 
     185    // the power value before exiting the main controller loop, so restore defined 
     186    // end state here again: 
     187    if (stoppedByDirectCmd) { 
     188        if (MotorRegulation(OUT_B) == OUT_REGMODE_SPEED) { 
     189            MotorBrake(OUT_B); 
     190        } else { 
     191            MotorOff(OUT_B); 
     192        }//end if 
     193    }//end if 
    171194 
    172195    #ifdef ENABLEDEBUGGING_OLDLCDTIMING 
     
    199222    taskCrunning = true; 
    200223 
    201     RunMotorC(OUT_C, motorParamsC.power, motorParamsC.tacholimit, motorParamsC.speedreg, motorParamsC.holdbrake, motorParamsC.smoothstart); 
    202  
     224    bool stoppedByDirectCmd; 
     225    stoppedByDirectCmd = RunMotorC(OUT_C, motorParamsC.power, motorParamsC.tacholimit, motorParamsC.speedreg, motorParamsC.holdbrake, motorParamsC.smoothstart); 
     226     
     227    // if we exited from external NXTMotor.Stop command, we might've overwritten 
     228    // the power value before exiting the main controller loop, so restore defined 
     229    // end state here again: 
     230    if (stoppedByDirectCmd) { 
     231        if (MotorRegulation(OUT_C) == OUT_REGMODE_SPEED) { 
     232            MotorBrake(OUT_C); 
     233        } else { 
     234            MotorOff(OUT_C); 
     235        }//end if 
     236    }//end if 
     237     
    203238    taskCrunning = false; 
    204239 
     
    213248        taskSyncRunning = true; 
    214249    #endif 
     250     
     251    bool stoppedByDirectCmd; 
    215252 
    216253    if (SyncPorts == 3) { // OUT_AB 
     
    219256            taskArunning = true; 
    220257            taskBrunning = true; 
    221             RunMotor2(OUT_A, motorParamsSync.power, motorParamsSync.tacholimit, false, motorParamsSync.holdbrake, motorParamsSync.smoothstart, OUT_B); 
     258            stoppedByDirectCmd = RunMotor2(OUT_A, motorParamsSync.power, motorParamsSync.tacholimit, false, motorParamsSync.holdbrake, motorParamsSync.smoothstart, OUT_B); 
     259            if (stoppedByDirectCmd) { 
     260                if (MotorRegulation(OUT_A) == OUT_REGMODE_SPEED) { 
     261                    MotorBrake2(OUT_A, OUT_B); 
     262                } else { 
     263                    MotorOff2(OUT_A, OUT_B); 
     264                }//end if 
     265            }//end if 
    222266            taskArunning = false; 
    223267            taskBrunning = false; 
     
    229273            taskArunning = true; 
    230274            taskCrunning = true; 
    231             RunMotor2(OUT_A, motorParamsSync.power, motorParamsSync.tacholimit, false, motorParamsSync.holdbrake, motorParamsSync.smoothstart, OUT_C); 
     275            stoppedByDirectCmd = RunMotor2(OUT_A, motorParamsSync.power, motorParamsSync.tacholimit, false, motorParamsSync.holdbrake, motorParamsSync.smoothstart, OUT_C); 
     276            if (stoppedByDirectCmd) { 
     277                if (MotorRegulation(OUT_A) == OUT_REGMODE_SPEED) { 
     278                    MotorBrake2(OUT_A, OUT_C); 
     279                } else { 
     280                    MotorOff2(OUT_A, OUT_C); 
     281                }//end if 
     282            }//end if 
    232283            taskArunning = false; 
    233284            taskCrunning = false; 
     
    239290            taskBrunning = true; 
    240291            taskCrunning = true; 
    241             RunMotor2(OUT_B, motorParamsSync.power, motorParamsSync.tacholimit, false, motorParamsSync.holdbrake, motorParamsSync.smoothstart, OUT_C); 
     292            stoppedByDirectCmd = RunMotor2(OUT_B, motorParamsSync.power, motorParamsSync.tacholimit, false, motorParamsSync.holdbrake, motorParamsSync.smoothstart, OUT_C); 
     293            if (stoppedByDirectCmd) { 
     294                if (MotorRegulation(OUT_B) == OUT_REGMODE_SPEED) { 
     295                    MotorBrake2(OUT_B, OUT_C); 
     296                } else { 
     297                    MotorOff2(OUT_B, OUT_C); 
     298                }//end if 
     299            }//end if 
    242300            taskBrunning = false; 
    243301            taskCrunning = false; 
     
    767825            MotorOff(OUT_B); 
    768826            MotorOff(OUT_C); 
    769             PlayTone(440, 100); 
     827            PlayTone(440, 500); 
    770828            Wait(500); 
     829             
     830             
     831            TextOut(5,LCD_LINE7, "EMERGENCY STOP    ", false); 
     832            TextOut(2,LCD_LINE8, "Restart program!  ", false); 
     833            Wait(2000); 
     834            StopAllTasks(); 
     835             
     836            /* 
    771837            // keep 
    772838            while(ButtonPressed(BTNCENTER, false)) { 
     
    777843                Wait(500); 
    778844            }//end while 
     845             
     846            // purge incoming queue... 
     847            in = "..."; 
     848            while(StrLen(in) > 0) { 
     849                ReceiveRemoteString(INBOX, true, in); 
     850            }//end while 
     851            in = ""; 
     852             
     853            // manually reset task-semaphores 
     854            // not very clean, but this button is for emergencies anyway... 
     855            taskArunning = false; 
     856            taskBrunning = false; 
     857            taskCrunning = false; 
     858             
     859            // reset counters for a nice clean start 
     860            ResetErrorCorrectionAndBlockCount(OUT_A); 
     861            ResetErrorCorrectionAndBlockCount(OUT_B); 
     862            ResetErrorCorrectionAndBlockCount(OUT_C); 
     863            */ 
    779864        }//end if 
    780865