KG_KAIROS/MCU (Arduino & STM32)

🦾[KG_KAIROS] RobotArm 제작!!

projectlim 2024. 7. 19. 17:51
728x90
반응형
SMALL

안녕하세요!!

오늘은 서보모터를 이용해서 로봇 팔을 만들어 봅시다!!!

코드는 아래와 같구요!!

#include <Servo.h>
Servo base_servo;
Servo sh_servo;
int servoPina = 11;
int servoPinb = 10;
void setup() {
  Serial.begin(9600);
  base_servo.attach(servoPina);
  sh_servo.attach(servoPinb);
  base_servo.write(90);
  sh_servo.write(90);
}
void loop() {
  if (Serial.available() > 0) {
    String angles = Serial.readStringUntil('\n');
    Serial.println(angles);
    int a_index = angles.indexOf('a');
    int b_index = angles.indexOf('b');
    int c_index = angles.indexOf('c');
    int base_angle = angles.substring(a_index+1, b_index).toInt();
    int sh_angle = angles.substring(b_index+1, c_index).toInt();
    Serial.println(base_angle);
    Serial.println(sh_angle);
    base_servo.write(base_angle);
    sh_servo.write(sh_angle);
    servoParallelController(base_angle,500);

    bool done = false;
while(!done){
  bool done = servoParallelController(base_angle, 500);

}
  }
}

bool servoParallelController(int aim_angle, int speed){
//부드럽게 움직이기
  base_servo.write(base_angle);
  int current_angle = base_servo.read();//현재 앵글을 읽어낼수 있음.
  if(aim_angle > current_angle){
    //올림
    current_angle +=1;
    base_servo.write(current_angle);
    delay(speed);
    return false;
  else if(aim_angle< current_angle){
    //내림
    current_angle -= 1;
    base_servo.write(current_angle)
    return false ;
  else {
    //같음
    return true;

  }
  }
  }

}

코드가 매끄럽진 않지만 원하시는 작업 진행하시면 됩니다,

 

제작 사이트는 https://wokwi.com/

 

Wokwi - Online ESP32, STM32, Arduino Simulator

Arduino (Uno, Mega, Nano)

wokwi.com

입니다. 시뮬레이션이 가능합니다!!

 

자, 그러면 이제 부품을 통해서 제작해 봅니다.

부품들 살짝쿵 찰칵

그리고 제작하면서 서보모터를 90도로 해서 중심 잡는 코드를 적용해서 영점을 잡아줍니다.

직접 만든 게 아니라 이미 출력되어 있는 걸 조립했네요...

아쉽지만 교육에선 시간 관계상 바로 제어 교육을..

 

base 90 나머지 60으로 설정하면 요런 모습이 나옵니다.

/*MIT License

Copyright (c) 2024 JD edu. http://jdedu.kr author: conner.jeong@gmail.com
     
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
     
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
     
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN TH
SOFTWARE.*/
#include<Servo.h>

Servo base;
Servo shoulder;
Servo upperarm;
Servo forearm;
Servo gripper;

int baseAngle = 90;
int shoulderAngle = 90;
int upperarmAngle = 90;
int forearmAngle = 90;
int gripperAngle = 90;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  base.attach(3);
  base.write(baseAngle);
  shoulder.attach(5);
  shoulder.write(shoulderAngle);
  forearm.attach(6);
  forearm.write(forearmAngle);
  upperarm.attach(9);
  upperarm.write(upperarmAngle);

}

void loop() {
  base.write(90);
  shoulder.write(90);
  forearm.write(90);
  upperarm.write(90);
  delay(2000);
}

 

그리고 위 사진처럼 되려면 loop문 base.write에 60으로 바꿔주시면 바뀝니다.

 

사실 작성하면서 느낀 건데...

내가 이걸 잘 쓰고 있는 건가 느낍니다.

그런데 꾸준히 기록하는 습관을 들이고

다시 돌이켜보면서 차근차근 수정하는 식으로 하려고 합니다.

우리가 작성하는 로봇 코딩처럼요 ㅎㅎ

처음부터 잘되는 것도 좋지만

원하는 방향을 맞추고 그에 대응하게 큰 틀을 만들고

추가적으로 필요한 부분들을 함수화 하는 것처럼요!!

728x90
반응형
LIST