SmartVehicle-Basis
SmartFactory
Drive.cpp
Go to the documentation of this file.
1 
13 #include "Drive.h"
14 
15 //=====PUBLIC====================================================================================
16 
21 Drive::Drive(const int MotorPortRight, const int MotorPortLeft) {
22  DBFUNCCALLln("Drive::Drive(const int MotorPortRight, const int MotorPortLeft)");
23  pMotorRight = pAFMS.getMotor(MotorPortRight);
24  pMotorLeft = pAFMS.getMotor(MotorPortLeft);
25  pAFMS.begin(); // create with the default frequency 1.6KHz
26  stop();
27 }
28 
29 void Drive::drive(Direction direction, unsigned int speed) {
30  DBFUNCCALLln("Drive::drive(Direction direction, unsigned int speed)");
31  speed = constrain(speed, 0, 255);
32  pWayVehicle = direction;
33  pSpeedLeft = speed;
34  pSpeedRight = speed;
35  pMotorLeft->setSpeed(pSpeedLeft);
36  pMotorRight->setSpeed(pSpeedRight);
37  DBINFO3(decodeDirection(direction));
38  DBINFO3ln(String(" Speed L/R: ") + String(pSpeedLeft) + String(" / ") + String(pSpeedRight));
39  switch (pWayVehicle) {
40  case Direction::Forward:
41  pMotorLeft->run(FORWARD);
42  pMotorRight->run(FORWARD);
43  break;
45  pMotorLeft->run(BACKWARD);
46  pMotorRight->run(BACKWARD);
47  break;
48  default:
49  break;
50  }
51 }
52 
53 void Drive::turn(Direction direction, unsigned int speed) {
54  DBFUNCCALLln("Drive::turn(Direction direction, unsigned int speed)");
55  switch (direction) {
56  case Direction::Left:
57  pTurnSpeedLeft = pSpeedLeft - speed;
58  pTurnSpeedRight = pSpeedRight + speed;
59  break;
60  case Direction::Right:
61  pTurnSpeedLeft = pSpeedLeft + speed;
62  pTurnSpeedRight = pSpeedRight - speed;
63  break;
64  default:
65  break;
66  }
67  pTurnSpeedLeft = constrain(pTurnSpeedLeft, 0, 255);
68  pTurnSpeedRight = constrain(pTurnSpeedRight, 0, 255);
69  DBINFO3(decodeDirection(direction));
70  DBINFO3ln(String(" Speed L/R: ") + String(pTurnSpeedLeft) + String(" / ") + String(pTurnSpeedRight));
71  pMotorLeft->setSpeed(pTurnSpeedLeft);
72  pMotorRight->setSpeed(pTurnSpeedRight);
73  switch (pWayVehicle) {
74  case Direction::Forward:
75  pMotorLeft->run(FORWARD);
76  pMotorRight->run(FORWARD);
77  break;
79  pMotorLeft->run(BACKWARD);
80  pMotorRight->run(BACKWARD);
81  break;
82  default:
83  break;
84  }
85 }
86 
87 void Drive::turnonpoint(Direction direction, unsigned int speed) {
88  DBFUNCCALLln("Drive::turn(Direction direction, unsigned int speed)");
89  speed = constrain(speed, 0, 255);
90  pTurnSpeedLeft = speed;
91  pTurnSpeedRight = speed;
92  pMotorLeft->setSpeed(pTurnSpeedLeft);
93  pMotorRight->setSpeed(pTurnSpeedRight);
94  switch (direction) {
95  case Direction::Left:
96  pMotorLeft->run(BACKWARD);
97  pMotorRight->run(FORWARD);
98  DBINFO3ln(String("Speed L/R: ") + String("-") + String(pTurnSpeedLeft) + String(" / ") + String(pTurnSpeedRight));
99  break;
100  case Direction::Right:
101  pMotorLeft->run(FORWARD);
102  pMotorRight->run(BACKWARD);
103  DBINFO3ln(String("Speed L/R: ") + String(pTurnSpeedLeft) + String(" / ") + String("-") + String(pTurnSpeedRight));
104  break;
105  default:
106  break;
107  }
108 }
109 
110 void Drive::stop() {
111  DBFUNCCALLln("Drive::stop()");
112  DBINFO3ln("Drive Stop!");
113  pSpeedLeft = 0;
114  pSpeedRight = 0;
115  pMotorLeft->setSpeed(pSpeedLeft);
116  pMotorRight->setSpeed(pSpeedRight);
117  pMotorLeft->run(RELEASE);
118  pMotorRight->run(RELEASE);
119 }
120 //=====PRIVATE===================================================================================
121 
122 //============================================================================
123 //==Aux-Function==============================================================
125  DBFUNCCALLln("Drive::decodeDirection(Direction direction)");
126  switch (direction) {
127  case Direction::Left:
128  return "Direction::Left";
129  break;
130  case Direction::Right:
131  return "Direction::Right";
132  break;
133  case Direction::Forward:
134  return "Direction::Forward";
135  break;
136  case Direction::Backward:
137  return "Direction::Backward";
138  break;
139  default:
140  return "ERROR: No matching event";
141  break;
142  }
143 }
Direction pWayVehicle
Actual movingdirection Forward or Backward.
Definition: Drive.h:103
Direction
Direction enum holds all possible directions.
Definition: Drive.h:49
String decodeDirection(Direction direction)
Decodes the Direction-Enum and returns a description.
Definition: Drive.cpp:124
int pSpeedRight
actual Speed of right Motor
Definition: Drive.h:100
void drive(Direction direction, unsigned int speed)
Drive straight Forwards or Backwards.
Definition: Drive.cpp:29
Adafruit_DCMotor * pMotorRight
Pointer to right Motor Obj.
Definition: Drive.h:97
int pTurnSpeedRight
Definition: Drive.h:102
Drive(const int MotorPortRight, const int MotorPortLeft)
Construct a new Drive object https://www.adafruit.com/product/2927.
Definition: Drive.cpp:21
void stop()
Power of the Motor. Does not apply breaking.
Definition: Drive.cpp:110
void turn(Direction direction, unsigned int speed)
Turn left or right. Speed will add to one Motor's actual speed and substract from the others depenig ...
Definition: Drive.cpp:53
The Drive Class handles the activation of the dc-motors for the drive.
int pTurnSpeedLeft
Definition: Drive.h:101
int pSpeedLeft
actual Speed of left Motor
Definition: Drive.h:99
Adafruit_MotorShield pAFMS
Definition: Drive.h:96
void turnonpoint(Direction direction, unsigned int speed)
Turn on Point Drive on Motor Forward and the other Backward with the given speed.
Definition: Drive.cpp:87
Adafruit_DCMotor * pMotorLeft
Pointer to left Motor Obj.
Definition: Drive.h:98