2014-git-work/arduino/hanzo2_1/hanzo2_1.ino

307 lines
7.8 KiB
Arduino
Raw Normal View History

2012-09-14 00:31:41 +09:00
/*
2.1
[7 654321 0]
| |
| |
[]---{}---[]
/ .\
/ | \
[]+--{}--+[]
(c)2012 kou029w - MIT License [http://kou029w.appspot.com/mit-license.txt]
2012-09-14 00:31:41 +09:00
*/
#include <Servo.h>
#include "Motor.h"
//#define LINE_BLACK //白地・黒ライン有効
2012-09-14 13:52:25 +09:00
const unsigned long LAP_COUNT = 854;
2012-09-14 16:54:51 +09:00
const unsigned long TARGET_NONE_COUNT = LAP_COUNT/10;
2012-09-14 00:31:41 +09:00
const int LANCE_ANGLE0 = 0; //度
const int LANCE_ANGLE1 = 43;
const int LANCE_ANGLE2 = 54;
const int LANCE_ANGLE3 = -33;
const int LANCE_ANGLE4 = 34;
const unsigned int LANCE_INTERVAL = 70; //ms
const byte SPEED_DEFAULT = 255;
2012-09-14 00:31:41 +09:00
2012-09-14 13:52:25 +09:00
const int HANDLE_DEGREE_DEFAULT = 90; //度
const unsigned int HANDLE_MIN = ( 800+30); //us
const unsigned int HANDLE_MAX = (2300+30); //us
2012-10-02 01:31:00 +09:00
const float HANDLE_KP = 4.0;
2012-09-14 00:31:41 +09:00
2012-09-14 13:52:25 +09:00
const int LANCE_DEGREE_DEFAULT = 90; //度
2012-09-29 16:16:33 +09:00
const unsigned int LANCE_MIN = ( 544-62); //us
const unsigned int LANCE_MAX = (2400-62); //us
2012-09-14 00:31:41 +09:00
const byte PIN_LED = 4;
const byte PIN_ROT = 2;
const byte PIN_SENSOR_0 = 12;
const byte PIN_SENSOR_1 = 13;
const byte PIN_SENSOR_2 = 14;
const byte PIN_SENSOR_3 = 15;
const byte PIN_SENSOR_4 = 16;
const byte PIN_SENSOR_5 = 17;
const byte PIN_SENSOR_6 = 18;
const byte PIN_SENSOR_7 = 19;
const byte PIN_SERVO_HANDLE = 10;
const byte PIN_SERVO_LANCE = 9;
const byte PIN_MOTOR_LEFT_1 = 5;
2012-09-14 13:52:25 +09:00
const byte PIN_MOTOR_LEFT_2 = 7;
2012-09-14 00:31:41 +09:00
const byte PIN_MOTOR_RIGHT_1 = 6;
2012-09-14 13:52:25 +09:00
const byte PIN_MOTOR_RIGHT_2 = 8;
2012-09-14 00:31:41 +09:00
2012-09-14 16:54:51 +09:00
enum {
MODE_STOP, //停止
MODE_TRACE, //標準
MODE_LEFT, //左カーブ
MODE_TARGET_PARALLEL1, //平行標的1
MODE_TARGET_PARALLEL2, //平行標的2
MODE_TARGET_RIGHT_VERTICAL, //右垂直標的
MODE_TARGET_LEFT_VERTICAL, //左垂直標的
MODE_TARGET_CYLINDER //円筒標的
};
2012-09-14 00:31:41 +09:00
const byte MASK_MODE_TRACE = 0b01111110;
const byte MASK_CHECK_MARKER = 0b10000001;
const byte MASK_CHECK_MARKER_RIGHT = 0b00000001;
const byte MASK_CHECK_MARKER_LEFT = 0b10000000;
/**************************************/
Servo servoHandle;
Servo servoLance;
Motor motorR;
Motor motorL;
byte mode;
2012-09-29 16:16:33 +09:00
volatile unsigned long counter = 0;
2012-09-14 16:54:51 +09:00
unsigned long counterOld;
2012-09-14 00:31:41 +09:00
/**************************************/
void trace(byte sensor){
2012-09-14 16:54:51 +09:00
switch(sensor&MASK_MODE_TRACE){
2012-09-14 00:31:41 +09:00
case 0b01000000:
2012-10-02 01:31:00 +09:00
handle(-5*HANDLE_KP);
2012-09-29 16:16:33 +09:00
motorMode(GO, GO, 0, SPEED_DEFAULT);
2012-09-14 00:31:41 +09:00
break;
case 0b01100000:
2012-10-02 01:31:00 +09:00
handle(-4*HANDLE_KP);
2012-09-29 16:34:17 +09:00
motorMode(GO, GO, SPEED_DEFAULT/4, SPEED_DEFAULT);
2012-09-14 00:31:41 +09:00
break;
case 0b00100000:
2012-10-02 01:31:00 +09:00
handle(-3*HANDLE_KP);
2012-09-14 16:54:51 +09:00
motorMode(GO, GO, SPEED_DEFAULT, SPEED_DEFAULT);
2012-09-14 00:31:41 +09:00
break;
case 0b00110000:
2012-10-02 01:31:00 +09:00
handle(-2*HANDLE_KP);
2012-09-14 16:54:51 +09:00
motorMode(GO, GO, SPEED_DEFAULT, SPEED_DEFAULT);
2012-09-14 00:31:41 +09:00
break;
case 0b00010000:
2012-10-02 01:31:00 +09:00
handle(-1*HANDLE_KP);
2012-09-14 16:54:51 +09:00
motorMode(GO, GO, SPEED_DEFAULT, SPEED_DEFAULT);
2012-09-14 00:31:41 +09:00
break;
case 0b00011000:
2012-09-14 13:52:25 +09:00
handle(0);
2012-09-14 16:54:51 +09:00
motorMode(GO, GO, SPEED_DEFAULT, SPEED_DEFAULT);
2012-09-14 00:31:41 +09:00
break;
case 0b00001000:
2012-10-02 01:31:00 +09:00
handle(1*HANDLE_KP);
2012-09-14 16:54:51 +09:00
motorMode(GO, GO, SPEED_DEFAULT, SPEED_DEFAULT);
2012-09-14 00:31:41 +09:00
break;
case 0b00001100:
2012-10-02 01:31:00 +09:00
handle(2*HANDLE_KP);
2012-09-14 16:54:51 +09:00
motorMode(GO, GO, SPEED_DEFAULT, SPEED_DEFAULT);
2012-09-14 00:31:41 +09:00
break;
case 0b00000100:
2012-10-02 01:31:00 +09:00
handle(3*HANDLE_KP);
2012-09-14 16:54:51 +09:00
motorMode(GO, GO, SPEED_DEFAULT, SPEED_DEFAULT);
2012-09-14 00:31:41 +09:00
break;
case 0b00000110:
2012-10-02 01:31:00 +09:00
handle(4*HANDLE_KP);
2012-09-14 16:54:51 +09:00
motorMode(GO, GO, SPEED_DEFAULT, SPEED_DEFAULT);
2012-09-14 00:31:41 +09:00
break;
case 0b00000010:
2012-10-02 01:31:00 +09:00
handle(5*HANDLE_KP);
2012-09-14 16:54:51 +09:00
motorMode(GO, GO, SPEED_DEFAULT, SPEED_DEFAULT);
2012-09-14 00:31:41 +09:00
break;
}
}
2012-09-15 13:24:31 +09:00
void motorMode(byte leftMode, byte rightMode, byte leftSpeed, byte rightSpeed){
motorL.mode( leftMode, leftSpeed);
motorR.mode(rightMode, rightSpeed);
2012-09-14 00:31:41 +09:00
}
2012-09-14 13:52:25 +09:00
void handle(int degree){
servoHandle.write(HANDLE_DEGREE_DEFAULT + degree);
2012-09-14 00:31:41 +09:00
}
2012-09-14 13:52:25 +09:00
void lance(int degree){
servoLance.write(LANCE_DEGREE_DEFAULT - degree);
2012-09-14 00:31:41 +09:00
}
/* ロータリーエンコーダーの変化を見る
todo:使
*/
2012-09-14 00:31:41 +09:00
void count(){
2012-09-14 13:52:25 +09:00
static byte rot[2] = {0}; //rot[0]:今の状態, rot[1]:前回の状態
rot[0] = digitalRead(PIN_ROT);
if(rot[0] != rot[1]){
rot[1] = rot[0];
2012-09-14 00:31:41 +09:00
counter++;
}
}
2012-09-29 16:16:33 +09:00
int checkMarker(byte sensor){
int mode = MODE_TRACE;
switch(sensor & MASK_CHECK_MARKER){
case 0b10000001:
counterOld = counter;
mode = MODE_LEFT;
break;
case 0b00000001:
if(counter%LAP_COUNT > LAP_COUNT/2){
mode = MODE_TARGET_RIGHT_VERTICAL;
}else{
mode = MODE_TARGET_PARALLEL1;
}
break;
case 0b10000000:
mode = MODE_TARGET_LEFT_VERTICAL;
break;
}
return mode;
}
2012-09-14 00:31:41 +09:00
void sensorInit(){
pinMode(PIN_SENSOR_0, INPUT);
pinMode(PIN_SENSOR_1, INPUT);
pinMode(PIN_SENSOR_2, INPUT);
pinMode(PIN_SENSOR_3, INPUT);
pinMode(PIN_SENSOR_4, INPUT);
pinMode(PIN_SENSOR_5, INPUT);
pinMode(PIN_SENSOR_6, INPUT);
pinMode(PIN_SENSOR_7, INPUT);
}
byte sensorRead(){
2012-09-15 13:24:31 +09:00
byte sensor = 0; //黒が1、白が0
if(digitalRead(PIN_SENSOR_0)) sensor += 0b00000001;
if(digitalRead(PIN_SENSOR_1)) sensor += 0b00000010;
if(digitalRead(PIN_SENSOR_2)) sensor += 0b00000100;
if(digitalRead(PIN_SENSOR_3)) sensor += 0b00001000;
if(digitalRead(PIN_SENSOR_4)) sensor += 0b00010000;
if(digitalRead(PIN_SENSOR_5)) sensor += 0b00100000;
if(digitalRead(PIN_SENSOR_6)) sensor += 0b01000000;
if(digitalRead(PIN_SENSOR_7)) sensor += 0b10000000;
return sensor;
}
2012-09-14 00:31:41 +09:00
void setup() {
//サーボ
servoHandle.attach(PIN_SERVO_HANDLE, HANDLE_MIN, HANDLE_MAX);
servoLance.attach(PIN_SERVO_LANCE, LANCE_MIN, LANCE_MAX);
handle(0);
lance(0);
//モーター
motorL.attach( PIN_MOTOR_LEFT_1, PIN_MOTOR_LEFT_2);
motorR.attach(PIN_MOTOR_RIGHT_1, PIN_MOTOR_RIGHT_2);
2012-09-14 16:54:51 +09:00
motorL.mode(STOP);
motorR.mode(STOP);
2012-09-14 00:31:41 +09:00
//センサー
sensorInit();
//エンコーダー
pinMode(PIN_ROT, INPUT);
//LED
pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_LED, HIGH);
//モード
2012-09-14 16:54:51 +09:00
mode = MODE_TRACE;
2012-09-14 00:31:41 +09:00
delay(1000);
/* スタート */
digitalWrite(PIN_LED, LOW);
handle(0);
lance(0);
2012-09-14 16:54:51 +09:00
motorMode(GO, GO, SPEED_DEFAULT, SPEED_DEFAULT);
2012-09-14 00:31:41 +09:00
}
void loop(){
2012-09-15 12:21:53 +09:00
/* センサーを見る */
2012-09-29 16:34:17 +09:00
byte sensor = 0;
sensor = ~sensorRead(); //ライン(白)が1、地面(黒)が0
2012-09-15 12:21:53 +09:00
count(); //ロータリーエンコーダーの変化を見る
/* 全体の動作を決定する */
2012-09-14 16:54:51 +09:00
switch(mode){
2012-09-15 12:21:53 +09:00
case MODE_TARGET_RIGHT_VERTICAL: //右垂直標的
2012-09-14 16:54:51 +09:00
lance(LANCE_ANGLE4);
mode = MODE_TRACE;
2012-09-14 13:52:25 +09:00
break;
2012-09-15 12:21:53 +09:00
case MODE_TARGET_LEFT_VERTICAL: //左垂直標的
2012-09-14 16:54:51 +09:00
lance(LANCE_ANGLE3);
mode = MODE_TRACE;
2012-09-14 13:52:25 +09:00
break;
2012-09-15 12:21:53 +09:00
case MODE_TARGET_PARALLEL1: //平行標的1
2012-09-14 16:54:51 +09:00
lance(LANCE_ANGLE1);
mode = MODE_TRACE;
2012-09-14 13:52:25 +09:00
break;
2012-09-15 12:21:53 +09:00
case MODE_TARGET_PARALLEL2: //平行標的2
2012-09-14 16:54:51 +09:00
lance(LANCE_ANGLE2);
mode = MODE_TRACE;
2012-09-14 13:52:25 +09:00
break;
2012-09-14 00:31:41 +09:00
case MODE_TRACE:
trace(sensor);
2012-09-14 16:54:51 +09:00
mode = checkMarker(sensor);
2012-09-29 16:16:33 +09:00
// 6周する前に、円筒標的を狙いに行く
// if( counter > LAP_COUNT*6 - LAP_COUNT/14 ){
// mode = MODE_TARGET_CYLINDER;
2012-09-29 16:34:17 +09:00
// }
if(counter > LAP_COUNT*3){
2012-09-29 16:16:33 +09:00
mode = MODE_STOP;
2012-09-14 00:31:41 +09:00
}
break;
2012-09-14 16:54:51 +09:00
case MODE_LEFT: //左カーブ
trace(sensor);
lance(LANCE_ANGLE0);
if(counter - counterOld > TARGET_NONE_COUNT) mode = MODE_TRACE;
break;
case MODE_TARGET_CYLINDER: //円筒標的
2012-09-14 00:31:41 +09:00
digitalWrite(PIN_LED, HIGH);
2012-09-14 13:52:25 +09:00
handle(0);
2012-09-14 16:54:51 +09:00
lance(-90);
motorMode(GO, GO, SPEED_DEFAULT, SPEED_DEFAULT);
if(sensor&MASK_CHECK_MARKER == 0b10000001){
counterOld = counter;
}else if( counter - counterOld > LAP_COUNT/21 ){
mode = MODE_STOP;
2012-09-14 00:31:41 +09:00
}
2012-09-14 16:54:51 +09:00
break;
case MODE_STOP: //停止
motorL.mode(STOP);
motorR.mode(STOP);
break;
2012-09-14 00:31:41 +09:00
}
2012-09-15 12:21:53 +09:00
/* 平行標的を狙う時は、ランスをぷるぷるする */
2012-09-14 22:47:27 +09:00
static unsigned long millis_lance = 0;
if(millis() - millis_lance > LANCE_INTERVAL){
millis_lance = millis();
switch(servoLance.read()){
2012-09-29 16:16:33 +09:00
case LANCE_DEGREE_DEFAULT - LANCE_ANGLE1:
2012-09-14 22:47:27 +09:00
lance(LANCE_ANGLE2);
break;
2012-09-29 16:16:33 +09:00
case LANCE_DEGREE_DEFAULT - LANCE_ANGLE2:
2012-09-14 22:47:27 +09:00
lance(LANCE_ANGLE1);
break;
}
}
2012-09-14 00:31:41 +09:00
}