Interface and Application Programming

For this week the assignment is to make an application that interfaces with an input and/or output device. As my final project involves working with motors, I made an application that sends an appropriate signal to four servo motors and one ESC for my brushless DC motor. As I could already control the servo positions and BLDC pwm via the serial monitor on the Arduino IDE, all I needed to do additionally was to open up communication through a different channel. Since I am more experienced with python than JavaScript, I opted for using the PySerial library. The full code for the site is linked in this link. The important file however is this one, which contains the python code used for building out a simple server on Flask and connecting to the desired port. To run the server simply type "flask run" in the terminal inside the directory containing the "app.py" file. After opening up the server, the simple site looks like this:

server

The way this works is by sending a signal in a predetermined manner to either of the four servo motors (corresponding to pressing either "left/right" or "up/down" for each individual servo), or to the BLDC. On the backened (in the "app.py" file), pressing a button for the servos or entering a pwm value for the ESC writes a signal to the ATTiny 1624 microcontroller which has been pre-programmed with this code in Arduino. The relevant part of the code is shown below. The idea is that each command will contain as its first bit a "ctrl" bit, which will be "0" for programming the ESC, and "1"-"4" for the respective four servo motors. If the control bit is a zero, then the following digits just send over the desired pwm. If the control is between "1" and "4" (inclusive) then the second digit is the direction bit, whereby "1" denotes a movement to the right and a "0" denotes movement to the left. I am assuming (based on some preliminary though not fully robust experimentation) that the neutral state of each servo is at 1500 microseconds, and that full motion to the left and right correspond to 1000 and 2000 microseconds, respectively.

      if (ctrl_bit == '0') { // pwm signal for ESC BLDC
        digitalWrite(LED, ledState);
        int pwm = msg.substring(1).toInt();
        servoESC.write(pwm); // send the signal to the ESC
  
      } else if (ctrl_bit == '1' || ctrl_bit == '2' || ctrl_bit == '3' || ctrl_bit == '4') { // servo control 
  
        // why do this the smart way??
        int servo_index;
        if (ctrl_bit == '1')
          servo_index = 1;
        if (ctrl_bit == '2')
          servo_index = 2;
        if (ctrl_bit == '3')
          servo_index = 3;
        if (ctrl_bit == '4')
          servo_index = 4;
  
        char direction_bit = msg[1];
        int microseconds;
        if (direction_bit == '0') { // go left
          microseconds = 1000;
          ledState = HIGH;
        } else if (direction_bit == '1') { // go right
          microseconds = 2000;
          ledState = HIGH;
        } else { // incorrect 
          Serial.println("Direction bit for servo control must be either 0 or 1");
          microseconds = 1500;
        }
        digitalWrite(LED, ledState);
        servo_arr[servo_index].writeMicroseconds(microseconds);      
    

The final project video demo shows this interface working in full force, being able to accurately control all four servo motors as well as the BLDC motor.

Link to index page