void moveToLocation(int newPosition) { if(newPosition != currentPosition){ startMotor(); Delay10KTCYx(7); while(!readIRSensor()); stopMotor(); //Track the current location of the plant currentPosition++; if(currentPosition == 3){ currentPosition = 0; } //recursive call to move the the next position if this is not the required state if(newPosition != currentPosition){ moveToLocation(newPosition); } } }
void high_isr(void) { //Timer for servo if(INTCONbits.TMR0IF) { PORTBbits.RB3 = 1; if(servoState == 0){ Delay1TCY(); Delay1TCY(); //Delay10TCYx(6); }else{ Delay1TCY(); Delay1TCY(); Delay1TCY(); Delay1TCY(); Delay1TCY(); Delay1TCY(); Delay1TCY(); Delay10TCYx(5); Delay100TCYx(4); } PORTBbits.RB3 = 0; INTCONbits.TMR0IF = 0; // Clear interrupt flag for timer 0 WriteTimer0(64911); } //code for servo state = 1 and regular timer if(PIR1bits.TMR1IF && servoState == 1){ valveCounter++; if(valveCounter == valveTotalTick){ closeValve(); valveCounter = 0; } PIR1bits.TMR1IF = 0; // Clear interrupt flag for timer 0 } else if(PIR1bits.TMR1IF && plantTimerCount == totalTimerCount){ //Waters all the plants moveToLocation(plantBeingWatered); openValve(); if(plantBeingWatered == totalplants){ plantTimerCount = 0; plantBeingWatered = -1; } plantBeingWatered++; PIR1bits.TMR1IF = 0; // Clear interrupt flag for timer 0 } //Timer for timed water sequence else if(PIR1bits.TMR1IF) { plantTimerCount++; PIR1bits.TMR1IF = 0; // Clear interrupt flag for timer 0 } // interupt for button to water plant 0 if(INTCONbits.INT0IF){ moveToLocation(0); openValve(); INTCONbits.INT0IF = 0; } // interupt for button to water plant 1 if(INTCON3bits.INT1IF){ moveToLocation(1); openValve(); INTCON3bits.INT1IF = 0; } // interupt for button to water plant 2 if(INTCON3bits.INT2IF){ moveToLocation(2); openValve(); INTCON3bits.INT2IF = 0; } }
void trainTask() { int trainNum, reply = 0, src; Receive(&src, (char *)&trainNum, sizeof(int)); Reply(src, (char *)&reply, sizeof(int)); track_node track[TRACK_MAX]; initTrack(track); int velocity[15]; initVelocities(trainNum, velocity); //find your location int location; struct TrainMessage msg; int dest,i,j, sensorsPassed; int oldSensor, sensor, oldTime, time; while(true) { Receive(&src, (char *)&msg, sizeof(struct TrainMessage)); switch(msg.type) { case TRAINGOTO: resetSensorBuffer(); dest = 0; while(dest < TRACK_MAX && strcmp(track[dest].name, msg.dest) != 0) dest++; location = moveToLocation(trainNum, location, dest, track, msg.doReverse, msg.speed, velocity); Reply(src, (char *)&reply, sizeof(int)); break; case TRAININIT: Reply(src, (char *)&reply, sizeof(int)); Putc2(1, (char)2, (char)trainNum); location = waitOnAnySensor(); Putc2(1, (char)0, (char)trainNum); break; case TRAINCONFIGVELOCITY: Putc2(1, (char)2, (char)trainNum); location = waitOnAnySensor(); Putc2(1, (char)0, (char)trainNum); //move to sensor B15 location = moveToLocation(trainNum, location, 30, track, false, 10, velocity); setSwitchState(14, 'C'); setSwitchState(13, 'S'); setSwitchState(10, 'S'); setSwitchState(9, 'C'); setSwitchState(8, 'C'); setSwitchState(17, 'S'); setSwitchState(16, 'S'); setSwitchState(15, 'C'); for(i=8; i<15; i++) { Putc2(1, (char)i, (char)trainNum); oldSensor = waitOnAnySensor(); oldTime = Time(); sensorsPassed = 0; for(j=0; j<15; j++) { sensor = waitOnAnySensor(); time = Time(); int distance = 1000*BFS(oldSensor, sensor, track, NULL, false); int newVelocity = distance/(time - oldTime); if(velocity[i] - newVelocity < velocity[i]/200 && sensorsPassed >= 10) break; velocity[i] *= 95; velocity[i] += 5*newVelocity; velocity[i] /= 100; ++sensorsPassed; } printf("Configured speed %d to %dmm/s\r", i, velocity[i]/10); } Putc2(1, (char)0, (char)trainNum); Reply(src, (char *)&reply, sizeof(int)); break; } } }
void MoralityServer::getCommand(int PlayerID, MoralThread *theThread, QByteArray packetcommand) { qDebug() << "GETTING COMMAND FROM THE THREAD" <<endl; qDebug() << "COMMAND: " << packetcommand << endl; // RESPOND TO THE CLIENT REQUEST HERE! // Response to the client would be more difficult and would have to have repeating keys for which we can use regular expressions to split // LOCATION::ID1-1,ID2-4,ID3-19 // {socket descriptor that your computer would turn into player 0, 1, 2, ... n players // WINNING ::ID // Which socket descriptor won // SCORE ::ID1-900,ID2-100000 // etc. QString incomingcommand(packetcommand); if (!incomingcommand.contains("//") || !incomingcommand.contains("::")) return; // DONT DO ANYTHING WITH EMPTY LINES QStringList BytesCommand = incomingcommand.split("//",QString::SkipEmptyParts); qint16 bytesize(BytesCommand[0].toInt()); // if (BytesCommand[1].size() != bytesize) // no it's not really bytes // qDebug() << "incorrect packet size!"; // but do nothing... really how big are our packets? 20 bytes? QStringList CKeyVal = BytesCommand[1].split("::"); QString commandString; if (CKeyVal[0] == "LOCATION") { commandString="LOCATION::"; qDebug() << CKeyVal[1] << endl; int newlocation = CKeyVal[1].toInt(); qDebug() << newlocation << endl; moveToLocation(PlayerID, newlocation); commandString.append(getLocations()); } else if (CKeyVal[0] == "WINNING") { commandString="WINNING::"; commandString.append(QString::number(PlayerID)); // just broadcast this to all players } else if (CKeyVal[0] == "SCORE") { // After winning is received on the server, clients will send a SCORE VALUE... // when received from all clients, will emit back in unison // commandString="SCORE::"; // commandString.append() NOT SURE ABOUT THIS YET SCORE OR ALIGNMENT // IT COULD BE DISPLAYED SOMEHOW } else if (CKeyVal[0] == "DAMAGE") { emit sendCommand(packetcommand); // the same one that came from the client since already in the right format return; } else if (incomingcommand.contains("SOCKETID")) { commandString="SOCKETID::"; commandString.append(QString::number(theThread->getSocketDescriptor())); QString command; command = QString::number(commandString.size()); // packet size command.append(tr("//")); command.append(commandString); QByteArray newPacketCommand; newPacketCommand.append(command); for (int i =0; i < Morals.size(); i++) { if (Morals[i]->getSocketDescriptor() == theThread->getSocketDescriptor()) { Morals[i]->commandToSocket(newPacketCommand); return; } } } else { return; } QString command; command = QString::number(commandString.size()); // packet size command.append(tr("//")); command.append(commandString); QByteArray newPacketCommand; newPacketCommand.append(command); for (int i =0; i < Morals.size(); i++) { Morals[i]->commandToSocket(newPacketCommand); } qDebug() << "THE PACKET: " << newPacketCommand <<endl; }