发个自制机器人——邻居家的坏小子
  • 浏览:305 评论:6 人

  • 昨天终于把做了老久的机器人搞得差不多了 ——邻居家的坏小子
    介绍下 他是一款基于arduino的智能机器人,拥有一个履带底盘和一个6自由度机械臂。可以远程控制完成各种任务,包括写字画画。下一步将会为他加上语音控制系统,触摸屏和摄像头。
    嘿嘿
    晒点代码(代码不完整):
    #include <Servo.h>
    Servo servo_1, servo_2, servo_3, servo_4, servo_5, servo_6, servo_7;
    int val_1 = 0;
    int val = 0;
    int P = 50;
    int M = 0;
    int N = 50;
    int B = 0;
    int T = 75;
    int Q = 20;
    int LED = 30;
    #define motor1pin 4
    #define motor1pwm 5
    #define motor2pin 7
    #define motor2pwm 6
    char buffer[10];
    void setup() {
    Serial.begin(9600);
    Serial.flush();
    Serial.println("STARTING......");
    servo_7.attach(3);
    servo_1.attach(8);
    servo_2.attach(9);
    servo_3.attach(10);
    servo_4.attach(11);
    servo_5.attach(12);
    servo_6.attach(13);
    pinMode(LED,OUTPUT);
    }
    void loop() {
    if (Serial.available() > 0) {
    int index = 0;
    delay(100);
    int numchar = Serial.available();
    if (numchar > 10) {
    numchar = 10;
    }
    while (numchar--) {
    buffer[index++] = Serial.read();
    }
    splitString(buffer);
    }
    }
    void splitString(char*data) {
    Serial.print("Data entered: ");
    Serial.println(data);
    char* parameter;
    parameter = strtok(data, ",");
    while (parameter != NULL) {
    Smotor(parameter);
    parameter = strtok(NULL, ",");
    }
    for (int x = 0; x < 9; x++) {
    buffer[x] = '\0';
    }
    Serial.flush();
    }
    void Smotor(char*data) {
    if ((data[0] == 'A') || (data[0] == '1')) {
    motor(motor1pin, motor1pwm, 0, 180);
    motor(motor2pin, motor2pwm, 1, 180);
    }
    if ((data[0] == 'D') || (data[0] == '1')) {
    motor(motor1pin, motor1pwm, 1, 180);
    motor(motor2pin, motor2pwm, 0, 180);
    }
    if ((data[0] == 'W') || (data[0] == '1')) {
    motor(motor1pin, motor1pwm, 1, 180);
    motor(motor2pin, motor2pwm, 1, 180);
    }
    if ((data[0] == 'S') || (data[0] == '1')) {
    motor(motor1pin, motor1pwm, 2, 180);
    motor(motor2pin, motor2pwm, 2, 180);
    }
    if ((data[0] == ' ') || (data[0] == '1')) {
    motor(motor1pin, motor1pwm, 0, 180);
    motor(motor2pin, motor2pwm, 0, 180);
    }
    if ((data[0] == 'Q') || (data[0] == '1')) {
    motor(motor1pin, motor1pwm, 2, 180);
    motor(motor2pin, motor2pwm, 1, 180)
    }
    if ((data[0] == 'E') || (data[0] == '1')) {
    motor(motor1pin, motor1pwm, 1, 180);
    motor(motor2pin, motor2pwm, 2, 180);
    }
    if ((data[0] == 'R') || (data[0] == '1')) {
    M = M + 5;
    if (M >= 0 & M <= 180) {
    servo_1.write(M);
    } else {
    M = 180;
    }
    }
    if ((data[0] == 'F') || (data[0] == '1')) {
    M = M - 5;
    if (M >= 0 & M <= 180) {
    servo_1.write(M);
    } else {
    M = 0;
    }
    }

    if ((data[0] == 'T') || (data[0] == '1')) {
    P = P + 5;
    if (P >= 50 & P <= 160) {
    servo_2.write(P);
    } else {
    P = 160;
    }
    }
    if ((data[0] == 'G') || (data[0] == '1')) {
    P = P - 5;
    if (P >= 50 & P <= 180) {
    servo_2.write(P);
    } else {
    P = 50;
    }
    }
    if ((data[0] == 'H') || (data[0] == '1')) {
    N = N + 5;
    if (N >= 0 & N <= 160) {
    servo_3.write(N);
    } else {
    N = 160;
    }
    }
    if ((data[0] == 'Y') || (data[0] == '1')) {
    N = N - 5;
    if (N >= 0 & N <= 150) {
    servo_3.write(N);
    } else {
    N = 0;
    }
    }
    if ((data[0] == 'U') || (data[0] == '1')) {
    B = B + 5;
    if (B >= 0 & B <= 150) {
    servo_4.write(B);
    } else {
    P = 150;
    }
    }
    if ((data[0] == 'J') || (data[0] == '1')) {
    B = B - 5;
    if (B >= 0 & B <= 160) {
    servo_4.write(B);
    } else {
    B = 0;
    }
    }
    if ((data[0] == 'K') || (data[0] == '1')) {
    T = T + 5;
    if (T >= 75 & T <= 180) {
    servo_5.write(T);
    } else {
    T = 180;
    }
    }
    if ((data[0] == 'I') || (data[0] == '1')) {
    T = T - 5;
    if (T >= 75 & T <= 180) {
    servo_5.write(T);
    } else {
    T = 75;
    }
    }
    if ((data[0] == 'O') || (data[0] == '1')) {
    Q = Q + 5;
    if (Q >= 0 & Q <= 180) {
    servo_6.write(Q);
    } else {
    Q = 180;
    }
    }
    if ((data[0] == 'L') || (data[0] == '1')) {
    Q = Q - 5;
    if (Q >= 0 & Q <= 180) {
    servo_6.write(Q);
    } else {
    Q = 20;
    }
    }
    if ((data[0] == 'M') || (data[0] == '1')) {
    servo_7.write(90);
    }
    if ((data[0] == 'N') || (data[0] == '1')) {
    servo_7.write(180);
    }
    if ((data[0] == 'Z') || (data[0] == '1')) {
    digitalWrite(LED,HIGH);
    }
    if ((data[0] == 'C') || (data[0] == '1')) {
    digitalWrite(LED,LOW);
    }