示例#1
0
int main ()
{
    Joystick joystick(1);

    while (true)
    {
        // Restrict rate
        usleep(1000);

        // Attempt to sample an event from the joystick
        JoystickEvent event;
        if (joystick.sample(&event))
        {
            if (event.isButton())
            {
                printf("Button %u is %s\n", event.number, event.value == 0 ? "up" : "down");
            }
            else if (event.isAxis())
            {
                printf("Axis %u is at position %d\n", event.number, event.value);
            }
        }
    }

    return 0;

}
示例#2
0
void joystickControl()
{
	// Attempt to sample an event from the joystick
    JoystickEvent event;
    if (joystick->sample(&event))
    {
      if (event.isButton())
      {
        printf("Button %u is %s\n",
          event.number,
          event.value == 0 ? "up" : "down");
      }
      else if (event.isAxis())
      {
        printf("Axis %u is at position %d\n", event.number, event.value);
		switch(event.number)
		{
			case 0:
				//balanceControl.spinSpeed =(float)event.value/JOYSTICK_AXIS_MAX;
				smonthSpinSpeed = (float)event.value/JOYSTICK_AXIS_MAX;
				//balanceControl.factorL = event.value/JOYSTICK_AXIS_MAX;
				//balanceControl.factorR = 1-balanceControl.factorL;
			break;
			case 1:
			break;
			case 2:		//右手左右
			break;
			case 3:		//右手前后
				//angle = axisToAngle(event.value);
				//printf("angle %f\r\n", angle);
				//servoSetAngle(&servoH, axisToAngle(event.value));
				smonthSpeed = -(float)event.value/JOYSTICK_AXIS_MAX * 10;
				
				//balanceControl.pidSpeed.desired = -(float)event.value/JOYSTICK_AXIS_MAX * 5;
			break;
		}
      }
  	}

  	balanceControl.pidSpeed.desired = LPF(balanceControl.pidSpeed.desired, smonthSpeed);
  	balanceControl.spinSpeed = LPF(balanceControl.spinSpeed, smonthSpinSpeed);

  	speed_need = LPF(speed_need, smonthSpeed);
}
int main() {
  init_serial();

  char package[5];
  package[0] = 'a';
  package[4] = 0;
  int v[3];



  Joystick joystick("/dev/input/js0");
  if (!joystick.isFound()) {
    std::cerr << "no joystick found" << std::endl;
    return 1;
  }

  while (true) {
    int axis0_pos, axis1_pos, axis3_pos;
    float v_ang, v_tan, v_norm;
    JoystickEvent event;
    if (joystick.sample(&event)) {
      if ((event.isAxis()) && (event.number == 1)) {
        axis1_pos = event.value;
        if (axis1_pos > 4000 || axis1_pos < -4000) {
          v_tan = (-(float)axis1_pos / 1000.0);
        } else {
          v_tan = 0;
        }
        v[0]=v_tan;
        std::cout << "v_tan"<< v_tan << std::endl;
      }
      if ((event.isAxis()) && (event.number == 0)) {
        axis0_pos = event.value;
        if (axis0_pos > 4000 || axis0_pos < -4000) {
          v_norm = (-(float)axis0_pos / 1000.0);
        } else {
          v_norm = 0;
        }
        v[1]=v_norm;
        std::cout << "v_norm" <<v_norm << std::endl;
      }
      if ((event.isAxis()) && (event.number == 3)) {
        axis3_pos = event.value;
        if ((axis3_pos > 7000) || (axis3_pos < -1000)) {
          v_ang = (-(float)axis3_pos / 1000.0);
        } 
        else {
          v_ang = 0;
        }
        v[2]=v_ang;
        std::cout << "v_ang" << v_ang << std::endl;
      }
      for(int i = 0; i< 3; i++){
        package[i+1] = v[i];
      }
      for(int i = 0; i < 5; i++){
        serial_port.write(&package[i], 1);
        std::cerr << package[i] << " ";
      }
      sleep(0.01);
    }
  }
  return EXIT_SUCCESS;
}
示例#4
0
 void check_controller(rov_context_t *context)
 {
     JoystickEvent event;
     bool result = context->joystick->sample(&event);
     if(result && !event.isInitialState())
     {
         if(event.isAxis())
         {
             int value = event.value;
             switch(event.number)
             {
                 case 0: // The x axis
                 {
                     x = value;
                     move_xy(context, x, y);
                 } break;
                 case 1: // The y axis
                 {
                     y = value;
                     move_xy(context, x, y);
                 } break;
                 case 2: // The twist axis
                 {
                     rotate_z(context, value);
                 } break;
                 case 3: // The kull axis
                 {
                     move_z(context, value);
                 } break;
                 case 4: // Top x-axis
                 {
                     top_x = value;
                     balance(context, top_x, top_y);
                 } break;
                 case 5: // Top y-axis
                 {
                     top_y = value;
                     balance(context, top_x, top_y);
                 } break;
             }
         }
         else if(event.isButton())
         {
             if (event.value)
             {
                 switch(event.number)
                 {
                     case 0:
                     {
                         rov_interface::control_gripper(context, 20);
                     } break;
                     case 1:
                     {
                         rov_interface::control_gripper(context, 150);
                     } break;
                     case 2:
                     {
                         rov_interface::switch_camera(context, 3);
                     } break;
                     case 3:
                     {
                         rov_interface::switch_camera(context, 4);
                     } break;
                     case 4:
                     {
                         rov_interface::switch_camera(context, 8);
                     } break;
                     case 8:
                     {
                         valve_opener_pwm -= 16;
                     } break;
                     case 9:
                     {
                         valve_opener_pwm += 16;
                     } break;
                 }
             }
             if (event.number == 10)
             {
                 rov_interface::set_motor(context, VALVE_OPENER, event.value, false, valve_opener_pwm);
             }
             else if (event.number == 11)
             {
                 rov_interface::set_motor(context, VALVE_OPENER, false, event.value, valve_opener_pwm);
             }
         }
     }
 }
示例#5
0
int main(int argc, char *argv[])
{
	logOpen();
	initXWindows();
	init_opengl();
	init_sounds();
	load_sounds();
	Game game;
	gblock_info gbi;
	gbi.width = 8;
	gbi.height = 8;
	gbi.rows = 100;
	gbi.columns = 100;
	begin_game(game, gbi);
	Joystick joystick;
	init(&game);
	init_textures(game);
	printf("%f\n",game.Player_1.stats.color[0]);
	srand(time(NULL));
	clock_gettime(CLOCK_REALTIME, &timePause);
	clock_gettime(CLOCK_REALTIME, &timeStart);
	int done=0;
	while (!done) {
		while (XPending(dpy)) {
			XEvent e;
			XNextEvent(dpy, &e);
			check_resize(&e);
			check_mouse(&e, &game);
			done = check_keys(&e);
		}
		//Joystick events need to be checked outside of XPending(dpy)
		JoystickEvent event;
		if (joystick.sample(&event)) {
			if (event.isButton() || event.isAxis()){
				getJoystickEvent(event, joy, axis);
			}
		}
		clock_gettime(CLOCK_REALTIME, &timeCurrent);
		timeSpan = timeDiff(&timeStart, &timeCurrent);
		timeCopy(&timeStart, &timeCurrent);
		physicsCountdown += timeSpan;
		while (physicsCountdown >= physicsRate) {
			physics(&game);
			physicsCountdown -= physicsRate;
		}
		/*if (argc == 1) {
			if (titleScreen) {
				if (keys[XK_Return]) {
					enterPressed = keys[XK_Return];
				}
				if (keys[XK_Down]) {
					downPressed ^= keys[XK_Down];
					upPressed = 0;
					enterPressed = 0;
				}
				if (keys[XK_Up]) {
					upPressed ^= keys[XK_Up];
					downPressed = 0;
					enterPressed = 0;
				}
				titleScreen = renderTitleScreen(introTextures, introImages, 
								enterPressed, downPressed, upPressed, keys);
			} else if (Pause) {
	
			QUIT = PAUSE(&game, keys);
			if (QUIT)
				return(0);

		} else if (winCondition) {
				//test
					endCredits(&game, keys);
			} 
			else {
				glClearColor(0.8, 0.8, 0.8, 1.0);
				render(&game);
			}
		}  
		if (winCondition) {
			//test
				endCredits(&game, keys);
		}  
		else if (Pause) {
	
			QUIT = PAUSE(&game, keys);
			if (QUIT)
				return(0);

		} else {
		    glClearColor(0.8,0.8,0.8,1.0);
		    render(&game);
		}*/
		render(&game);
		glXSwapBuffers(dpy, win);
	}
	cleanupXWindows();
	cleanup_fonts();
	logClose();
	release_sounds();
	return 0;
}