티스토리 뷰

OS/micro:bit

micro:bit - Servo(SG-90) 모터 제어

파란크리스마스 2018. 1. 28. 19:10
728x90

본 체험 제품은 element14 의 후원을 받아 아이씨뱅큐㈜ 에서 진행하는 무상 체험단 활동으로 작성한 것입니다.

출처 : lampbotics: mbed Servo Control - 0 to 180º fix - Thomas Murphy
BLENano_SimpleControls - SimpleControls works with the BLEController iOS/Android App. | Mbed
Servo - A class to control a model R/C servo, using a PwmOut | Mbed
Input/Output Pins — BBC micro:bit MicroPython 0.0.1 documentation
Visual Bastic.Net Serial 통신 - Create a basic BBC Micro:Bit Air Mouse – Hardware Hacks
micro:bit IoT In C - Pulse Width Modulation, Servos And More

micro:bit - Servo(SG-90) 모터 제어

micro:bit를 이용한 서보모터중에 가장 저렴한 SG-90을 제어하는 예제를 구현해보았습니다.

Servo.h

#ifndef _SERVO_H
#define _SERVO_H

#include "mbed.h"

class Servo
{
public:
    Servo(PinName pin);
    ~Servo(void);
    
    void write(unsigned char degree);

private:
    void convert(unsigned char degree);
    
    PwmOut _servo;
    unsigned int pulse;
};

#endif

Servo.cpp

#include "Servo.h"

Servo::Servo(PinName pin) : _servo(pin)
{
    _servo.period_ms(20);
}

Servo::~Servo(void)
{
    
}

void Servo::write(unsigned char degree)
{
    convert(degree);
    _servo.pulsewidth_us(pulse);
}

void Servo::convert(unsigned char degree)
{
    // 0~180 degree correspond to 500~2500
    pulse = degree * 11 + 500;
}

main.cpp

#include "MicroBit.h"
#include "Servo.h"

/*
PwmOut servo(P0_1);
int main() {
    servo.period(0.020);          
    while (1) {
        for(float offset=0.0003; offset<0.003; offset+=0.0001) {
            servo.pulsewidth(offset); 
            wait(0.25);
        }
    }
}
*/

#define SERVO_PIN P0_1

Serial pc(USBTX, USBRX);
Servo myservo(SERVO_PIN);

int main() 
{  
    int degree = 0;
    int direction = 1;
    
    while(1)
    {
        
        pc.printf("degree=%d \n\r",degree);
        myservo.write(degree);
            
        if(direction==1)
        {
            degree = degree + 5;
        }
            
        if (direction==2)
        {
            degree = degree - 5; 
        }
            
        if(degree>180)
        {
            degree = 180;
            direction = 2;
        }
        if(degree<0)
        {
            degree = 0;
            direction = 1;
        }
            
        wait(0.05);
    }
}

실행

setServoValue 함수를 사용하는 방법

출처 : micro:bit IoT In C - Pulse Width Modulation, Servos And More

#include "MicroBit.h"
MicroBit uBit;

int main() {
    uBit.init();
    for (;;) {
        uBit.io.P0.setServoValue(0);
        uBit.sleep(1000);
        uBit.io.P0.setServoValue(90);
        uBit.sleep(1000);
        uBit.io.P0.setServoValue(180);
        uBit.sleep(1000);
    }
    release_fiber();
    return 0;
}

아나로그 output을 이용하는 방법

출처 : micro:bit IoT In C - Pulse Width Modulation, Servos And More

#include "MicroBit.h"
MicroBit uBit;

int main() {
    uBit.init();
    uBit.io.P0.setAnalogValue(0);
    uBit.io.P0.setAnalogPeriod(20);

   for (;;) {
    uBit.io.P0.setAnalogValue(52 * 0 / 180 + 920);
    uBit.sleep(1000);
    uBit.io.P0.setAnalogValue(52 * 90 / 180 + 920);
    uBit.sleep(1000);
    uBit.io.P0.setAnalogValue(52 * 180 / 180 + 920);
    uBit.sleep(1000);
  }
  release_fiber();
  return 0;
}


마이크로비트 공식 구입처 : 아이씨뱅큐 http://www.icbanq.com/
엘리먼트14의 특장점 - 글로벌 전자부품 유통회사, 6만원 이상 무료배송, 60만가지 재고 보유, MOQ 없음, 한글 웹사이트, 국내서비스센터 운영 (http://kr.element14.com/?CMP=DSP-ODB-KR-JAN2018-BLOG-ICBanQ1-HOMEPAGE)

마이크로비트 공식 카페 : http://cafe.naver.com/bbcmicro
아이씨뱅큐 공식 블로그 : http://blog.naver.com/icbanq

댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함