硬件组成:
控制端:Arduino UNO主控板+蓝牙主机模块+摇杆模块+9V电池组
小车端:Arduino MEGA2560主控板+蓝牙从机模块+四驱小车支架(含四个减速直流电动机)+四路直流电机驱动模块+7.4V锂电池
图片如下:
软件代码:
控制端:
- #include "SoftwareSerial.h"
- #define BLUETOOTH_RX_DPIN 2
- #define BLUETOOTH_TX_DPIN 3
- #define JOYSTICK_KEY_DPIN 4
- #define JOYSTICK_X_APIN A0
- #define JOYSTICK_Y_APIN A1
- #define BLUETOOTH_BAUD_RATE 115200
- #define MANUAL_MODE 0
- #define AUTO_MODE 1
- int current_mode;
- SoftwareSerial bluetoothSerial(BLUETOOTH_RX_DPIN,BLUETOOTH_TX_DPIN);
- int oriX,oriY;
- int minX,maxX,minY,maxY;
- void setup()
- {
- Serial.begin(9600);
- pinMode(JOYSTICK_KEY_DPIN, INPUT);
- pinMode(BLUETOOTH_RX_DPIN, INPUT);
- pinMode(BLUETOOTH_TX_DPIN, OUTPUT);
- bluetoothSerial.begin(BLUETOOTH_BAUD_RATE);
- oriX = 0;
- oriY = 0;
-
- current_mode=MANUAL_MODE;
-
- }
- void loop()
- {
- int X,Y;
- float rx, ry;
- int key_down = digitalRead(JOYSTICK_KEY_DPIN);
- if(key_down == 0)
- {
- delay(50);
- key_down = digitalRead(JOYSTICK_KEY_DPIN);
- if(key_down==0){
- bluetoothSerial.write('m');
- current_mode=!current_mode;
- }
- }
-
- int sensorValue = analogRead(JOYSTICK_X_APIN);
- if(oriX == 0)
- {
- oriX = sensorValue;
- minX=0-oriX;
- maxX=1023-oriX;
- }
-
- X=sensorValue - oriX;
-
-
- sensorValue = analogRead(JOYSTICK_Y_APIN);
- if(oriY == 0)
- {
- oriY = sensorValue;
- minY=0-oriY;
- maxY=1023-oriY;
- }
-
- Y=sensorValue - oriY;
-
- if(X>0) rx=(float)X/(float)maxX;
- else if(X<0) rx=(float)X/(float)minX;
-
- if(Y>0) ry=(float)Y/(float)maxY;
- else if(Y<0) ry=(float)Y/(float)minY;
-
- if(current_mode==MANUAL_MODE){
- if(abs(X)<8 && abs(Y)<8) bluetoothSerial.write('h');
- else if(Y>100 && abs(X)<50) bluetoothSerial.write('w');
- else if(Y<-100 && abs(X)<50 ) bluetoothSerial.write('s');
- else if(X>100 ) bluetoothSerial.write('d');
- else if(X<-100 ) bluetoothSerial.write('a');
- }
- /*
- if( rx <0.2 && ry <0.2 ) bluetoothSerial.write('h');
- else if( ry>0.6 && rx<0.2){
- if(Y<0) bluetoothSerial.write('s');
- else if(Y>0) bluetoothSerial.write('w');
- }else if(rx>0.6 && ry<0.5 ){
- if(X<0 ) bluetoothSerial.write('a');
- else if(X>0 ) bluetoothSerial.write('d');
- }
- */
-
- delay(50);
- }
小车端:
- #include "Arduino.h"
- #include "Servo.h"
- #include "SoftwareSerial.h"
- /*
- Pins for PWM signal
- */
- #define MOTOR1_PWM 4
- #define MOTOR2_PWM 5
- #define MOTOR3_PWM 6
- #define MOTOR4_PWM 7
- /* camera 2-degree servos */
- #define SERVO_HORI 8
- #define SERVO_VERT 9
- /* ultra-sonic sensor */
- #define TRIG_PIN 10 //Trig_pin
- #define ECHO_PIN 11 //Echo_pin
- #define MAX_BUF_LEN 256
- #define WHEEL_MAX_SPEED 240
- #define WHEEL_TURN_SPEED 160
- #define SERVO_HORI_DEFAULT_DEGREE 80
- #define SERVO_VERT_DEFAULT_DEGREE 0
- #define SERVO_INCREMENT 10
- /*
- Pins for logic input
- */
- #define MOTOR1_IN1 22
- #define MOTOR1_IN2 23
- #define MOTOR2_IN1 24
- #define MOTOR2_IN2 25
- #define MOTOR3_IN1 26
- #define MOTOR3_IN2 27
- #define MOTOR4_IN1 28
- #define MOTOR4_IN2 29
- #define SERIAL_BAUD 115200
- #define STATUS_STOP 0
- #define STATUS_FORWARD 1
- #define STATUS_BACKWARD 2
- #define STATUS_TURN_FORWARD_LEFT 3
- #define STATUS_TURN_FORWARD_RIGHT 4
- #define STATUS_TURN_BACKWARD_LEFT 5
- #define STATUS_TURN_BACKWARD_RIGHT 6
- #define MANUAL_MODE 0
- #define AUTO_MODE 1
- int current_mode;
- /*
- speed (PWM relative) values for 4 wheel-motors
- */
- int wheel_forward_speed[4];
- int wheel_backward_speed[4];
- int wheel_turn_left_speed[4];
- int wheel_turn_right_speed[4];
- /* current car status */
- int current_status;
- Servo servo_hori, servo_vert;
- void wheel_id_to_pwm_pin(int wheel_id, int* pin)
- {
-
- switch(wheel_id){
- case 1:
- *pin=MOTOR1_PWM;
- break;
- case 2:
- *pin=MOTOR2_PWM;
- break;
- case 3:
- *pin=MOTOR3_PWM;
- break;
- case 4:
- *pin=MOTOR4_PWM;
- break;
- default:
- Serial.println("illegal wheel_id input\n");
- }
- }
- void wheel_id_to_logic_pin(int wheel_id, int* in1, int* in2)
- {
- switch(wheel_id){
- case 1:
- *in1=MOTOR1_IN1;
- *in2=MOTOR1_IN2;
- break;
- case 2:
- *in1=MOTOR2_IN1;
- *in2=MOTOR2_IN2;
- break;
- case 3:
- *in1=MOTOR3_IN1;
- *in2=MOTOR3_IN2;
- break;
- case 4:
- *in1=MOTOR4_IN1;
- *in2=MOTOR4_IN2;
- break;
- default:
- Serial.println("illegal wheel_id input\n");
- }
- }
- void wheel_driver(int wheel_id, int wheel_speed)
- {
- int pwm_pin;
- int in1,in2;
- int wheel_dir=0;
-
- if(wheel_id<1 || wheel_id>4) return; // do nothing
-
-
- wheel_id_to_pwm_pin(wheel_id, &pwm_pin);
- wheel_id_to_logic_pin(wheel_id, &in1, &in2);
-
- // set speed parameter
- if(wheel_speed<0){
- wheel_dir=-1;
- wheel_speed=-wheel_speed;
- }else{
- wheel_dir=1;
- }
-
- if(wheel_speed>255) wheel_speed=255;
-
- analogWrite(pwm_pin, wheel_speed);
-
- // stop the motor if speed is 0
- if(wheel_speed==0){
- digitalWrite(in1, LOW);
- digitalWrite(in2, LOW);
- }
-
-
- if(wheel_dir>0){
- // make wheel scroll forward
- digitalWrite(in1, HIGH);
- digitalWrite(in2, LOW);
- }else if(wheel_dir<0){
- // scroll backward
- digitalWrite(in1, LOW);
- digitalWrite(in2, HIGH);
- }else{
- // stop the wheel
- digitalWrite(in1, LOW);
- digitalWrite(in2, LOW);
- analogWrite(pwm_pin,0);
- }
-
- }
- void car_stop()
- {
- int i;
- if(current_status==STATUS_STOP) return;
- for(i=1;i<=4;i++){
- wheel_driver(i, 0);
- }
- current_status=STATUS_STOP;
- }
- void car_forward()
- {
- int i;
- if(current_status==STATUS_FORWARD) return;
- car_stop();
- for(i=1;i<=4;i++){
- wheel_driver(i, wheel_forward_speed[i-1]);
- }
- current_status=STATUS_FORWARD;
- }
- void car_backward()
- {
- int i;
- if(current_status==STATUS_BACKWARD) return;
- car_stop();
- for(i=1;i<=4;i++){
- wheel_driver(i, wheel_backward_speed[i-1]);
- }
- current_status=STATUS_BACKWARD;
- }
- void car_turn_forward_left()
- {
- int i;
- if(current_status==STATUS_TURN_FORWARD_LEFT) return;
- car_stop();
- for(i=1;i<=4;i++){
- wheel_driver(i, wheel_turn_left_speed[i-1]);
- }
-
- current_status=STATUS_TURN_FORWARD_LEFT;
- }
- void car_turn_forward_right()
- {
- int i;
- if(current_status==STATUS_TURN_FORWARD_RIGHT) return;
- car_stop();
- for(i=1;i<=4;i++){
- wheel_driver(i, wheel_turn_right_speed[i-1]);
- }
- current_status=STATUS_TURN_FORWARD_RIGHT;
- }
- void process_cmd(char* cmd, int cmd_len)
- {
- int i;
- int cmd_step=1;
- int h_degree, v_degree;
-
- if(servo_hori.attached()) h_degree=servo_hori.read();
- if(servo_hori.attached()) v_degree=servo_vert.read();
-
- for(i=0;i<cmd_len;i+=cmd_step){
- switch(cmd[i]){
- case 'w':
- car_forward();
- break;
- case 's':
- car_backward();
- break;
- case 'a':
- car_turn_forward_left();
- break;
- case 'd':
- car_turn_forward_right();
- break;
- case 'm':
- if(current_mode==MANUAL_MODE) current_mode=AUTO_MODE;
- else current_mode=MANUAL_MODE;
- break;
- case 'h':
- car_stop();
- break;
- case 'j':
- if(servo_hori.attached())
- servo_hori.write(h_degree-SERVO_INCREMENT);
- delay(50);
- break;
- case 'l':
- if(servo_hori.attached())
- servo_hori.write(h_degree+SERVO_INCREMENT);
- delay(50);
- break;
- case 'i':
- if(servo_vert.attached())
- servo_vert.write(v_degree-SERVO_INCREMENT);
- delay(50);
- break;
- case 'k':
- if(servo_vert.attached())
- servo_vert.write(v_degree+SERVO_INCREMENT);
- delay(50);
- break;
- default:
- break;
- }
- }
- }
- int Distance(long time)
- {
- // Distance_CM = ((Duration of high level)*(Sonic :340m/s))/2
- // = ((Duration of high level)*(Sonic :0.034 cm/us))/2
- // = ((Duration of high level)/(Sonic :29.4 cm/us))/2
- int dist = time/29/2 ;
-
- return dist;
- }
- long Trig_Echo()
- {
- digitalWrite(TRIG_PIN, LOW);
- delayMicroseconds(2);
- digitalWrite(TRIG_PIN, HIGH); // pull the Trig pin to high level for more than 10us impulse
- delayMicroseconds(10);
- digitalWrite(TRIG_PIN, LOW);
- long microseconds = pulseIn(ECHO_PIN,HIGH); // waits for the pin to go HIGH, and returns the length of the pulse in microseconds
- return microseconds; // return microseconds
- }
- void setup()
- {
-
- /*
- servo_hori.attach(SERVO_HORI);
- servo_vert.attach(SERVO_VERT);
- servo_hori.write(SERVO_HORI_DEFAULT_DEGREE);
- servo_vert.write(SERVO_VERT_DEFAULT_DEGREE);
- */
-
- Serial.begin(SERIAL_BAUD); // init serial
-
- pinMode(MOTOR1_IN1,OUTPUT);
- pinMode(MOTOR1_IN2,OUTPUT);
- pinMode(MOTOR2_IN1,OUTPUT);
- pinMode(MOTOR2_IN2,OUTPUT);
- pinMode(MOTOR3_IN1,OUTPUT);
- pinMode(MOTOR3_IN2,OUTPUT);
- pinMode(MOTOR4_IN1,OUTPUT);
- pinMode(MOTOR4_IN2,OUTPUT);
-
- pinMode(TRIG_PIN,OUTPUT); // set output pin for trigger
- pinMode(ECHO_PIN,INPUT); // set input pin for echo
-
-
- /* setting default wheel speeds */
- int i;
- for(i=0;i<4;i++){
- wheel_forward_speed[i]=WHEEL_MAX_SPEED;
- wheel_backward_speed[i]=-WHEEL_MAX_SPEED;
- }
-
- wheel_turn_left_speed[0]=WHEEL_TURN_SPEED;
- wheel_turn_left_speed[1]=WHEEL_TURN_SPEED;
- wheel_turn_left_speed[2]=-WHEEL_TURN_SPEED;
- wheel_turn_left_speed[3]=-WHEEL_TURN_SPEED;
-
- wheel_turn_right_speed[0]=-WHEEL_TURN_SPEED;
- wheel_turn_right_speed[1]=-WHEEL_TURN_SPEED;
- wheel_turn_right_speed[2]=WHEEL_TURN_SPEED;
- wheel_turn_right_speed[3]=WHEEL_TURN_SPEED;
-
-
- /* make the car stop */
- car_stop();
-
- current_mode=MANUAL_MODE;
- }
- void loop()
- {
- int i,cmd_len;
- char buf[MAX_BUF_LEN]={0};
- char ch;
- int wheel_id, wheel_speed;
- if(Serial.available()){
- i=0;
- do{
- buf[i++] = Serial.read();
- delay(2);
- }while(Serial.available() && i<MAX_BUF_LEN);
- process_cmd(buf, i);
- }
-
- if(current_mode==AUTO_MODE){
- long microseconds = Trig_Echo();
- int dist_cm = Distance(microseconds);
- while(dist_cm<30 && dist_cm>0 ) {
- car_turn_forward_right();
- microseconds = Trig_Echo();
- dist_cm = Distance(microseconds);
- }
- car_forward();
- }
- }
阅读(10003) | 评论(0) | 转发(0) |