void init() {
	NLINES = 8;

	*sensors = {
		.TLEFT_P = EV3_PORT_1,
		.COLOR_P = EV3_PORT_2,
		.ULTRA_P = EV3_PORT_3,
		.TRIGHT_P = EV3_PORT_4,

		.LEFT_P = EV3_PORT_A,
		.RIGHT_P = EV3_PORT_D
	};

	shared_memory->yellow = 0;
	shared_memory->blue = 0;
	shared_memory->red = 0;

	set_font(EV3_FONT_MEDIUM);
	//	Motor init
	ev3_motor_config(sensors->LEFT_P, LARGE_MOTOR);
	ev3_motor_config(sensors->RIGHT_P, LARGE_MOTOR);
	//	Sensor init
	ev3_sensor_config(sensors->ULTRA_P, ULTRASONIC_SENSOR);
	ev3_sensor_config(sensors->COLOR_P, COLOR_SENSOR);
	ev3_sensor_config(sensors->TLEFT_P, TOUCH_SENSOR);
	ev3_sensor_config(sensors->TRIGHT_P, TOUCH_SENSOR);

	btConnect();
}
Esempio n. 2
0
int
nxtrike_init(void)
{
	syslog(LOG_NOTICE, "NXTrike initialization started");

    // Configure sensors
    ev3_sensor_config(gyro_sensor, GYRO_SENSOR);
    ev3_sensor_config(touch_sensor, TOUCH_SENSOR);
    ev3_sensor_config(SONIC_SENSOR, ULTRASONIC_SENSOR);
    ev3_sensor_config(LIGHT_SENSOR, COLOR_SENSOR);

    // Configure motors
    ev3_motor_config(LEFT_MOTOR/*left_motor*/, LARGE_MOTOR);
    ev3_motor_config(RIGHT_MOTOR/*right_motor*/, LARGE_MOTOR);
    ev3_motor_config(STEER_MOTOR/*steer_motor*/, LARGE_MOTOR);

	/* スピードとステアリング角度の初期化 */
	if(nxtrike_set_light_sensor_active() == NXTRIKE_ERROR){
		syslog(LOG_NOTICE, "light sensor is not activated");
	}

	/* モータのエンコーダ値の初期化 */
    ev3_motor_reset_counts(STEER_MOTOR);
    ev3_motor_reset_counts(LEFT_MOTOR);
    ev3_motor_reset_counts(RIGHT_MOTOR);

	/* スピードとステアリング角度の初期化 */
	nxtrike_set_speed(0);
	nxtrike_set_steer(0);

	/* 駆動タスクの起動 */
	act_tsk(NXTRIKE_DRIVE_TASK);
	/* ハンドラの起動 */
    assert(false /* not support yet */);
    act_tsk(NXTRIKE_DRIVE_HDR_TASK);
    act_tsk(NXTRIKE_TOUCH_SENSOR_HDR_TASK);
	//sta_cyc(NXTRIKE_DRIVE_HDR);
	//sta_cyc(NXTRIKE_TOUCH_SENSOR_HDR);

	syslog(LOG_NOTICE, "NXTrike has beed initialized");

	return NXTRIKE_OK;
}
Esempio n. 3
0
static mrb_value
mrb_mruby_motor_initialize(mrb_state *mrb, mrb_value self)
{
	mrb_int n1, n2;
	mrb_get_args(mrb, "ii", &n1, &n2);
	mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@port"), mrb_fixnum_value(n1));
	mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@type"), mrb_fixnum_value(n2));

	motor_port_t port = (motor_port_t)n1;
	motor_type_t type = (motor_type_t)n2;
	ev3_motor_config(port, type);
	return self;
}
Esempio n. 4
0
ER load_application(const void *mod_data, SIZE mod_data_sz) {
	ER ercd;

	real_kernel_evt_hdr = kernel_evt_hdr;

    ev3_motors_init(NONE_MOTOR, NONE_MOTOR, NONE_MOTOR, NONE_MOTOR);

	handle_btn_clicked = false;
	ev3_led_set_color(LED_GREEN);
	chg_status(STATUS_RUNNING);
	ercd = dmloader_ins_ldm(mod_data, mod_data_sz, 1);
	if (ercd != E_OK) {
		syslog(LOG_ERROR, "Failed to load app, ercd: %d", ercd);
	} else {
		SVC_PERROR(wai_sem(APP_TER_SEM));
		syslog(LOG_NOTICE, "HERE dmloader_rmv_ldm");
		SVC_PERROR(dmloader_rmv_ldm(1));
	}
	chg_status(STATUS_IDLE);
	for(ID i = EV3_PORT_A; i <= EV3_PORT_D; i++)
        ev3_motor_config(i, NONE_MOTOR);

	return ercd;
}
Esempio n. 5
0
void main_task(intptr_t unused) {
    ev3_lcd_set_font(EV3_FONT_MEDIUM);
    ev3_font_get_size(EV3_FONT_MEDIUM, &fontw, &fonth);

    memfile_t memfile;
    ev3_memfile_load("/test.bmp", &memfile);
    image_t image;
    ev3_image_load(&memfile, &image);
    ev3_lcd_draw_image(&image, 0, fonth * 2);

    //ev3_sta_cyc(TEST_EV3_CYC1);
    // Enable TEST_EV3_CYC2 for 5 seconds
    ev3_sta_cyc(TEST_EV3_CYC2);
    tslp_tsk(5000);
    ev3_stp_cyc(TEST_EV3_CYC2);
#if 0
    // Register button handlers
    ev3_button_set_on_clicked(BACK_BUTTON, button_clicked_handler, BACK_BUTTON);
    ev3_button_set_on_clicked(ENTER_BUTTON, button_clicked_handler, ENTER_BUTTON);
    ev3_button_set_on_clicked(LEFT_BUTTON, button_clicked_handler, LEFT_BUTTON);

    // Configure sensors
    ev3_sensor_config(gyro_sensor, GYRO_SENSOR);

    // Configure motors
    ev3_motor_config(left_motor, LARGE_MOTOR);
    ev3_motor_config(right_motor, LARGE_MOTOR);

    // Start task for self-balancing
    act_tsk(BALANCE_TASK);

    // Open Bluetooth file
    bt = ev3_serial_open_file(EV3_SERIAL_BT);
    assert(bt != NULL);

    // Start task for printing message while idle
	act_tsk(IDLE_TASK);

    while(1) {
    	uint8_t c = fgetc(bt);
    	sus_tsk(IDLE_TASK);
    	switch(c) {
    	case 'w':
    		if(motor_control_drive < 0)
    			motor_control_drive = 0;
    		else
    			motor_control_drive += 10;
    		fprintf(bt, "motor_control_drive: %d\n", motor_control_drive);
    		break;

    	case 's':
    		if(motor_control_drive > 0)
    			motor_control_drive = 0;
    		else
    			motor_control_drive -= 10;
    		fprintf(bt, "motor_control_drive: %d\n", motor_control_drive);
    		break;

    	case 'a':
    		if(motor_control_steer < 0)
    			motor_control_steer = 0;
    		else
    			motor_control_steer += 10;
    		fprintf(bt, "motor_control_steer: %d\n", motor_control_steer);
    		break;

    	case 'd':
    		if(motor_control_steer > 0)
    			motor_control_steer = 0;
    		else
    			motor_control_steer -= 10;
    		fprintf(bt, "motor_control_steer: %d\n", motor_control_steer);
    		break;

    	case 'h':
    		fprintf(bt, "==========================\n");
    		fprintf(bt, "Usage:\n");
    		fprintf(bt, "Press 'w' to speed up\n");
    		fprintf(bt, "Press 's' to speed down\n");
    		fprintf(bt, "Press 'a' to turn left\n");
    		fprintf(bt, "Press 'd' to turn right\n");
    		fprintf(bt, "Press 'i' for idle task\n");
    		fprintf(bt, "Press 'h' for this message\n");
    		fprintf(bt, "==========================\n");
    		break;

    	case 'i':
    		fprintf(bt, "Idle task started.\n");
    		rsm_tsk(IDLE_TASK);
    		break;

    	default:
    		fprintf(bt, "Unknown key '%c' pressed.\n", c);
    	}
    }
#endif
}
Esempio n. 6
0
/* メインタスク */
void main_task(intptr_t unused)
{
    signed char forward;      /* 前後進命令 */
    signed char turn;         /* 旋回命令 */
    signed char pwm_L, pwm_R; /* 左右モータPWM出力 */

    /* LCD画面表示 */
    ev3_lcd_fill_rect(0, 0, EV3_LCD_WIDTH, EV3_LCD_HEIGHT, EV3_LCD_WHITE);
    ev3_lcd_draw_string("ミヤウチ", 0, CALIB_FONT_HEIGHT*1);

    /* センサー入力ポートの設定 */
    ev3_sensor_config(sonar_sensor, ULTRASONIC_SENSOR);
    ev3_sensor_config(color_sensor, COLOR_SENSOR);
    ev3_color_sensor_get_reflect(color_sensor); /* 反射率モード */
    ev3_sensor_config(touch_sensor, TOUCH_SENSOR);
    ev3_sensor_config(gyro_sensor, GYRO_SENSOR);
    /* モーター出力ポートの設定 */
    ev3_motor_config(left_motor, LARGE_MOTOR);
    ev3_motor_config(right_motor, LARGE_MOTOR);
    ev3_motor_config(tail_motor, LARGE_MOTOR);
    ev3_motor_reset_counts(tail_motor);
    
     //キャリブレイト 白取得
    printf("Press the touch sensor to measure light intensity of WHITE.\n");
    while(!ev3_touch_sensor_is_pressed(touch_sensor));
    while(ev3_touch_sensor_is_pressed(touch_sensor));
    int white = ev3_color_sensor_get_reflect(color_sensor);
    printf("WHITE light intensity: %d.\n", white);

    //キャリブレイト 黒取得
    printf("Press the touch sensor to measure light intensity of BLACK.\n");
    while(!ev3_touch_sensor_is_pressed(touch_sensor));
    while(ev3_touch_sensor_is_pressed(touch_sensor));
    int black = ev3_color_sensor_get_reflect(color_sensor);
    printf("BLACK light intensity: %d.\n", black);

    //PID制御
    float lasterror = 0, integral = 0;
    float midpoint = (white - black) / 2 + black;

    /* Open Bluetooth file */
    bt = ev3_serial_open_file(EV3_SERIAL_BT);
    assert(bt != NULL);

    /* Bluetooth通信タスクの起動 */
    act_tsk(BT_TASK);

    ev3_led_set_color(LED_ORANGE); /* 初期化完了通知 */

    /* スタート待機 */
    while(1)
    {
    //尻尾さげる
        tail_control(TAIL_ANGLE_STAND_UP); 

        if (bt_cmd == 1)
        {
            //リモートスタート
            break; 
        }

        if (ev3_touch_sensor_is_pressed(touch_sensor) == 1)
        {
            //タッチセンサが押された
            break; 
        }

        tslp_tsk(1);
    }

    /* 走行モーターエンコーダーリセット */
    ev3_motor_reset_counts(left_motor);
    ev3_motor_reset_counts(right_motor);

    /* ジャイロセンサーリセット */
    ev3_gyro_sensor_reset(gyro_sensor);
    balancer.init(GYRO_OFFSET);                // <1>

	/* スタート(LED緑色) */
    ev3_led_set_color(LED_GREEN); 

    while(1)
	{
        int32_t motor_ang_l, motor_ang_r;
        int gyro, volt;

        if (ev3_button_is_pressed(BACK_BUTTON)) break;

        tail_control(TAIL_ANGLE_DRIVE); /* バランス走行用角度に制御 */

		forward = 30;
		
        float error = midpoint - ev3_color_sensor_get_reflect(color_sensor);
        integral = error + integral * 0.5;
        turn = 0.07 * error + 0.3 * integral + 1 * (error - lasterror);
           // float steer = 0.07 * error + 0.3 * integral + 1 * (error - lasterror);
           // ev3_motor_steer(left_motor, right_motor, 10, steer);
        lasterror = error;
        tslp_tsk(1);

        /* 倒立振子制御API に渡すパラメータを取得する */
        motor_ang_l = ev3_motor_get_counts(left_motor);
        motor_ang_r = ev3_motor_get_counts(right_motor);
        gyro = ev3_gyro_sensor_get_rate(gyro_sensor);
        volt = ev3_battery_voltage_mV();

        /* 倒立振子制御APIを呼び出し、倒立走行するための */
        /* 左右モータ出力値を得る */
        balancer.setCommand(forward, turn);   // <1>
        balancer.update(gyro, motor_ang_r, motor_ang_l, volt); // <2>
        pwm_L = balancer.getPwmRight();       // <3>
        pwm_R = balancer.getPwmLeft();        // <3>

        /* EV3ではモーター停止時のブレーキ設定が事前にできないため */
        /* 出力0時に、その都度設定する */
        if (pwm_L == 0)
        {
             ev3_motor_stop(left_motor, true);
        }
        else
        {
            ev3_motor_set_power(left_motor, (int)pwm_L);
        }

        if (pwm_R == 0)
        {
             ev3_motor_stop(right_motor, true);
        }
        else
        {
         ev3_motor_set_power(right_motor, (int)pwm_R);
        }
	

        tslp_tsk(4); /* 4msec周期起動 */
	}
    ev3_motor_stop(left_motor, false);
    ev3_motor_stop(right_motor, false);

    ter_tsk(BT_TASK);
    fclose(bt);

    ext_tsk();
}
void main_task(intptr_t unused) {
	int grey = (BLACK+WHITE)/2;
	int white_side = (BLACK+WHITE*2)/3;
	int black_side = (BLACK*3+WHITE)/4;
	int light = 0;
	int count = 0;
	unsigned int timeCounter = 1;
	int flag = 0;
	float turn_angle = 0.0;
	int distace = 0;
	int dflag = 0;

	pid *my_pid = pid_make(4.0, 0.2, 0.1, grey); //default pid 1.0 0.02 0.0
	pid *white_pid = pid_make(4.0, 0.2, 0.1, white_side);
	pid *black_pid = pid_make(4.0, 0.2, 0.1, black_side);

	turn_angle_structure *tas = turn_angle_constructor();

	ev3_lcd_fill_rect(0, 0, EV3_LCD_WIDTH, EV3_LCD_HEIGHT, EV3_LCD_WHITE);
	ev3_lcd_draw_string("EV3way ETRobcon 2015 KatLab", 0, CALIB_FONT_HEIGHT*1);
	ev3_lcd_draw_string("Ver.20150508", 0, CALIB_FONT_HEIGHT*2);
	ev3_lcd_draw_string("right side", 0, CALIB_FONT_HEIGHT*3);
    // Configure motors
  ev3_motor_config(STEER, LARGE_MOTOR);
  ev3_motor_config(DRIVE_L, LARGE_MOTOR);
  ev3_motor_config(DRIVE_R, LARGE_MOTOR);

    // Configure sensors
  ev3_sensor_config(IR, COLOR_SENSOR);
  ev3_sensor_config(TOUCH, TOUCH_SENSOR);
	ev3_sensor_config(GYRO, GYRO_SENSOR);

  ev3_led_set_color(LED_GREEN);

	ev3_motor_reset_counts(STEER);
	ev3_motor_reset_counts(DRIVE_L);
	ev3_motor_reset_counts(DRIVE_R);


	ev3_motor_set_power(STEER,0);


	ev3_gyro_sensor_reset(GYRO);

	while(!ev3_touch_sensor_is_pressed(TOUCH)){
	  //tslp_tsk(500); /* 500msec wait */
	}
	while(!ev3_button_is_pressed(BACK_BUTTON)){
	  light = ev3_color_sensor_get_reflect(IR);
	  count = ev3_motor_get_counts(STEER);

	  switch(changeStyle){
	  case 0://始めのカーブ差し掛かりまで
	    count = ev3_motor_get_counts(STEER);
	    forward = 55;
	    pid_input(my_pid, ev3_color_sensor_get_reflect(IR));
	    turn = (signed char) pid_get_output(*my_pid);

	    if(turn > 100){
	      turn = 100;
	    }else if(turn < -100){
	      turn = -100;
	    }

	    turn = -turn;

	    if(count<MAX_STEERING_ANGLE && count>-MAX_STEERING_ANGLE)
	      {
		ev3_motor_set_power(STEER, turn/2);
	      }else if(count<=-MAX_STEERING_ANGLE && turn>=0){
	      ev3_motor_set_power(STEER, turn/2);
	    }else if(count>=MAX_STEERING_ANGLE && turn<0){
	      ev3_motor_set_power(STEER, turn/2);
	    }else{
	      ev3_motor_stop(STEER, true);
	    }


	    if((ev3_motor_get_counts(DRIVE_L)+ev3_motor_get_counts(DRIVE_R))/2 < (-7300 + 2400)){
	      ev3_lcd_draw_string("changeStyle 1", 0, 70);
	      changeStyle = 1;
	    }

	    break;
	  case 1: //カーブ白より走行
	    count = ev3_motor_get_counts(STEER);
	    forward = 26;
	    pid_input(white_pid, ev3_color_sensor_get_reflect(IR));
	    turn = (signed char) pid_get_output(*white_pid);

	    if(turn > 0){
	      turn = turn*2;
	    }
	    if(turn > 100){
	      turn = 100;
	    }else if(turn < -100){
	      turn = -100;
	    }

	    turn = -turn;

	    if(count<MAX_STEERING_ANGLE && count>-MAX_STEERING_ANGLE)
	      {
		ev3_motor_set_power(STEER, turn/2);
	      }else if(count<=-MAX_STEERING_ANGLE && turn>=0){
	      ev3_motor_set_power(STEER, turn/2);
	    }else if(count>=MAX_STEERING_ANGLE && turn<0){
	      ev3_motor_set_power(STEER, turn/2);
	    }else{
	      ev3_motor_stop(STEER, true);
	    }


	    if((ev3_motor_get_counts(DRIVE_L)+ev3_motor_get_counts(DRIVE_R))/2 < (-9200 + 2400)){
	      changeStyle = 2;
	    }


	    break;
	  case 2://段差検知まで 黒より走行
	    count = ev3_motor_get_counts(STEER);
	    forward = 30;
	    pid_input(black_pid, ev3_color_sensor_get_reflect(IR));
	    turn = (signed char) pid_get_output(*black_pid);

	    if(turn < 0){
	      turn = turn*2;
	    }
	    if(turn > 100){
	      turn = 100;
	    }else if(turn < -100){
	      turn = -100;
	    }

	    turn = -turn;

	    if(count<MAX_STEERING_ANGLE && count>-MAX_STEERING_ANGLE)
	      {
		ev3_motor_set_power(STEER, turn/2);
	      }else if(count<=-MAX_STEERING_ANGLE && turn>=0){
	      ev3_motor_set_power(STEER, turn/2);
	    }else if(count>=MAX_STEERING_ANGLE && turn<0){
	      ev3_motor_set_power(STEER, turn/2);
	    }else{
	      ev3_motor_stop(STEER, true);
	    }


	    if(stepDetective(12)==1){
	      changeStyle = 3;
	      ev3_lcd_draw_string("changeStyle Now", 0, 70);
	      flag = 1;
	    }

	    break;
	  case 3://二本橋クリアする
	    if(bridge(timeCounter) == 1){
	      changeStyle = 13;
	    }
	    timeCounter++;
	    break;
	  case 4://止まる
	    if(ev3_color_sensor_get_color(IR) == 6 || ev3_color_sensor_get_color(IR) == 3){
	      ev3_motor_set_power(STEER,0);
	      ev3_motor_set_power(DRIVE_L,0);
	      ev3_motor_set_power(DRIVE_R,0);
	    }else{
	      ev3_motor_set_power(STEER,0);
	      ev3_motor_set_power(DRIVE_L,-20);
	      ev3_motor_set_power(DRIVE_R,-20);
	    }
	    break;

	  case 5://バーコード終了後ライントレース
	    count = ev3_motor_get_counts(STEER);
	    forward = 25;
	    pid_input(my_pid, ev3_color_sensor_get_reflect(IR));
	    turn = (signed char) pid_get_output(*my_pid);

	    if(turn > 100){
	      turn = 100;
	    }else if(turn < -100){
	      turn = -100;
	    }

	    turn = -turn;

	    if(count<MAX_STEERING_ANGLE && count>-MAX_STEERING_ANGLE)
	      {
		ev3_motor_set_power(STEER, turn/2);
	      }else if(count<=-MAX_STEERING_ANGLE && turn>=0){
	      ev3_motor_set_power(STEER, turn/2);
	    }else if(count>=MAX_STEERING_ANGLE && turn<0){
	      ev3_motor_set_power(STEER, turn/2);
	    }else{
	      ev3_motor_stop(STEER, true);
	    }

	    if((ev3_motor_get_counts(DRIVE_L)+ev3_motor_get_counts(DRIVE_R))/2 < -300){
	      changeStyle = 6;
	      flag = 1;
	      ev3_motor_reset_counts(DRIVE_L);
	      ev3_motor_reset_counts(DRIVE_R);
	    }
	    break;
	  case 6://まっすぐ走る
	    ev3_motor_set_power(STEER,0);
	    ev3_motor_set_power(DRIVE_L,-40);
	    ev3_motor_set_power(DRIVE_R,-40);
	    if((ev3_motor_get_counts(DRIVE_L)+ev3_motor_get_counts(DRIVE_R))/2 < -750){
	      changeStyle = 7;
	      ev3_motor_set_power(DRIVE_L,0);
	      ev3_motor_set_power(DRIVE_R,0);
	      timeCounter = 0;
	    }
	    break;

	  case 7://前輪を真横にする
	    if(timeCounter <= 9){
	      ev3_motor_rotate(STEER,90,50,true);
	    }else if(timeCounter > 9){
	      changeStyle = 8;
	      ev3_lcd_draw_string("changeStyle Now", 0, 70);
	      timeCounter = 0;
	    }
	    timeCounter++;
	    break;
	  case 8://車体を左向きに
	    turn_angle = turn_angle_detection(tas);
	    if(turn_angle >= -76.0){
	      ev3_motor_set_power(STEER,0);
	      ev3_motor_set_power(DRIVE_L,30);
	      ev3_motor_set_power(DRIVE_R,-20);
	    }else if(turn_angle < -76.0){
	      ev3_motor_set_power(STEER,0);
	      ev3_motor_set_power(DRIVE_L,0);
	      ev3_motor_set_power(DRIVE_R,0);
	      changeStyle = 9;
	    }
	    break;
	  case 9://前輪を戻す
	    ev3_motor_rotate(STEER,-900,50,true);
	    changeStyle = 10;
	    ev3_lcd_draw_string("changeStyle Now", 0, 70);
	    ev3_motor_reset_counts(DRIVE_L);
	    ev3_motor_reset_counts(DRIVE_R);
	    break;

	  case 10://使用未確定エリアに乗る
	    ev3_motor_set_power(DRIVE_L,-50);
	    ev3_motor_set_power(DRIVE_R,-50);
	    if((ev3_motor_get_counts(DRIVE_L)+ev3_motor_get_counts(DRIVE_R))/2 < -500){
	      timeCounter++;
	      ev3_motor_set_power(DRIVE_L,0);
	      ev3_motor_set_power(DRIVE_R,0);
	    }
	    if(timeCounter >= 100){
	      timeCounter=0;
	      changeStyle=11;
	    }
	    break;
	  case 11://ウィリー走行に切り替え
	    if(tail_run(timeCounter) == 1){
	      changeStyle = 12;
	      timeCounter = 0;
	    }else{
	      timeCounter++;
	    }

	    break;

	  case 12://止まる
	    ev3_motor_set_power(STEER,0);
	    ev3_motor_set_power(DRIVE_L,0);
	    ev3_motor_set_power(DRIVE_R,0);
	    break;

	  case 13://ライン復帰
	    ev3_motor_rotate(STEER,940,50,true);
	    changeStyle = 14;
	    ev3_lcd_draw_string("changeStyle Now", 0, 70);
	    timeCounter = 1;
	    break;
	  case 14:
	    if(timeCounter < 37){
	      ev3_motor_set_power(STEER,0);
	      ev3_motor_set_power(DRIVE_L,30);
	      ev3_motor_set_power(DRIVE_R,-30);
	    }else if(timeCounter >= 37){
	      ev3_motor_set_power(STEER,0);
	      ev3_motor_set_power(DRIVE_L,0);
	      ev3_motor_set_power(DRIVE_R,0);
	      changeStyle = 15;
	      timeCounter = 0;
	    }timeCounter++;
	    break;
	  case 15:
	    ev3_motor_rotate(STEER,-1000,50,true);
	    changeStyle = 16;
	    ev3_lcd_draw_string("changeStyle Now", 0, 70);
	    break;
	  case 16:

	    ev3_motor_set_power(STEER,0);
	    if(timeCounter <= 20){
	      ev3_motor_set_power(DRIVE_L,15);
	      ev3_motor_set_power(DRIVE_R,15);
	    }else if(timeCounter > 20 && (timeCounter -20) /60 % 2 == 1){
	      ev3_motor_set_power(DRIVE_L,15);
	      ev3_motor_set_power(DRIVE_R,15);
	    }else if(timeCounter > 20 && (timeCounter - 20) /60 % 2 == 0){
	      ev3_motor_set_power(DRIVE_L,-15);
	      ev3_motor_set_power(DRIVE_R,-15);
	    }
	    timeCounter++;
	    if(ev3_color_sensor_get_color(IR) == 1){
	      changeStyle = 30;
	      ev3_motor_set_power(DRIVE_L,0);
	      ev3_motor_set_power(DRIVE_R,0);
	      timeCounter = 0;
	    }
	    break;
	  case 17:
	    ev3_motor_rotate(STEER,830,50,true);
	    changeStyle = 18;
	    ev3_lcd_draw_string("changeStyle Now", 0, 70);
	    break;
	  case 18:
	    if(ev3_color_sensor_get_reflect(IR) > white_side){
	      ev3_motor_set_power(STEER,0);
	      ev3_motor_set_power(DRIVE_L,-6);
	      ev3_motor_set_power(DRIVE_R,6);
	    }else if(ev3_color_sensor_get_reflect(IR) < white_side){
	      ev3_motor_set_power(STEER,0);
	      ev3_motor_set_power(DRIVE_L,0);
	      ev3_motor_set_power(DRIVE_R,0);
	      changeStyle = 19;
	    }
	    break;
	  case 19:
	    ev3_motor_rotate(STEER,-950,40,true);
	    changeStyle = 20;
	    ev3_lcd_draw_string("changeStyle Now", 0, 70);
	    break;
	  case 20:
	    ev3_motor_set_power(STEER,0);
	    ev3_motor_set_power(DRIVE_L,-17);
	    ev3_motor_set_power(DRIVE_R,-15);
	    if(ev3_color_sensor_get_color(IR) == 1){
	      changeStyle = 21;
	      flag = 0;
	      ev3_motor_reset_counts(DRIVE_L);
	      ev3_motor_reset_counts(DRIVE_R);
	    }
	    break;
	  case 21:
	    count = ev3_motor_get_counts(STEER);
	    forward = 14;
	    pid_input(my_pid, ev3_color_sensor_get_reflect(IR));
	    turn = (signed char) pid_get_output(*my_pid);

	    if(turn > 100){
	      turn = 100;
	    }else if(turn < -100){
	      turn = -100;
	    }


	    if(count<MAX_STEERING_ANGLE && count>-MAX_STEERING_ANGLE)
	      {
		ev3_motor_set_power(STEER, turn/2);
	      }else if(count<=-MAX_STEERING_ANGLE && turn>=0){
	      ev3_motor_set_power(STEER, turn/2);
	    }else if(count>=MAX_STEERING_ANGLE && turn<0){
	      ev3_motor_set_power(STEER, turn/2);
	    }else{
	      ev3_motor_stop(STEER, true);
	    }

	    if((ev3_motor_get_counts(DRIVE_L)+ev3_motor_get_counts(DRIVE_R))/2 < -3000){
	      changeStyle = 5;
	      ev3_motor_reset_counts(DRIVE_L);
	      ev3_motor_reset_counts(DRIVE_R);
	    }

	    break;
	  case 30:
	    ev3_motor_set_power(STEER,0);
	    if(timeCounter <= 65){
	      ev3_motor_set_power(DRIVE_L,-17);
	      ev3_motor_set_power(DRIVE_R,-17);
	    }else if(timeCounter > 65 && timeCounter <= 120){
	      ev3_motor_set_power(DRIVE_L,0);
	      ev3_motor_set_power(DRIVE_R,0);
	    }else if(timeCounter > 120){
	      changeStyle = 17;
	      timeCounter = 0;
	    }			timeCounter++;
	  default:

	    break;
	  }
	  if(flag==0){
	    ev3_motor_steer(DRIVE_L, DRIVE_R, -forward, -turn);
	  }

	  tslp_tsk(10); /* 10msec wait */

	}

	ev3_motor_stop(STEER, false);
	ev3_motor_stop(DRIVE_L, false);
	ev3_motor_stop(DRIVE_R, false);

	ext_tsk();
}
void main_task(intptr_t unused) {
  int grey = (BLACK+WHITE)/2;
  //int light = 0;
  //int count = 0;

  // Configure motors
  ev3_motor_config(STEER, LARGE_MOTOR);
  ev3_motor_config(DRIVE_L, LARGE_MOTOR);
  ev3_motor_config(DRIVE_R, LARGE_MOTOR);

  // Configure sensors
  ev3_sensor_config(IR, COLOR_SENSOR);
  ev3_sensor_config(TOUCH, TOUCH_SENSOR);

  ev3_led_set_color(LED_GREEN);

  ev3_motor_reset_counts(STEER);
  ev3_motor_reset_counts(DRIVE_L);
  ev3_motor_reset_counts(DRIVE_R);

  while(!ev3_touch_sensor_is_pressed(TOUCH));
  tslp_tsk(500); /* 500msec wait */

  char str[20];
  sprintf(str, "%d", 1);
  ev3_lcd_draw_string(str, 0, 0);


  /*
  //戦略の初期化(もろもろやってくれる
  //上のSTRATEGYさえ変えれば、該当の戦略に対する戦術がセットされる
  Strategy* strategy = Strategy_make(STRATEGY);

  //ライントレースクラスのインスタンス
  //各メンバ変数へのset関数を用いて走りをコントロール
  //これ以降もset関数で値を各戦略内で自由に変えることが出来る
  Line_tracer* line_tracer = Line_tracer_make();
  Line_tracer_init_pid(line_tracer, 4.0, 0.2, 0.1, grey);
  Line_tracer_set_current_edge(line_tracer, right);
  Line_tracer_set_target_grey_point(line_tracer, grey);
  Line_tracer_set_forward(line_tracer, 0);
  Line_tracer_set_turn(line_tracer, 0);
  */

  strategy = Strategy_make(STRATEGY);
  line_tracer = Line_tracer_make();
  Line_tracer_init_pid(line_tracer, 4.0, 0.2, 0.1, grey);
  Line_tracer_set_current_edge(line_tracer, right);
  Line_tracer_set_target_grey_point(line_tracer, grey);
  Line_tracer_set_forward(line_tracer, 0);
  Line_tracer_set_turn(line_tracer, 0);

  while(!ev3_button_is_pressed(BACK_BUTTON)) {
    Strategy_update(strategy);
    Line_tracer_output(line_tracer);
    tslp_tsk(10); /* 10msec wait */
    /*
    light = ev3_color_sensor_get_reflect(IR);
    count = ev3_motor_get_counts(STEER);
    if(light>grey){
      if(count<MAX_STEERING_ANGLE){
	ev3_motor_set_power(STEER, 100);
      }else{
	ev3_motor_stop(STEER, true);
      }
      ev3_motor_set_power(DRIVE_L, -DRIVING_POWER);
      ev3_motor_set_power(DRIVE_R,   1);
    }else{
      if(count>-MAX_STEERING_ANGLE){
	ev3_motor_set_power(STEER, -100);
      }else{
	ev3_motor_stop(STEER, true);
      }
      ev3_motor_set_power(DRIVE_L,   1);
      ev3_motor_set_power(DRIVE_R, -DRIVING_POWER);
    */
  }

  ev3_motor_stop(STEER, false);
  ev3_motor_stop(DRIVE_L, false);
  ev3_motor_stop(DRIVE_R, false);

  ext_tsk();
}
Esempio n. 9
0
/* メインタスク */
void main_task(intptr_t unused)
{
    signed char forward;      /* 前後進命令 */
    signed char turn;         /* 旋回命令 */
    signed char pwm_L, pwm_R; /* 左右モータPWM出力 */
	static int turn = 0;

    /* LCD画面表示 */
    ev3_lcd_fill_rect(0, 0, EV3_LCD_WIDTH, EV3_LCD_HEIGHT, EV3_LCD_WHITE);
    ev3_lcd_draw_string("ETミヤウチ", 0, CALIB_FONT_HEIGHT*1);

    /* センサー入力ポートの設定 */
    ev3_sensor_config(sonar_sensor, ULTRASONIC_SENSOR);
    ev3_sensor_config(color_sensor, COLOR_SENSOR);
    ev3_color_sensor_get_reflect(color_sensor); /* 反射率モード */
    ev3_sensor_config(touch_sensor, TOUCH_SENSOR);
    ev3_sensor_config(gyro_sensor, GYRO_SENSOR);
    /* モーター出力ポートの設定 */
    ev3_motor_config(left_motor, LARGE_MOTOR);
    ev3_motor_config(right_motor, LARGE_MOTOR);
    ev3_motor_config(tail_motor, LARGE_MOTOR);
    ev3_motor_reset_counts(tail_motor);

    /* Open Bluetooth file */
    bt = ev3_serial_open_file(EV3_SERIAL_BT);
    assert(bt != NULL);

    /* Bluetooth通信タスクの起動 */
    act_tsk(BT_TASK);

    ev3_led_set_color(LED_ORANGE); /* 初期化完了通知 */

    /* スタート待機 */
    while(1)
    {
        tail_control(TAIL_ANGLE_STAND_UP); /* 完全停止用角度に制御 */

        if (bt_cmd == 1)
        {
            break; /* リモートスタート */
        }

        if (ev3_touch_sensor_is_pressed(touch_sensor) == 1)
        {
            break; /* タッチセンサが押された */
        }

        tslp_tsk(10); /* 10msecウェイト */
    }

    /* 走行モーターエンコーダーリセット */
    ev3_motor_reset_counts(left_motor);
    ev3_motor_reset_counts(right_motor);

    /* ジャイロセンサーリセット */
    ev3_gyro_sensor_reset(gyro_sensor);
    balancer.init(GYRO_OFFSET);                // <1>

    ev3_led_set_color(LED_GREEN); /* スタート通知 */

    /**
    * Main loop for the self-balance control algorithm
    */
    while(1)
    {
        int32_t motor_ang_l, motor_ang_r;
        int gyro, volt;
    	
    	turn = (ev3_color_sensor_get_reflect(color_sensor) - (LIGHT_WHITE + LIGHT_BLACK)/2) * KP;
        	

        if (ev3_button_is_pressed(BACK_BUTTON)) break;

        tail_control(TAIL_ANGLE_DRIVE); /* バランス走行用角度に制御 */

        if (sonar_alert() == 1) /* 障害物検知 */
        {
            forward = turn = 0; /* 障害物を検知したら停止 */
        }
        else
        {
            forward = 30; /* 前進命令 */
 		if (100 < turn) {
            turn = 100.0;
        } else if (turn < -100) {
            turn = -100.0;
        }
        }

        /* 倒立振子制御API に渡すパラメータを取得する */
        motor_ang_l = ev3_motor_get_counts(left_motor);
        motor_ang_r = ev3_motor_get_counts(right_motor);
        gyro = ev3_gyro_sensor_get_rate(gyro_sensor);
        volt = ev3_battery_voltage_mV();

        /* 倒立振子制御APIを呼び出し、倒立走行するための */
        /* 左右モータ出力値を得る */
        balancer.setCommand(forward, turn);   // <1>
        balancer.update(gyro, motor_ang_r, motor_ang_l, volt); // <2>
        pwm_L = balancer.getPwmRight();       // <3>
        pwm_R = balancer.getPwmLeft();        // <3>

        /* EV3ではモーター停止時のブレーキ設定が事前にできないため */
        /* 出力0時に、その都度設定する */
        if (pwm_L == 0)
        {
             ev3_motor_stop(left_motor, true);
        }
        else
        {
            ev3_motor_set_power(left_motor, (int)pwm_L);
        }

        if (pwm_R == 0)
        {
             ev3_motor_stop(right_motor, true);
        }
        else
        {
            ev3_motor_set_power(right_motor, (int)pwm_R);
        }

        tslp_tsk(4); /* 4msec周期起動 */
    }
    ev3_motor_stop(left_motor, false);
    ev3_motor_stop(right_motor, false);

    ter_tsk(BT_TASK);
    fclose(bt);

    ext_tsk();
}