root/branches/atorf/NXC/TestRampMotorControl.nxc @ 535

Revision 535, 4.9 KB (checked in by atorf, 4 years ago)
  • Continued testing motor ramps
Line 
1safecall 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
17safecall 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
33safecall 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...
58safecall 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
73void 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
82task main(){
83
84    int accel = 50;
85    int decel = 350;
86    byte minpower = 20;
87
88    int distance = 1000;
89   
90    byte port = OUT_B;
91    byte power = 100;
92    bool speedreg = false;
93   
94    bool smoothStart = false;
95   
96   
97
98
99    TextOut(0,LCD_LINE1, "", true);
100   
101   
102    MotorOff(port);
103    ResetErrorCorrectionAndBlockCount(port);
104   
105    Wait(1200);
106   
107
108// **** precalc
109    int accelStartPower;
110    if (power > minpower) {
111        smoothStart = true;
112        accelStartPower = power / 2;
113        if (accelStartPower < minpower) {
114            accelStartPower = minpower;
115        }
116    } else { // for smaller values do the "classic" thing
117        accelStartPower = power;
118    }
119
120// **** Acceleration
121    if (smoothStart) {
122        MotorCmdEx(port, accelStartPower, accel, 0, speedreg, false, OUT_RUNSTATE_RUNNING);
123        MotorCmdEx(port, power, accel, 0, speedreg, false, OUT_RUNSTATE_RAMPUP);
124    } else {
125        // improve this by merging this running step to the next one...
126        MotorCmdEx(port, power, accel, 0, speedreg, false, OUT_RUNSTATE_RUNNING);
127    }
128
129
130    //TextOut(0,LCD_LINE1, "Accel...");
131    while(MotorTachoCount(port) < accel) {
132        // do nothing
133        //DebugPrintPower(port);
134    }//end while
135
136
137// **** Running
138    MotorCmdEx(port, power, distance - accel - decel, 0, speedreg, false, OUT_RUNSTATE_RUNNING);
139   
140    //TextOut(0,LCD_LINE1, "Running...");
141    while(MotorTachoCount(port) < distance - decel) {
142        // do nothing
143    }//end while
144
145// **** Deceleration
146    MotorCmdEx(port, 0, decel, 0, speedreg, false, OUT_RUNSTATE_RAMPDOWN);
147   
148    //TextOut(0,LCD_LINE1, "Decel...");
149    while(MotorTachoCount(port) < distance) {
150        // do nothing
151        //DebugPrintPower(port);
152    }//end while
153   
154    MotorBrake(port);
155
156    //TextOut(0,LCD_LINE1, "Braked...");
157    Wait(1000);
158   
159    MotorOff(port);
160
161
162    string tmp = "";
163   
164    tmp = NumToStr(distance);
165    tmp = StrCat("goal=", tmp);
166    TextOut(0,LCD_LINE4, tmp);
167   
168    tmp = NumToStr(MotorTachoCount(port));
169    tmp = StrCat("real=", tmp);
170    TextOut(0,LCD_LINE5, tmp);
171   
172    Wait(3000);
173
174}
Note: See TracBrowser for help on using the browser.