/* * function IR_Sample is to be called from an interrupt * reads value from ADC and stores in buffer * only samples when IR_Sample_Count = the Global variable IR sample rate set by user * stores a number of values in buffer according to Global variable IR sample per Estimate * when all samples taken sets Sample_Finished flag to 1 */ void IR_Sample(void) { IR_Sample_count++; if(IR_Sample_count == (GLOBAL_IRsampleR)) { IR_Sample_count = 0; switchChannels(SIDE_IR); *S_IR = doADC(); S_IR++; IR_count++; if(IR_count == (GLOBAL_IRsampleE)) { S_IR = &Side_IR_Buff[0]; IR_count = 0; Sample_Finished = 1; } } }
//#pragma code highPriorityInterruptAddress=0x0008 //void high_interrupt(void) //{ // _asm GOTO highPriorityIsr _endasm //} // //#pragma code void main(void){ //Default TRISD = 0x00; //set port D signals to output menu_ref_1=1; menu_ref_2=0; /*zone of disgust*/ values[MAXSPEED]=100; //Max speed values[PIDGAINS]=100; //PID Gains values[MAXYAW]=100; //Max Yaw values[IRSAMPE]=10; //ir_samp_e; values[IRSAMPR]=20; //ir_samp_r; values[IRRAW]=60; //ir_raw values[IRAVG]=10; //ir_avg; /*****************/ setupSerial(); Lcd_Init(); ADC_setup(); Button_Setup(); welcome(); LCD_disp(menu_ref_1, menu_ref_2); RUN=0; while(1){ //NORMAL OPERATION while(RUN==0){ if(PORTDbits.RD0 == 0) { mode_button = 1; } if(PORTDbits.RD1 == 0) { motor_button = 1; } if (mode_button==1){ //Mode change sequence checkMode(menu); menu_ref_2=0; //Default menu ref, should only be SPEED delayms(200); mode_button=0; LCD_disp(menu_ref_1, menu_ref_2); } delayms(200); //Used to prevent double tapping //possibly new function switchChannels(0); joy_x = doADC(); switchChannels(1); joy_y = doADC(); if(menu_ref_1==MANUAL) //IF IN MANUAL { if(joy_x<=LEFT){ //User pushes right joystick left if(values[menu_ref_2]>0){ values[menu_ref_2]=values[menu_ref_2]-5; //decrement value by 5% GLOBAL_MAX_SPEED = values[menu_ref_2]; sendMaxSpeed(); } LCD_disp(menu_ref_1, menu_ref_2); delayms(200); //Delay to prevent flickering, and also to prevent 100-0 increments } else if (joy_x>=RIGHT){ //User pushes right joystick right if(values[menu_ref_2]<100){ values[menu_ref_2]=values[menu_ref_2]+5; //increment value by 5% GLOBAL_MAX_SPEED = values[menu_ref_2]; sendMaxSpeed(); } LCD_disp(menu_ref_1, menu_ref_2); delayms(200); } } else if(menu_ref_1==FACTORY){ //IF IN FACTORY MODE if (joy_y>=UP){ //User pushes left joystick UP //Circular selection if(menu_ref_2==0){ menu_ref_2=4; } else{ menu_ref_2--; //switched the order of this to prevent overflow } LCD_disp(menu_ref_1, menu_ref_2); delayms(250); //Arbitrary delay of 250 milliseconds } else if (joy_y<=DOWN){ //User pushes left joystick DOWN menu_ref_2++; //Circular selection if(menu_ref_2>=7){ menu_ref_2=1; } LCD_disp(menu_ref_1, menu_ref_2); delayms(250); } else if (joy_x<=LEFT){ //User pushes right joystick left if(values[menu_ref_2]>0){ values[menu_ref_2]=values[menu_ref_2]-5; //decrement value by 5% } LCD_disp(menu_ref_1, menu_ref_2); delayms(255); delayms(200); } else if (joy_x>=RIGHT){ //User pushes right joystick right if(values[menu_ref_2]<100){ values[menu_ref_2]=values[menu_ref_2]+5; //decrement value by 5% } LCD_disp(menu_ref_1, menu_ref_2); delayms(255); delayms(200); } } //All other menu_ref_1's do not have values displayed if (motor_button==1){ motor_button=0; RUN=1; } //send here } /*Menu on setup*/ if (RUN==1){ on_setup(); GLOBAL_RUN = RUN; sendRun(); //MOTOR ON GOES AFTER motor_button=0; } /*MOTOR ON BEHAVIOUR*/ while(RUN==1){ if(PORTDbits.RD1 == 0) { motor_button = 1; } if (motor_button==1){ //At interrupt, stop motor and return to menu motor_button=0; RUN=0; GLOBAL_RUN = RUN; sendRun(); } switchChannels(0); //GLOBAL_VELOCITY = doADC(); GLOBAL_VELOCITY = doADC() //Get joystick values joy_y = doADC(); GLOBAL_VELOCITY = joy_y; switchChannels(1); joy_x = doADC(); GLOBAL_OMEGA = joy_x; sendVelocity(); sendOmega(); //send command here } /*MOTOR OFF MESSAGE*/ RUN=0; //MOTOR OFF FUNCTION GOES BEFORE MESSAGE LCD_title(t5); //"Whoa!" delays(2); //safety delay LCD_disp(menu_ref_1, menu_ref_2); //Return to normal display } }
void *client_handler(void *fd) { struct USER user = *(struct USER *)fd; struct PACKET packet; struct UNODE *curr; struct CNODE *channelIterator; size_t bytes, sent; struct PACKET response; while(1) { bytes = recv(user.socketfd, (void *)&packet, sizeof(struct PACKET), 0); if(!bytes) { fprintf(stderr, "Connection lost from [%d] %s...\n", user.socketfd, user.alias); removeUser(&user); break; } printf("[%d] %s %s %s %s\n", user.socketfd, packet.option, user.channelAlias, packet.senderAlias, packet.message); if(!strcmp(packet.option, "alias")) { printf("Set alias to %s\n", packet.senderAlias); channelIterator = channels_find(&channels, user.channelAlias); struct CHANNEL *channel = &channelIterator->channel; pthread_mutex_lock(&channel->channel_mutex); for(curr = channel->head; curr != NULL; curr = curr->next) { if(compare(&curr->user, &user) == 0) { strcpy(curr->user.alias, packet.senderAlias); strcpy(user.alias, packet.senderAlias); break; } } pthread_mutex_unlock(&channel->channel_mutex); memset(&response, 0, sizeof(struct PACKET)); strcpy(response.option, "alias-ok"); strcpy(response.message, user.alias); send(user.socketfd, (void *)&response, sizeof(struct PACKET), 0); } else if (!strcmp(packet.option, "create")) { pthread_mutex_lock(&channels.channels_mutex); if (channels_contains(&channels, packet.message)) { sendError("Channel already exists.", &user); } else { struct CHANNEL newChannel; channel_init(&newChannel); strncpy(newChannel.alias, packet.message, ALIAS_LEN); channels_insert(&channels, &newChannel); } pthread_mutex_unlock(&channels.channels_mutex); } else if (!strcmp(packet.option, "join")) { switchChannels(user.channelAlias, packet.message, &user); } else if (!strcmp(packet.option, "leave")) { switchChannels(user.channelAlias, DEFAULT_CHANNEL_ALIAS, &user); } else if(!strcmp(packet.option, "send")) { pthread_mutex_lock(&channels.channels_mutex); channelIterator = channels_find(&channels, user.channelAlias); struct CHANNEL *currentChannel = &channelIterator->channel; pthread_mutex_unlock(&channels.channels_mutex); pthread_mutex_lock(¤tChannel->channel_mutex); for(curr = currentChannel->head; curr != NULL; curr = curr->next) { if(!compare(&curr->user, &user)) continue; struct PACKET spacket; memset(&spacket, 0, sizeof(struct PACKET)); strcpy(spacket.option, "msg"); strcpy(spacket.senderAlias, user.alias); strcpy(spacket.message, packet.message); sent = send(curr->user.socketfd, (void *)&spacket, sizeof(struct PACKET), 0); } pthread_mutex_unlock(¤tChannel->channel_mutex); } else if(!strcmp(packet.option, "exit")) { printf("[%d] %s has disconnected...\n", user.socketfd, user.alias); removeUser(&user); break; } else { fprintf(stderr, "Garbage data from [%d] %s...\n", user.socketfd, user.alias); } } close(user.socketfd); return NULL; }