| 1 | safecall void MotorBrake(byte port) { |
|---|
| 2 | // Similar to "StopMotor(port, 'brake')", enables active braking for a motor... |
|---|
| 3 | |
|---|
| 4 | //MotorCmd(port, 0, 0, 0, true, false); |
|---|
| 5 | |
|---|
| 6 | byte flags = UF_UPDATE_MODE + UF_UPDATE_SPEED; // + UF_UPDATE_RESET_COUNT; |
|---|
| 7 | |
|---|
| 8 | byte mode = OUT_MODE_BRAKE + OUT_MODE_MOTORON + OUT_MODE_REGULATED; |
|---|
| 9 | byte reg = OUT_REGMODE_SPEED; |
|---|
| 10 | byte state = OUT_RUNSTATE_RUNNING; |
|---|
| 11 | |
|---|
| 12 | SetOutput(port, Power, 0, OutputMode, mode, RegMode, reg, RunState, state, UpdateFlags, flags); |
|---|
| 13 | |
|---|
| 14 | }//end MotorBrake |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | safecall void MotorOff(byte port) { |
|---|
| 18 | // Similar to "StopMotor(port, 'off')", turns off power to a motor, enabling COAST mode... |
|---|
| 19 | |
|---|
| 20 | //MotorCmd(port, 0, 0, 0, true, false); |
|---|
| 21 | |
|---|
| 22 | byte flags = UF_UPDATE_MODE + UF_UPDATE_SPEED; // + UF_UPDATE_RESET_COUNT; |
|---|
| 23 | |
|---|
| 24 | byte mode = OUT_MODE_COAST; |
|---|
| 25 | byte reg = OUT_REGMODE_IDLE; |
|---|
| 26 | byte state = OUT_RUNSTATE_IDLE; |
|---|
| 27 | |
|---|
| 28 | SetOutput(port, Power, 0, OutputMode, mode, RegMode, reg, RunState, state, UpdateFlags, flags); |
|---|
| 29 | |
|---|
| 30 | }//end MotorOff |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | safecall void MotorCmdEx(byte port, int pwr, long tacholimit, int turnratio, bool speedreg, bool sync, byte runstate) { |
|---|
| 34 | // The actual Motor-Command we use, based on IOOutputMap settings. A bit similar to the |
|---|
| 35 | // SetOutputState direct command... |
|---|
| 36 | |
|---|
| 37 | byte flags = UF_UPDATE_MODE + UF_UPDATE_SPEED + UF_UPDATE_TACHO_LIMIT; // + UF_UPDATE_RESET_COUNT; |
|---|
| 38 | |
|---|
| 39 | byte mode = OUT_MODE_BRAKE + OUT_MODE_MOTORON; |
|---|
| 40 | byte reg = OUT_REGMODE_IDLE; |
|---|
| 41 | byte state = runstate; |
|---|
| 42 | |
|---|
| 43 | if (speedreg) { |
|---|
| 44 | mode = mode + OUT_MODE_REGULATED; |
|---|
| 45 | reg = OUT_REGMODE_SPEED; |
|---|
| 46 | }//end if |
|---|
| 47 | |
|---|
| 48 | if (sync) { |
|---|
| 49 | mode = mode + OUT_MODE_REGULATED; |
|---|
| 50 | reg = OUT_REGMODE_SYNC; |
|---|
| 51 | }//end if |
|---|
| 52 | |
|---|
| 53 | SetOutput(port, Power, pwr, TachoLimit, tacholimit, TurnRatio, turnratio, OutputMode, mode, RegMode, reg, RunState, state, UpdateFlags, flags); |
|---|
| 54 | }//end MotorCmdEx |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | // safecall! if a something were interfering, this would be dangerous... |
|---|
| 58 | safecall void ResetErrorCorrectionAndBlockCount(byte port) { |
|---|
| 59 | // Resets TachoCount of a motor, which also resets internal error correction memory. |
|---|
| 60 | // also resets BlockTachoCount with just one single call. Needed for sync driving. If not |
|---|
| 61 | // reset, bot will "re-align" itself to last turnratio, which is usually 0, so in this |
|---|
| 62 | // case, bot will turn to drive straight again. Nice feature if wanted, but usually |
|---|
| 63 | // unexpected and then looks like "dancing" or totally chaotic movements |
|---|
| 64 | |
|---|
| 65 | byte flags = UF_UPDATE_RESET_COUNT + UF_UPDATE_RESET_BLOCK_COUNT; |
|---|
| 66 | |
|---|
| 67 | SetOutput(port, UpdateFlags, flags); |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | }//end ResetErrorCorrection |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | void DebugPrintPower(byte port) { |
|---|
| 74 | string tmp; |
|---|
| 75 | tmp = NumToStr(MotorPower(port)); |
|---|
| 76 | tmp = StrCat("Power=", tmp); |
|---|
| 77 | TextOut(0,LCD_LINE2, tmp); |
|---|
| 78 | }//end |
|---|
| 79 | |
|---|
| 80 | // ------------------------------------------------- |
|---|
| 81 | // main function |
|---|
| 82 | task main(){ |
|---|
| 83 | |
|---|
| 84 | int accel = 50; |
|---|
| 85 | int decel = 270; |
|---|
| 86 | byte minpower = 20; |
|---|
| 87 | |
|---|
| 88 | int distance = 1000; |
|---|
| 89 | |
|---|
| 90 | byte port = OUT_B; |
|---|
| 91 | byte power = 100; |
|---|
| 92 | bool accSpeedreg = false; |
|---|
| 93 | bool speedreg = false; |
|---|
| 94 | bool decSpeedreg = true; |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | bool smoothStart = false; |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | TextOut(0,LCD_LINE1, "", true); |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | MotorOff(port); |
|---|
| 106 | ResetErrorCorrectionAndBlockCount(port); |
|---|
| 107 | |
|---|
| 108 | Wait(1200); |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | // **** precalc |
|---|
| 112 | int accelStartPower; |
|---|
| 113 | if (power > minpower) { |
|---|
| 114 | smoothStart = true; |
|---|
| 115 | accelStartPower = power / 2; |
|---|
| 116 | if (accelStartPower < minpower) { |
|---|
| 117 | accelStartPower = minpower; |
|---|
| 118 | } |
|---|
| 119 | } else { // for smaller values do the "classic" thing |
|---|
| 120 | accelStartPower = power; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | // **** Acceleration |
|---|
| 124 | if (smoothStart) { |
|---|
| 125 | MotorCmdEx(port, accelStartPower, accel, 0, false, false, OUT_RUNSTATE_RUNNING); |
|---|
| 126 | MotorCmdEx(port, power, accel, 0, accSpeedreg, false, OUT_RUNSTATE_RAMPUP); |
|---|
| 127 | } else { |
|---|
| 128 | // improve this by merging this running step to the next one... |
|---|
| 129 | MotorCmdEx(port, power, accel, 0, speedreg, false, OUT_RUNSTATE_RUNNING); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | //TextOut(0,LCD_LINE1, "Accel..."); |
|---|
| 134 | while(MotorTachoCount(port) < accel) { |
|---|
| 135 | // do nothing |
|---|
| 136 | //DebugPrintPower(port); |
|---|
| 137 | }//end while |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | // **** Running |
|---|
| 141 | MotorCmdEx(port, power, distance - accel - decel, 0, speedreg, false, OUT_RUNSTATE_RUNNING); |
|---|
| 142 | |
|---|
| 143 | //TextOut(0,LCD_LINE1, "Running..."); |
|---|
| 144 | while(MotorTachoCount(port) < distance - decel) { |
|---|
| 145 | // do nothing |
|---|
| 146 | }//end while |
|---|
| 147 | |
|---|
| 148 | // **** Deceleration |
|---|
| 149 | MotorCmdEx(port, 0, decel, 0, decSpeedreg, false, OUT_RUNSTATE_RAMPDOWN); |
|---|
| 150 | |
|---|
| 151 | //TextOut(0,LCD_LINE1, "Decel..."); |
|---|
| 152 | while(MotorTachoCount(port) < distance) { |
|---|
| 153 | // do nothing |
|---|
| 154 | //DebugPrintPower(port); |
|---|
| 155 | }//end while |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | //MotorBrake(port); |
|---|
| 159 | // NXT-G MoveBlock sets TachoLimit=0 for final brake, we do so too now... |
|---|
| 160 | MotorCmdEx(port, 0, 0, 0, true, false, OUT_RUNSTATE_RUNNING); |
|---|
| 161 | |
|---|
| 162 | //TextOut(0,LCD_LINE1, "Braked..."); |
|---|
| 163 | Wait(1000); |
|---|
| 164 | |
|---|
| 165 | MotorOff(port); |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | string tmp = ""; |
|---|
| 169 | |
|---|
| 170 | tmp = NumToStr(distance); |
|---|
| 171 | tmp = StrCat("goal=", tmp); |
|---|
| 172 | TextOut(0,LCD_LINE4, tmp); |
|---|
| 173 | |
|---|
| 174 | tmp = NumToStr(MotorTachoCount(port)); |
|---|
| 175 | tmp = StrCat("real=", tmp); |
|---|
| 176 | TextOut(0,LCD_LINE5, tmp); |
|---|
| 177 | |
|---|
| 178 | Wait(3000); |
|---|
| 179 | |
|---|
| 180 | } |
|---|