질문게시판 > 답변 : 아두이노 및 전기배선 작동이 안되어 해결 부탁드립니다

TODAY5,047 TOTAL7,751,373
사이트 이용안내
Login▼/회원가입
최신글보기 질문게시판 기술자료 동영상강좌

아두이노 센서 ATMEGA128 PWM LED 초음파 AVR 블루투스 LCD UART 모터 적외선


BASIC4MCU | 질문게시판 | 답변 : 아두이노 및 전기배선 작동이 안되어 해결 부탁드립니다

페이지 정보

작성자 master 작성일2024-09-14 10:33 조회388회 댓글0건

본문

	

 

#include <Servo.h>

#include <ros.h>

#include <std_msgs/String.h>

#include <std_msgs/Int32.h>

#include <Adafruit_NeoPixel.h> // Neopixel을 사용하기 위해서 라이브러리를 불러옵니다.

#include <avr/power.h>

//

#define PIN      2

#define NUM_LEDS 23  // LED 개수

Adafruit_NeoPixel strip=Adafruit_NeoPixel(NUM_LEDS,PIN,NEO_GRB+NEO_KHZ800);

ros::NodeHandle nh;

std_msgs::String str_msg;

ros::Publisher thrust("thrust",&str_msg);

Servo thrusterLeft,thrusterRight,thrusterCenter;

const int relayPin2=3,relayPin=4;

const int rcChannelPin1=5,rcChannelPin2=6;

const int receiverPin=7;

const int emergencyPin=8;

const int thrustPinLeft=9,thrustPinRight=10,thrustPinCenter=11;

const int relayPin3=12;

// 쓰러스터 보정 값(필요에 따라 조정)

const int leftThrusterCorrection=0,rightThrusterCorrection=0;

bool      isAutonomousMode=false;

//

void leftThrusterCallback(const std_msgs::Int32& msg){

  if(isAutonomousMode){ thrusterLeft.writeMicroseconds(msg.data); }

}

//

void rightThrusterCallback(const std_msgs::Int32& msg){

  if(isAutonomousMode){ thrusterRight.writeMicroseconds(msg.data); }

}

//

void centerThrusterCallback(const std_msgs::Int32& msg){

  if(isAutonomousMode){ thrusterCenter.writeMicroseconds(msg.data); }

}

//

ros::Subscriber<std_msgs::Int32> sub_left("Thruster_left_topic",&leftThrusterCallback);

ros::Subscriber<std_msgs::Int32> sub_right("Thruster_right_topic",&rightThrusterCallback);

ros::Subscriber<std_msgs::Int32> sub_center("Thruster_center_topic",¢erThrusterCallback);

//

void setup(){

  strip.begin(); strip.show();

  thrusterLeft.attach(thrustPinLeft); thrusterRight.attach(thrustPinRight); thrusterCenter.attach(thrustPinCenter);

  pinMode(rcChannelPin1,INPUT); pinMode(rcChannelPin2,INPUT);

  pinMode(receiverPin,INPUT);   pinMode(emergencyPin,INPUT);

  pinMode(relayPin,OUTPUT);   pinMode(relayPin2,OUTPUT); pinMode(relayPin3,OUTPUT);

  digitalWrite(relayPin,1);   digitalWrite(relayPin2,1); digitalWrite(relayPin3,1);

  nh.initNode(); nh.advertise(thrust); nh.subscribe(sub_left); nh.subscribe(sub_right); nh.subscribe(sub_center);

}

//

void loop(){

  int rcValue1=pulseIn(rcChannelPin1,1); int throttlePWM1=map(rcValue1,1100,1900,1100,1900);

  int rcValue2=pulseIn(rcChannelPin2,1); int throttlePWM2=map(rcValue2,1100,1900,-380, 380);

  int rcValue3=pulseIn(receiverPin,1);   int throttlePWM3=map(rcValue3,1100,1900,1100,1900);

  int rcValue4=pulseIn(emergencyPin,1);  int throttlePWM4=map(rcValue4,1100,1900,1100,1900);

  const char* mode;

  if(throttlePWM4>1600){ // 비상정지모드

    for(int i=0;i<NUM_LEDS;i++)strip.setPixelColor(i,255,0,0); strip.show(); // R=255,G=0,B=0(빨간색)

    digitalWrite(relayPin,0); digitalWrite(relayPin2,0); digitalWrite(relayPin3,0);

    isAutonomousMode=false;

    mode="EMERGENCY_STOP";

  }

  else{

    if(throttlePWM3>1600){ // RC모드

      for(int i=0;i<NUM_LEDS;i++)strip.setPixelColor(i,0,255,0); strip.show(); // R=0,G=255,B=0(초록색)

      digitalWrite(relayPin,1); digitalWrite(relayPin2,1); digitalWrite(relayPin3,1);

      isAutonomousMode=false;

      mode="RC_MODE";

      int combinedThrottleLeft=throttlePWM1 -throttlePWM2+leftThrusterCorrection;

      int combinedThrottleRight=throttlePWM1+throttlePWM2+rightThrusterCorrection;

      int combinedThrottleCenter=throttlePWM1;

      if(throttlePWM2==0){

        combinedThrottleLeft =throttlePWM1+leftThrusterCorrection;

        combinedThrottleRight=throttlePWM1+rightThrusterCorrection;

      }

      if(throttlePWM1>=1401 && throttlePWM1<=1599){

        combinedThrottleLeft=1500; combinedThrottleRight=1500; combinedThrottleCenter=1500;

      }

      combinedThrottleLeft  =constrain(combinedThrottleLeft,1100,1900);

      combinedThrottleRight =constrain(combinedThrottleRight,1100,1900);

      combinedThrottleCenter=constrain(combinedThrottleCenter,1100,1900);

      thrusterLeft.writeMicroseconds(combinedThrottleLeft);

      thrusterRight.writeMicroseconds(combinedThrottleRight);

      thrusterCenter.writeMicroseconds(combinedThrottleCenter);

      char result_str[50];

      snprintf(result_str,100,"PWM1: %d,PWM2: %d,PWM3: %d,MODE: %s",combinedThrottleLeft,combinedThrottleRight,combinedThrottleCenter,mode);

      str_msg.data=result_str;

      thrust.publish(&str_msg);

    }

    else if(throttlePWM3>1400){ // 자율운항모드

      for(int i=0;i<NUM_LEDS;i++)strip.setPixelColor(i,255,255,0); strip.show(); delay(100); // R=255,G=255,B=0(노란색)

      for(int i=0;i<NUM_LEDS;i++)strip.setPixelColor(i,  0,  0,0); strip.show(); delay(100); // 모든 LED 끄기

      digitalWrite(relayPin,1); digitalWrite(relayPin2,1); digitalWrite(relayPin3,1);

      isAutonomousMode=true;

      char result_str[50];

      snprintf(result_str,100,"MODE: AUTO_MODE");

      str_msg.data=result_str;

      thrust.publish(&str_msg);

    }

    else{ // 쓰러스터 제어 // if(throttlePWM3<=1400)

      for(int i=0;i<NUM_LEDS;i++)strip.setPixelColor(i,0,0,0); strip.show(); delay(100); // 모든 LED 끄기

      isAutonomousMode=false;

      digitalWrite(relayPin,1); digitalWrite(relayPin2,0); digitalWrite(relayPin3,1);

    }

  }

  nh.spinOnce();

  delay(20);

}

 

//

if 조건식을 조금 변형했습니다.

(동작은 동일)

//

 

위 코드와 업로드된 회로도로 쓰러스터를 작동하려고 하는데 

비상정지 모드만 작동이 되고 

RC 및 자율운항모드는 작동이 되지 않습니다 

  if(throttlePWM4>1600){ // 비상정지모드

이 코드만 실행 된다면 throttlePWM4가 1600을 넘는지 확인하는 것이 우선되어야겠죠?
시리얼추가해서 시리얼모니터에 출력해보세요

// 

LED또한 들어오지 않아서 답변 부탁드리겠습니다

LED가 켜지지 않는다는 것이 Neopixel을 의미하나요?

Neopixel 예제를 돌려서 체크하시면 됩니다.

 




  • BASIC4MCU 작성글 SNS에 공유하기
  • 페이스북으로 보내기
  • 트위터로 보내기
  • 구글플러스로 보내기

댓글 0

조회수 388

등록된 댓글이 없습니다.

질문게시판HOME > 질문게시판 목록

MCU, AVR, 아두이노 등 전자공학에 관련된 질문을 무료회원가입 후 작성해주시면 전문가가 답변해드립니다.
ATMEGA128PWMLED초음파
아두이노AVR블루투스LCD
UART모터적외선ATMEGA
전체 스위치 센서
질문게시판 목록
제목 작성자 작성일 조회
공지 MCU, AVR, 아두이노 등 전자공학에 관련된 질문은 질문게시판에서만 작성 가능합니다. 스태프 19-01-15 24377
공지 사이트 이용 안내댓글[31] master 17-10-29 39063
질문 DLM-64DCAA 고장나서 수리하고 싶습니다댓글[2] 이미지첨부파일 공부하고싶은콩 25-03-11 51
질문 로봇 얼굴 제작에 대하여 질문드립니다.댓글[4] 카모킹스 25-03-03 290
질문 모터와 모터드라이브 질문있습니다댓글[1] ponyo 25-02-19 143
질문 공통음극형 FND를 9V로 동작하게 하고 싶어요.댓글[7] 멘토 25-01-21 1601
질문 oled 모듈 연결 질문입니다!댓글[1] 펌린이 24-12-17 300
질문 Atmega 128 타이머 질문있습니다댓글[1] ednda 24-12-11 323
답변 답변글 답변 : Atmega 128 타이머 질문있습니다 master 24-12-11 235
질문 ATmega128 2개로 hc-05,hc-06블루트수 통신 동콩이 24-12-04 295
답변 답변글 답변 : ATmega128 2개로 hc-05,hc-06블루트수 통신댓글[4] master 24-12-04 2277
질문 lm75a 온도센서 관련 질문입니다.댓글[6] 이미지첨부파일 hanmw0707 24-12-01 5211
질문 리얼타임클락 질문이요 ㅠㅠ댓글[1] 이미지 팬케이크 24-11-22 395
질문 가속도센서 2개를 강아지 2마리에 각각 달아서 스마트폰으로 움직임을 보고 싶은데요댓글[1] gainomax 24-11-21 567
질문 라즈베리파이 4B 사용 중 막히는 부분 질문합니다댓글[1] 한마바키 24-11-12 6688
질문 스위치로 PI 모터 제어 바밤 24-11-11 288
답변 답변글 답변 : 스위치로 PI 모터 제어 master 24-11-12 285
질문 아트메가128 마이크로프로세서댓글[3] 옹심이 24-11-05 1108
질문 초음파 센서로 장애물 인식댓글[1] 나난ㄴ 24-10-31 450
질문 이런 투명 LCD는 뭐라고 부르나요?댓글[2] 이미지 펌린이 24-10-29 657
질문 모터제어 중 RPM 계산 질문입니다.댓글[3] suuuuuuuh 24-10-24 961
질문 아트메가 128과 블루투스 모듈(HC-06) 연결 질문댓글[1] 메가아트 24-10-24 451
질문 ATmega128 질문 DFplayer mini댓글[1] Miin 24-10-18 7039
질문 스탭모터 3개 회로도 질문.. 삉삉이 24-10-11 429
답변 답변글 답변 : 스탭모터 3개 회로도 질문.. 이미지 master 24-10-12 443
질문 수분수위센서와 멀티플렉서댓글[3] 김고래쓰 24-10-10 1204
질문 ATmegq128 PI 제어기 추가댓글[1] 바밤 24-10-08 459
답변 답변글 답변 : ATmegq128 PI 제어기 추가댓글[1] master 24-10-08 618
질문 아두이노 ide 네오픽셀 각 셀 각자 코드 현수 24-09-29 299
질문 아두이노 IDE 프로마이크로 네오픽셀 현수 24-09-29 341
게시물 검색

2022년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2021년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2020년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2019년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2018년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
Privacy Policy
MCU BASIC ⓒ 2020
모바일버전으로보기