bool SavePackInfox::init(bool is_save, const string & file_n) { is_save_stream = is_save; this->file_name = file_n; if (is_save_stream) { if (NULL != file_stream) { file_stream->close(); delete file_stream; file_stream = NULL; } char file_str[100] = {0}; ACE_Date_Time curr_time(ACE_OS::gettimeofday()); sprintf(file_str, "%s_%d_%d_%dT%d_%d_%d", file_name.c_str(), curr_time.year(), curr_time.month(), curr_time.day(), curr_time.hour(), curr_time.minute(), curr_time.second()); file_stream = new fstream(); file_stream->open(file_str, ios_base::out | ios_base::app | ios_base::binary); if (file_stream->fail()) { delete file_stream; file_stream = NULL; return false; } } return true; }
std::string timewrapper::get_curr_date_time() { tm* timeinfo = new tm(); curr_time(timeinfo); char data_ptr[20]; strftime(data_ptr, 20, "%F %T", timeinfo); return std::string(data_ptr); }
int main(int argc, char *argv[]) { int j, sig_cnt; sigset_t block_mask, empty_mask; struct sigaction sa; if (argc < 2 || strcmp(argv[1], "--help") == 0) usage_err("%s child-sleep-time...\n", argv[0]); setbuf(stdout, NULL); sig_cnt = 0; num_live_children = argc - 1; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = sig_child_handler; if (sigaction(SIGCHLD, &sa, NULL) == -1) err_exit("sigaction"); /* * Block SIGCHLD to prevent its delivery if a child terminates * before the parent commences the sigsuspend() loop below */ sigemptyset(&block_mask); sigaddset(&block_mask, SIGCHLD); if (sigprocmask(SIG_SETMASK, &block_mask, NULL) == -1) err_exit("sigprocmask"); for (j = 1; j < argc; j++) { switch(fork()) { case -1: err_exit("fork"); case 0: sleep(get_int(argv[j], GN_NONNEG, "child-sleep-time")); printf("%s Child %d (PID=%ld) exiting\n", curr_time("%T"), j, (long)getpid()); _exit(EXIT_SUCCESS); default: break; } } /* Parent come here: wait for SIGCHLD until all children are dead */ while(num_live_children > 0) { if (sigsuspend(&empty_mask) == -1 && errno != EINTR) err_exit("sigsuspend"); sig_cnt++; } printf("%s All %d children have terminated; SIGCHLD was caught %d times\n", curr_time("%T"), argc - 1, sig_cnt); }
static void sig_child_handler(int sig) { int status, saved_errno; pid_t child_pid; /* UNSAFE: This handler uses non-async-signal-safe functions * (printf(), print_wait_status(), curr_time()) */ saved_errno = errno; /* In case we modify 'errno' */ printf("%s handler: Caught SIGCHLD\n", curr_time("%T")); while((child_pid = waitpid(-1, &status, WNOHANG)) > 0) { printf("%s handler: Reaped child %ld - ", curr_time("%T"), (long)child_pid); print_wait_status(NULL, status); num_live_children--; } if (child_pid == -1 && errno != ECHILD) err_msg("waitpid"); printf("%s handler: return\n", curr_time("%T")); }
int main (int argc, char *argv[]) { FILE *out = fopen("/home/arseni/Documents/p/bell_on_pi/daemon/ringed.list","a"); if(!out) { perror("fopen"); exit(EXIT_FAILURE); } fprintf(out,"%s %s\n",argv[1],curr_time("%T")); if(fclose(out) == -1) { perror("fclose"); exit(EXIT_FAILURE); } return 0; }
int main() { // INITIALIZE int x, y, color=0, pink=1; // set up for color channel 0 (green) int xvalue, yvalue; int greencentered = 0; int loop = 0; int centerx=77; // calibrate these number and enter manually int centery=63; int margin=8; int deltax = 0; int deltay = 0; int stepsizex; int stepsizey; int currpos; int s_time; light_start(L_SENSOR); // light start shut_down_in(119); camera_open(LOW_RES); enable_servos(); // enable servos set_servo_position(S_CATCHER,S_DOWN); msleep(500); set_servo_position(S_GATE,S_OPEN); msleep(500); // TRIBBLE PILE 1 //set_servo_position(S_GATE,S_OPEN); // open gate msleep(5000); set_servo_position(S_CATCHER,CATCHER_UP); right(28,ks/2); // right f_until_black(TOPHAT_RIGHT/*,TOPHAT_LEFT*/); // forward until right sensor sees black] forward(2); //printf("see black! time to close the gate\n"); set_servo_position(S_GATE,S_GAP); // close gate //f_until_white(TOPHAT_RIGHT); set_servo_position(CATCHERARM, CATCHER_UP); // camera sort s_time = curr_time(); while((curr_time()-s_time) < 14) { while (greencentered == 0) { camera_update(); // process the most recent image camera_update(); // process the most recent image camera_update(); // process the most recent image camera_update(); // process the most recent image camera_update(); // process the most recent image loop = loop + 1; printf("Update camera, loop %d\n", loop); //msleep(10); if (get_object_count(color) > 0) { xvalue = get_object_center(color,0).x; yvalue = get_object_center(color,0).y; deltax = abs(xvalue-centerx); deltay = abs(yvalue-centery); stepsizex = (deltax>10) ? 50: 20; stepsizey = (deltay>10) ? 50: 20; printf("x is %d, y is %d\n", xvalue, yvalue); greencentered = ((xvalue >= centerx-margin) & (xvalue <= centerx+margin)) && ((yvalue >= centery-margin) & (yvalue <= centery+margin)); if(greencentered == 1)//get x, y for the biggest blob the channel sees { printf("Biggest blob at (%d,%d)\n",xvalue,yvalue); msleep(300); set_servo_position(CATCHERARM, CATCHER_MIDWAY); msleep(300); set_servo_position(CATCHERARM, CATCHER_DOWN); //slow_servo(CATCHERARM,20,40,CATCHER_UP,CATCHER_DOWN); msleep(300); currpos=get_servo_position(CATCHERARM); printf("%d\n",currpos); /*if(CATCHER_MIDWAY>=currpos>CATCHER_DOWN) { currpos=get_servo_position(CATCHERARM); set_servo_position(CATCHERARM, currpos-INCREMENT); msleep(SLEEP_INCREMENT); currpos=get_servo_position(CATCHERARM); } if(currpos<CATCHER_MIDWAY) { currpos=get_servo_position(CATCHERARM); set_servo_position(CATCHERARM, currpos+INCREMENT); msleep(SLEEP_INCREMENT); currpos=get_servo_position(CATCHERARM); }*/ set_servo_position(CATCHERARM,CATCHER_UP); msleep(500); } else { if(xvalue <= centerx+margin) //moves right if senses value greater than 80 { motor(MOT_RIGHT,70); motor(MOT_LEFT,-70); msleep(stepsizex); motor(MOT_RIGHT,0); motor(MOT_LEFT,0); //msleep(10); printf("blob is too right\n"); } if(xvalue >= centerx-margin) // moves left if senses value less than 70 { motor(MOT_LEFT,70); motor(MOT_RIGHT,-70); msleep(stepsizex); motor(MOT_LEFT,0); motor(MOT_RIGHT,0); //msleep(10); printf("blob is too left\n"); } if ((xvalue > 67 /*centerx-margin*/) && (xvalue < 87 /*centerx+margin*/)) { if(yvalue > centery+margin) //moves forward if senses value greater than 43 { motor(MOT_LEFT,-50); motor(MOT_RIGHT,-50); msleep(stepsizey); motor(MOT_LEFT,0); motor(MOT_RIGHT,0); //msleep(10); printf("blob is too close\n"); } if(yvalue < centery-margin) // moves backwar if senses value less than 38 { motor(MOT_LEFT,50); motor(MOT_RIGHT,50); msleep(stepsizey); motor(MOT_LEFT,0); motor(MOT_RIGHT,0); //msleep(10); printf("blob is too far\n"); } } } } } } set_servo_position(S_GATE,S_CLOSE); while (analog10(2)<BLACK_SEN_THRESH) { motor(MOT_LEFT,-100); } // SCORE PILE 1 left(5,0); //backward(50); touch_back(TOUCH_SEN); forward(10); backward(10); forward(20); right(55,0); forward(35); backward(4); right(45,0); while (analog10(3)<BLACK_SEN_THRESH) { motor(MOT_LEFT, 100); motor(MOT_RIGHT, 100); } msleep(10); backward(24); left(42,0); backward(10); left(50,0); //backward(35); touch_back(TOUCH_SEN); forward(120.00); // TRIBBLE PILE 2 set_servo_position(S_GATE,S_OPEN); forward(40.00); set_servo_position(S_GATE,S_GAP); // camera sort s_time = curr_time(); while((curr_time()-s_time) < 14) //timer { while (greencentered == 0) { camera_update(); // process the most recent image camera_update(); // process the most recent image camera_update(); // process the most recent image camera_update(); // process the most recent image camera_update(); // process the most recent image loop = loop + 1; printf("Update camera, loop %d\n", loop); //msleep(10); if (get_object_count(color) > 0) { xvalue = get_object_center(color,0).x; yvalue = get_object_center(color,0).y; deltax = abs(xvalue-centerx); deltay = abs(yvalue-centery); stepsizex = (deltax>10) ? 50: 20; stepsizey = (deltay>10) ? 50: 20; printf("x is %d, y is %d\n", xvalue, yvalue); greencentered = ((xvalue >= centerx-margin) & (xvalue <= centerx+margin)) && ((yvalue >= centery-margin) & (yvalue <= centery+margin)); if(greencentered == 1)//get x, y for the biggest blob the channel sees { printf("Biggest blob at (%d,%d)\n",xvalue,yvalue); msleep(300); set_servo_position(CATCHERARM, CATCHER_MIDWAY); msleep(300); set_servo_position(CATCHERARM, CATCHER_DOWN); //slow_servo(CATCHERARM,20,40,CATCHER_UP,CATCHER_DOWN); msleep(300); currpos=get_servo_position(CATCHERARM); printf("%d\n",currpos); /*if(CATCHER_MIDWAY>=currpos>CATCHER_DOWN) { currpos=get_servo_position(CATCHERARM); set_servo_position(CATCHERARM, currpos-INCREMENT); msleep(SLEEP_INCREMENT); currpos=get_servo_position(CATCHERARM); } if(currpos<CATCHER_MIDWAY) { currpos=get_servo_position(CATCHERARM); set_servo_position(CATCHERARM, currpos+INCREMENT); msleep(SLEEP_INCREMENT); currpos=get_servo_position(CATCHERARM); }*/ set_servo_position(CATCHERARM,CATCHER_UP); msleep(500); } else { if(xvalue <= centerx+margin) //moves right if senses value greater than 80 { motor(MOT_RIGHT,70); motor(MOT_LEFT,-70); msleep(stepsizex); motor(MOT_RIGHT,0); motor(MOT_LEFT,0); //msleep(10); printf("blob is too right\n"); } if(xvalue >= centerx-margin) // moves left if senses value less than 70 { motor(MOT_LEFT,70); motor(MOT_RIGHT,-70); msleep(stepsizex); motor(MOT_LEFT,0); motor(MOT_RIGHT,0); //msleep(10); printf("blob is too left\n"); } if ((xvalue > 67 /*centerx-margin*/) && (xvalue < 87 /*centerx+margin*/)) { if(yvalue > centery+margin) //moves forward if senses value greater than 43 { motor(MOT_LEFT,-50); motor(MOT_RIGHT,-50); msleep(stepsizey); motor(MOT_LEFT,0); motor(MOT_RIGHT,0); //msleep(10); printf("blob is too close\n"); } if(yvalue < centery-margin) // moves backwar if senses value less than 38 { motor(MOT_LEFT,50); motor(MOT_RIGHT,50); msleep(stepsizey); motor(MOT_LEFT,0); motor(MOT_RIGHT,0); //msleep(10); printf("blob is too far\n"); } } } } } } set_servo_position(S_GATE,S_CLOSE); // SCORE PILE 2 right(55,0); forward(28); backward(12); right(55,0); touch_back(TOUCH_SEN); forward(200); right(50,0); touch_back(TOUCH_SEN); forward(15); left(50,0); while (analog10(3)<BLACK_SEN_THRESH) { motor(MOT_LEFT, 100); motor(MOT_RIGHT, 100); } backward(4); right(50,0); msleep(10); forward(24); right(42,0); touch_back(TOUCH_SEN); backward(4); // END ao(); disable_servos(); printf("done!"); }
timewrapper::timewrapper() { timeinfo = new tm(); curr_time(timeinfo); std::strftime(data_ptr, 11, "%F", timeinfo); }
void TestSuite::menu_tests( string spec_file_path ) { ifstream fin; ofstream fout_tst, fout_ans; string tst_filename = "menu_test_"; string ans_filename = "menu_ans_"; string read; stringstream ss; int num_test_files = -1; double max, min = 0; int rand_int = 0; float rand_float = 0; srand (time(NULL)); system("mkdir -p tests"); char ans[128] = {'\0'}; while (strcmp(ans,"y") && strcmp(ans,"n") ) { for (int i = 0; i < 128; i++) ans[i] = '\0'; cout << "Would you like to generate files for testing menues? (y/n): " << endl; cin >> ans; } if (!strcmp(ans,"y") ) { // open .spec file check for success fin.open(spec_file_path.c_str()); if (!fin) return; fin.close(); //display menu for range, number of test cases while ( num_test_files < 0 || num_test_files > 100) { cout << "Enter number of test files to generate for menu testing (between 0 and 100): "; cin >> num_test_files; cout << endl; } cout << "\nWhat is the MINIMUM value you would like the randomly generated values to be?" << "\n Number between –2147483648 to 2147483646"<< endl; cin >> min; cout << "\nWhat is the MAXIMUM value you would like the randomly generated values to be?" << "\n Number between –2147483647 to 2147483647"<< endl; cin >> max; while(max <= min) { cout << "\n Maximum must be larger than mimimum." << endl; cout << "\nWhat is the MINIMUM value you would like the randomly generated values to be?" << "\n Number between –2147483648 to 2147483646"<< endl; cin >> min; cout << "\nWhat is the MAXIMUM value you would like the randomly generated values to be?" << "\n Number between –2147483647 to 2147483647"<< endl; cin >> max; } double range = max - min; //generate .tst files for (int i = 0; i < num_test_files;i++) { string test_filename = tst_filename; string answer_filename = ans_filename; //generate time stamp time_t rawTime; tm * timeInfo; char buffer [40]; string i_to_string = ""; ss.str(""); time (&rawTime); timeInfo = localtime (&rawTime); strftime (buffer,40,"%d_%m_%y_%H_%M_%S",timeInfo); string curr_time(buffer); ss << i+1; i_to_string = ss.str(); test_filename += i_to_string; answer_filename += i_to_string; test_filename = test_filename + "_" + curr_time + ".tst"; answer_filename = answer_filename + "_" + curr_time + ".ans"; fin.open(spec_file_path.c_str()); fout_ans.open(answer_filename.c_str()); fout_tst.open(test_filename.c_str()); //open .tst and .ans files for output while ( fin >> read ) { //atoi returns 0 if it is a string value, use in if stmts if (atoi(read.c_str())) fout_tst << read << endl; else if ( !atoi(read.c_str()) ) { if (read == "int") { rand_int = rand() % int(range)+(max-range+1); fout_tst << rand_int << endl; } else { rand_float = range * ((double)rand()/(double)RAND_MAX) + min; fout_tst << rand_float << endl; } } } fout_tst.close(); fin.close(); string s = goldencpp + " < " + test_filename; FILE *pfile = popen(s.c_str(), "r"); char buff[256]; while(fgets(buff, sizeof(buff), pfile) != 0) { string result(buff); fout_ans << result; } //closing out files //fout1.close(); fout_ans.close(); pclose(pfile); string command = "mv "; command += test_filename; command += " tests"; system (command.c_str()); command = "mv " + answer_filename + " tests"; system (command.c_str()); } }