Code: Select all
// Ramp up for a motor.
void RampUp(byte output,
char pwr,
long angle, // Number of degrees to do the rampup.
bool UseSpeedControl = true)
{
char power = sign(angle);
angle = abs(angle);
power *= pwr;
byte om = OUT_MODE_MOTORON|OUT_MODE_BRAKE;
byte rm = OUT_REGMODE_IDLE;
if (UseSpeedControl) {
om += OUT_MODE_REGULATED;
rm = OUT_REGMODE_SPEED;
}
// we want to rotate a total of <angle> degrees
// we'll rampup from 0 power to specified power
SetOutput(output, OutputModeField, om,
TachoLimitField, angle,
PowerField, power,
RegModeField, rm,
RunStateField, OUT_RUNSTATE_RAMPUP,
UpdateFlagsField, UF_UPDATE_MODE|UF_UPDATE_SPEED|UF_UPDATE_TACHO_LIMIT);
Yield(); // give firmware a chance to process this request to update motor state
// monitor runstate
while(MotorRunState(output) <> OUT_RUNSTATE_IDLE)
Yield();
// as soon as it goes idle put the motor into the running state
OnFwd(output, power);
}
task RampB()
{
RampUp(OUT_B, 50,1800);
}
task main()
{
SetSensorTouch(S4);
StartTask(RampB);
until(SensorBoolean(S4)) Yield();
StopTask(RampB);
Off(OUT_B);
}