Arduino& CC3000& Motorshield&机器人

时间:2015-07-09 20:37:05

标签: rest arduino

我有一辆4马达驱动车,上面装有ARD MEGA / CC3000 / Motorshield。 我写了一个运行REST的草图和一个带按钮的网页,它在Serial中运行良好。 我写了一个控制马达前进/后退/左/右的草图,这很好用。 当我合并2个草图并在网页上按下FORWARD它可以工作,然后'挂起'。

    void loop()
    {
      // Handle any multicast DNS requests
      mdns.update();
      Adafruit_CC3000_ClientRef client = restServer.available();
      rest.handle(client);
      wdt_reset();
      if(!cc3000.checkConnected()){while(1){}}
      wdt_reset(); 
    }

    int forward(String command) {
      goforwards();
      return 1;
    }
    int backward(String command) {

      gobackwards();
      return 1;
    }
    int left(String command) {
      Serial.println(F("GO LEFT..."));
      //goleft();
      return 1;
    }
    int right(String command) {
      Serial.println(F("GO RIGHT..."));
      //goright();
      return 1;
    }
    int estop(String command) {
      Serial.println(F("Asked to STOP..."));
       allstop();
       return 1;
    }

    void goforwards() {
     Serial.println(F("GO FORWARD..."));
      motor(1, FORWARD, 150); motor(4, FORWARD, 150);  motor(2, FORWARD, 150); motor(3, FORWARD, 150);  delay(500); 
      allstop();
    }
    void gobackwards() {
     Serial.println(F("GO BACKWARD..."));
     motor(1, BACKWARD, 150); motor(4, BACKWARD, 150);  motor(2, BACKWARD, 150); motor(3, BACKWARD, 150);  delay(500); 
     allstop();
    }
    void goleft() {motor(1, FORWARD, 175); motor(4, FORWARD, 175); motor(2, FORWARD, 50); motor(3, FORWARD, 50);delay(2000); }
    void goright() {motor(1, FORWARD, 50); motor(4, FORWARD, 50); motor(2, FORWARD, 175); motor(3, FORWARD, 175); delay(2000); }
    void allstop() {motor(1, RELEASE, 0); motor(2, RELEASE, 0);motor(3, RELEASE, 0);motor(4, RELEASE, 0);delay(1); }

    void motor(int nMotor, int command, int speed)
    {
      int motorA, motorB;

      if (nMotor >= 1 && nMotor <= 4)
      {  
        switch (nMotor)
        {
        case 1:
          motorA   = MOTOR1_A;
          motorB   = MOTOR1_B;
          break;
        case 2:
          motorA   = MOTOR2_A;
          motorB   = MOTOR2_B;
          break;
        case 3:
          motorA   = MOTOR3_A;
          motorB   = MOTOR3_B;
          break;
        case 4:
          motorA   = MOTOR4_A;
          motorB   = MOTOR4_B;
          break;
        default:
          break;
        }

        switch (command)
        {
        case FORWARD:
          motor_output (motorA, HIGH, speed);
          motor_output (motorB, LOW, -1);     // -1: no PWM set
          break;
        case BACKWARD:
          motor_output (motorA, LOW, speed);
          motor_output (motorB, HIGH, -1);    // -1: no PWM set
          break;
        case BRAKE:
          motor_output (motorA, LOW, 255); // 255: fully on.
          motor_output (motorB, LOW, -1);  // -1: no PWM set
          break;
        case RELEASE:
          motor_output (motorA, LOW, 0);  // 0: output floating.
          motor_output (motorB, LOW, -1); // -1: no PWM set
          break;
        default:
          break;
        }
      }
    }

    void motor_output (int output, int high_low, int speed)
    {
      int motorPWM;

      switch (output)
      {
      case MOTOR1_A:
      case MOTOR1_B:
        motorPWM = MOTOR1_PWM;
        break;
      case MOTOR2_A:
      case MOTOR2_B:
        motorPWM = MOTOR2_PWM;
        break;
      case MOTOR3_A:
      case MOTOR3_B:
        motorPWM = MOTOR3_PWM;
        break;
      case MOTOR4_A:
      case MOTOR4_B:
        motorPWM = MOTOR4_PWM;
        break;
      default:
        // Use speed as error flag, -3333 = invalid output.
        speed = -3333;
        break;
      }

      if (speed != -3333)
      {
        shiftWrite(output, high_low);

       if (speed >= 0 && speed <= 255)    
        {
          analogWrite(motorPWM, speed);
        }
      }
    }

    void shiftWrite(int output, int high_low)
    {
      static int latch_copy;
      static int shift_register_initialized = false;


      if (!shift_register_initialized)
      {
        pinMode(MOTORLATCH, OUTPUT);
        pinMode(MOTORENABLE, OUTPUT);
        pinMode(MOTORDATA, OUTPUT);
        pinMode(MOTORCLK, OUTPUT);

        // Set pins for shift register to default value (low);
        digitalWrite(MOTORDATA, LOW);
        digitalWrite(MOTORLATCH, LOW);
        digitalWrite(MOTORCLK, LOW);
        // Enable the shift register, set Enable pin Low.
        digitalWrite(MOTORENABLE, LOW);

        // start with all outputs (of the shift register) low
        latch_copy = 0;

        shift_register_initialized = true;
      }

      // The defines HIGH and LOW are 1 and 0.
      // So this is valid.
      bitWrite(latch_copy, output, high_low);

      // Use the default Arduino 'shiftOut()' function to
      // shift the bits with the MOTORCLK as clock pulse.
      // The 74HC595 shiftregister wants the MSB first.
      // After that, generate a latch pulse with MOTORLATCH.
      shiftOut(MOTORDATA, MOTORCLK, MSBFIRST, latch_copy);
      delayMicroseconds(5);    // For safety, not really needed.
      digitalWrite(MOTORLATCH, HIGH);
      delayMicroseconds(5);    // For safety, not really needed.
      digitalWrite(MOTORLATCH, LOW);
    }

上面的代码是Meat出错的地方。上面的代码都很好。 当代码获得命令'forward'时,它运行VOID goforwards();但然后'挂起'。同样的'倒退'。 似乎陷入了这些VOIDS,因此不响应来自网页的下一个命令。 任何帮助表示赞赏!

0 个答案:

没有答案