Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Interfacing with C plus plus-programing communication with microcontrolers (K. Bentley, 2006)

.pdf
Скачиваний:
192
Добавлен:
12.08.2013
Размер:
3.18 Mб
Скачать

8 DRIVING MOTORS - DC & STEPPER 245

class AbstractMotor

{

private:

int Speed;

public:

AbstractMotor();

void SetSpeed(int speed); int GetSpeed();

virtual void Off()=0; virtual void Forward()=0; virtual void Reverse()=0; virtual void Brake()=0; virtual ~AbstractMotor(){}

};

AbstractMotor::AbstractMotor()

{

Speed =0;

}

void AbstractMotor::SetSpeed(int speed)

{

Speed = speed;

if(Speed > 255) Speed = 255; // Limit upper value if(Speed < 0) Speed = 0; // Limit lower value

}

int AbstractMotor::GetSpeed()

{

return Speed;

}

class Motor : public AbstractMotor, public ParallelPort

{

public:

Motor(int baseaddress=0x378); void Off();

virtual void Forward()=0; virtual void Reverse()=0; virtual void Brake()=0; virtual ~Motor(){}

};

Motor::Motor(int baseaddress): ParallelPort(baseaddress)

246 8 DRIVING MOTORS - DC & STEPPER

{

Off();

}

void Motor::Off()

{

WritePort0(0x00);

}

class DCMotor : public Motor

{

public:

DCMotor(int baseaddress=0x378); virtual void Forward();

virtual void Reverse(); virtual void Brake();

};

DCMotor::DCMotor(int baseaddress):Motor(baseaddress)

{

}

void DCMotor::Forward()

{

int j;

for(j = 0; j < GetSpeed(); j++) WritePort0(0x09);

for(;j < 256; j++) WritePort0(0x00);

}

void DCMotor::Reverse()

{

int j;

for(j = 0; j < GetSpeed(); j++) WritePort0(0x06);

for(;j < 256; j++) WritePort0(0x00);

}

void DCMotor::Brake()

{

WritePort0(0x0C);

}

8 DRIVING MOTORS - DC & STEPPER 247

enum MOTORTYPE {UPFS, UPHS, BPFS, BPHS};

class StepperMotor : public Motor

{

private:

MOTORTYPE MotorType; unsigned char Switching[8]; int CycleIndex;

int MaxIndex;

public:

StepperMotor(MOTORTYPE motortype = UPFS, int baseaddress=0x378);

virtual void Forward(); virtual void Reverse(); virtual void Brake();

};

StepperMotor::StepperMotor(MOTORTYPE motortype,

int baseaddress): Motor(baseaddress)

{

MotorType = motortype; CycleIndex = 0;

switch(MotorType)

{

case UPFS: MaxIndex = 4; Switching[0] = 0x11; Switching[1] = 0x12; Switching[2] = 0x22; Switching[3] = 0x21; break;

case UPHS: MaxIndex = 8; Switching[0] = 0x01; Switching[1] = 0x11; Switching[2] = 0x10; Switching[3] = 0x12; Switching[4] = 0x02; Switching[5] = 0x22; Switching[6] = 0x20; Switching[7] = 0x21; break;

case BPFS: MaxIndex = 4; Switching[0] = 0x99; Switching[1] = 0x69;

248 8 DRIVING MOTORS - DC & STEPPER

Switching[2] = 0x66;

Switching[3] = 0x96; break;

case BPHS: MaxIndex = 8; Switching[0] = 0x99; Switching[1] = 0x09; Switching[2] = 0x69; Switching[3] = 0x60; Switching[4] = 0x66; Switching[5] = 0x06; Switching[6] = 0x96; Switching[7] = 0x90;

}

}

void StepperMotor::Forward()

{

if(++CycleIndex == MaxIndex) CycleIndex = 0; WritePort0(Switching[CycleIndex]); delay(259-GetSpeed());

}

void StepperMotor::Reverse()

{

if(--CycleIndex == -1) CycleIndex = MaxIndex -1; WritePort0(Switching[CycleIndex]); delay(259-GetSpeed());

}

void StepperMotor::Brake()

{

switch(MotorType)

{

case UPFS: case UPHS: WritePort0(0x11); break;

case BPFS: case BPHS: WritePort0(0x99);

}

}

void main()

{

Motor *MotorPtr; int Selection;

8 DRIVING MOTORS - DC & STEPPER 249

clrscr();

 

 

 

cout << endl << "

MOTOR MENU";

cout << endl << "

~~~~~~~~~~" << endl;

cout << "

1

DC Motor" << endl;

cout << "

2

UPFS" << endl;

cout << "

3

UPHS" << endl;

cout << "

4

BPFS" << endl;

cout << "

5

BPHS" << endl;

cout << "

6

QUIT" << endl;

cout << endl;

 

 

cout << "

Select the MOTOR Number: ";

cin >> Selection;

switch(Selection)

{

case 1: MotorPtr = new DCMotor; break;

case 2: MotorPtr = new StepperMotor(UPFS); break;

case 3: MotorPtr = new StepperMotor(UPHS); break;

case 4: MotorPtr = new StepperMotor(BPFS); break;

case 5: MotorPtr = new StepperMotor(BPHS); break;

case 6: return;

default: cout << endl;

cout << " Unspecified Motor type...."; cout << " PRESS a key to END Program!"; getch();

exit(1); // Exits the program

}

if(MotorPtr == NULL)

{

cout << "Memory allocation failed " << endl; getch();

exit(1);

}

cout << "**********************************" << endl; cout << "* CONNECT BOARD POWER SUPPLY NOW *" << endl;

250 8 DRIVING MOTORS - DC & STEPPER

cout << "**********************************" << endl; cout << endl;

cout << " After connecting power,”;

cout << “ press a key to continue " << endl; getch();

cout << endl;

cout << " Keypress changes Speed/Rotation”; cout << “ (& Braking)." << endl;

//..... Motor control part starts here .....

MotorPtr->SetSpeed(150); while(!kbhit()) MotorPtr->Forward(); getch(); // clear keyboard buffer

MotorPtr->SetSpeed(220); while(!kbhit()) MotorPtr->Forward(); getch();

MotorPtr->SetSpeed(250); while(!kbhit()) MotorPtr->Reverse(); getch();

MotorPtr->SetSpeed(255); while(!kbhit()) MotorPtr->Reverse(); getch();

cout << endl << " Braking Applied!" << endl; while(!kbhit()) MotorPtr->Brake();

getch();

MotorPtr->Off();

//..... Motor control part ends here .....

// Free the memory occupied by the 'Motor' object delete MotorPtr;

}

Don’t be overwhelmed by the length of this program. In general, programmers create header files and library files to hide all the code that is shown ahead of the main() function. Had we done the same, the program to control any of the motors in our list would be the size of the main function.

Observe the motor’s behaviour without the effect of dynamic braking by commenting out the call to the Brake() function in Listing 8-19. This is best seen if the motor shaft has some inertial load connected to it.

If we did NOT use virtual functions, then the code to control the motors (shown in

8 DRIVING MOTORS - DC & STEPPER 251

Listing 8-10) will need to be included within each case of the switch statement in the main() function of Listing 8-13. The pointer used within each case will need to be declared to point specifically to an object for that particular case, and all functions will be linked at programming time (early binding). A main() function equivalent to that of Listing 8-13, but without the use of virtual functions is given in Listing 8-20.

Listing 8-20 main() function WITHOUT using virtual functions.

void main()

{

int Selection; DCMotor* DCMotorPtr;

StepperMotor* UPFSStepperMotorPtr; StepperMotor* UPHSStepperMotorPtr; StepperMotor* BPFSStepperMotorPtr; StepperMotor* BPHSStepperMotorPtr;

clrscr();

 

 

 

cout << endl << "

MOTOR MENU";

cout << endl << "

~~~~~~~~~~" << endl;

cout << "

1

DC Motor" << endl;

cout << "

2

UPFS" << endl;

cout << "

3

UPHS" << endl;

cout << "

4

BPFS" << endl;

cout << "

5

BPHS" << endl;

cout << "

6

QUIT" << endl;

cout << endl;

 

 

cout << "

Select the MOTOR Number: ";

cin >> Selection; cout << endl;

switch(Selection)

{

case 1: DCMotorPtr = new DCMotor; if(DCMotorPtr == NULL)

{

cout << "Memory allocation failed " << endl; exit(1);

}

cout << "**********************************" <<

endl;

252 8 DRIVING MOTORS - DC & STEPPER

cout << "* CONNECT BOARD POWER SUPPLY NOW *" <<

endl;

cout << "**********************************" << endl; cout << endl;

cout << " After connecting power,”;

cout << “ press a key to continue " << endl; getch();

cout << " KEYPRESS changes SPEED/ROTATION (& Braking)." << endl;

// DCMotor control part starts here DCMotorPtr->SetSpeed(150); while(!kbhit()) DCMotorPtr->Forward(); getch(); // clear keyboard buffer

DCMotorPtr->SetSpeed(255); while(!kbhit()) DCMotorPtr->Forward(); getch();

DCMotorPtr->SetSpeed(150); while(!kbhit()) DCMotorPtr->Reverse(); getch();

DCMotorPtr->SetSpeed(255); while(!kbhit()) DCMotorPtr->Reverse(); getch();

while(!kbhit()) DCMotorPtr->Brake();

DCMotorPtr->Off();

//DCMotor control part ends here

//Release memory occupied by the DCMotor object delete DCMotorPtr;

break;

case 2: UPFSStepperMotorPtr = new StepperMotor(UPFS); if(UPFSStepperMotorPtr == NULL)

{

cout << "Memory allocation failed " << endl; exit(1);

}

cout << "**********************************" <<

endl;

cout << "* CONNECT BOARD POWER SUPPLY NOW *" <<

endl;

cout << "**********************************" <<

8 DRIVING MOTORS - DC & STEPPER 253

endl; cout << endl;

cout << " After connecting power,”;

cout << “ press a key to continue " << endl; getch();

cout << " KEYPRESS changes SPEED/ROTATION (& Braking)." << endl;

// UPFS Motor control part starts here UPFSStepperMotorPtr->SetSpeed(150); while(!kbhit()) UPFSStepperMotorPtr->Forward(); getch(); // clear keyboard buffer

UPFSStepperMotorPtr->SetSpeed(220); while(!kbhit()) UPFSStepperMotorPtr->Forward(); getch();

UPFSStepperMotorPtr->SetSpeed(250); while(!kbhit()) UPFSStepperMotorPtr->Reverse(); getch();

UPFSStepperMotorPtr->SetSpeed(255); while(!kbhit()) UPFSStepperMotorPtr->Reverse(); getch();

while(!kbhit()) UPFSStepperMotorPtr->Brake();

UPFSStepperMotorPtr->Off();

//UPFS Motor control part ends here

//Release memory occupied by the DCMotor object delete UPFSStepperMotorPtr;

break;

case 3: UPHSStepperMotorPtr = new StepperMotor(UPHS); if(UPHSStepperMotorPtr == NULL)

{

cout << "Memory allocation failed " << endl; exit(1);

}

cout << "**********************************" <<

endl;

cout << "* CONNECT BOARD POWER SUPPLY NOW *" <<

endl;

cout << "**********************************" << endl; cout << endl;

cout << " After connecting power,”;

cout << “ press a key to continue " << endl;

254 8 DRIVING MOTORS - DC & STEPPER

getch();

cout << " KEYPRESS changes SPEED/ROTATION (& Braking)." << endl;

// UPHS Motor control part starts here UPHSStepperMotorPtr->SetSpeed(150); while(!kbhit()) UPHSStepperMotorPtr->Forward(); getch(); // clear keyboard buffer

UPHSStepperMotorPtr->SetSpeed(220); while(!kbhit()) UPHSStepperMotorPtr->Forward(); getch();

UPHSStepperMotorPtr->SetSpeed(250); while(!kbhit()) UPHSStepperMotorPtr->Reverse(); getch();

UPHSStepperMotorPtr->SetSpeed(255); while(!kbhit()) UPHSStepperMotorPtr->Reverse(); getch();

while(!kbhit()) UPHSStepperMotorPtr->Brake();

UPHSStepperMotorPtr->Off();

//UPHS Motor control part ends here

//Release memory occupied by the DCMotor object delete UPHSStepperMotorPtr;

break;

case 4: BPFSStepperMotorPtr = new StepperMotor(BPFS); if(BPFSStepperMotorPtr == NULL)

{

cout << "Memory allocation failed " << endl; exit(1);

}

cout << "**********************************" <<

endl;

cout << "* CONNECT BOARD POWER SUPPLY NOW *" <<

endl;

cout <<

"**********************************" <<

endl; cout <<

endl;

cout <<

"

After connecting power,”;

cout <<

“ press a key to continue " << endl;

getch();

"

KEYPRESS changes SPEED/ROTATION (&

cout <<

Braking)." << endl;

 

// BPFS

Motor control part starts here