Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum driverStates
- {
- Accelerating,
- Slowing,
- Cornering,
- };
- driverStates currentState;
- int timeInCurrentGear = 0;
- int distToCorner = 0;
- int maxAllowedCornerSpeed = 20;
- static void
- drive(int index, tCarElt* car, tSituation *s)
- {
- memset((void *)&car->ctrl, 0, sizeof(tCarCtrl));
- currentState = Accelerating;
- int currentGear = car->_gear;
- int currentSpeed = car->_speed_x;
- if (car->_trkPos.seg->type == TR_LFT || car->_trkPos.seg->type == TR_RGT)
- {
- currentState = Cornering;
- }
- else if (car->_trkPos.seg->type == TR_STR)
- {
- currentState = Accelerating;
- }
- //Track types are stored as ints, and defined as
- // TR_RGT = 1
- // TR_LFT = 2
- // TR_RGT = 3
- //within track.h, making this for loop and list iteration nice and simple.
- std::list<trackSeg *> nextThirtySegs;
- trackSeg * currSeg = car->_trkPos.seg;
- for (int i = 0; i < 30; i++)
- {
- nextThirtySegs.push_front(currSeg);
- currSeg = currSeg->next;
- }
- //The below is a range-based for loop, apparently introduced with C++11, more readable for me
- //than using something like 'for (nextTenSegs::iterator i = nextTenSegs.begin(); i != nextTenSegs.end(); i++){}'
- //We check the next 30 segments for a corner, and when we find one we break the loop
- //and calculate the distance to that segment.
- for (auto const& i : nextThirtySegs)
- {
- if (i->type == TR_RGT || i->type == TR_LFT)
- {
- currentState = Slowing;
- std::cout << "corner detected" << std::endl;
- break;
- }
- else
- {
- distToCorner += i->length;
- }
- }
- nextThirtySegs.clear();
- //std::cout << distToCorner << std::endl;
- //Tells us the angle of the car.
- float carAngle = car->_yaw;
- //Tells us (in metres) how far we are from the middle 'line' of the current track segment,
- //we then divide it by the width of the track segment to avoid oversteering.
- float disToMiddle = car->_trkPos.toMiddle / car->_trkPos.seg->width;
- //Tells us the width of the current track segment (in metres).
- float segWidth = car->_trkPos.seg->width;
- //This calculates the turning angle of the current track segment in radians.
- float angleOfTrack = RtTrackSideTgAngleL(&(car->_trkPos));
- float steering = angleOfTrack - carAngle - disToMiddle;
- //Bringing the steering value to within -PI / PI.
- NORM_PI_PI(steering);
- //the 'car->_steerLock' ensures the value of the steering stays within -1 to +1.
- car->_steerCmd = steering / car->_steerLock;
- if (currentState = Accelerating)
- {
- //std::cout << "Accelerating" << std::endl;
- car->ctrl.brakeCmd = 0.0;
- car->_accelCmd = 0.4;
- if (car->_enginerpm >= 850.0)
- {
- currentGear++;
- timeInCurrentGear = 0;
- }
- else if (car->_enginerpm <= 450.0 && currentGear >= 2 && timeInCurrentGear >= 50)
- {
- currentGear--;
- timeInCurrentGear = 0;
- }
- }
- else if (currentState = Slowing)
- {
- //std::cout << "Slowing" << std::endl;
- if (currentSpeed > maxAllowedCornerSpeed)
- {
- car->_accelCmd = 0.0;
- car->ctrl.brakeCmd = 0.5;
- }
- else
- {
- car->ctrl.brakeCmd = 0.0;
- car->_accelCmd = 0.4;
- if (car->_enginerpm >= 850.0)
- {
- currentGear++;
- timeInCurrentGear = 0;
- }
- else if (car->_enginerpm <= 450.0 && currentGear >= 2 && timeInCurrentGear >= 50)
- {
- currentGear--;
- timeInCurrentGear = 0;
- }
- }
- }
- else if (currentState = Cornering)
- {
- //std::cout << "Cornering" << std::endl;
- car->_brakeCmd = car->_trkPos.seg->next->arc * 2;
- car->_accelCmd = 0.4;
- if (car->_enginerpm >= 600 && currentGear >= 2)
- {
- currentGear--;
- timeInCurrentGear = 0;
- }
- }
- timeInCurrentGear++;
- distToCorner = 0;
- car->_gearCmd = currentGear;
- //std::cout << car->_enginerpm << std::endl;
- //std::cout << timeInCurrentGear << std::endl;
- //std::cout << car->_trkPos.seg->type << std::endl;
- //std::cout << currentState << std::endl;
- //std::cout << currentSpeed << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement