質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

Q&A

1回答

3738閲覧

Arduinoを利用し、2つのプログラムを同時に動かす方法について

mt_1012121

総合スコア0

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

0グッド

0クリップ

投稿2021/12/23 11:15

編集2021/12/23 17:43

前提・実現したいこと

アルディーノを使って、LEDテープを光らせるのとレヴィーウォークで動くロボットのプログラムを同時に行ういたい。

発生している問題・エラーメッセージ

LEDテープは光るがロボット(モーター)が動かない。

該当のソースコード

C言語

1#include <avr/pgmspace.h> 2#include <SoftwareSerial.h> 3#include <string.h> 4#include <avr/wdt.h> 5#include <Adafruit_NeoPixel.h> 6#ifdef __AVR__ 7#include <avr/power.h> 8#include <TimedAction.h> 9#endif 10#define LED_PIN 10 11#define LED_COUNT 13 12 13Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); 14 15 16int LM1 = 7 ,LM2 = 8 ,LPWM = 6; 17int RM1 = 2 ,RM2 = 4 ,RPWM = 5; 18int RSSI_Read = 5; 19int TS_F = 12 ,TS_B = 13; 20int TOKEN =0; 21int packet[100]; 22int address[100]; 23int Selection_val; 24#define Motor_Out 141 // 9Vbattery 25#define Motor_Out2 90 // 9Vbattery 26double val_L1 = 0;//左前方センサ値:決定値 27double val_R1 = 0;//右前方センサ値:決定値 28double val_L2 = 0;//左後方センサ値:決定値 29double val_R2 = 0;//右後方センサ値:決定値 30double ave_L1 = 0;//左前方センサ値:実測値 31double ave_R1 = 0;//右前方センサ値:実測値 32double ave_L2 = 0;//左後方センサ値:実測値 33double ave_R2 = 0;//右後方センサ値:実測値 34double before=0; //行動時間決定用変数 35double before2=0; //発見情報送信用変数 36double before3=0;//回避角度決定用変数 37int time = 500; 38boolean one_time = false; 39int stackL=0;//スタックカウント 40int stackR=0;//同上 41int advancea=0;//RW 時移動状態決定用:0=回転,1=直進 42double rnd_d =0;//RW 時回転方向決定用乱数 43double rnd_c=0; 44double rnd_c2=0; 45double rnd_f=0;//RW 時移動距離決定用乱数 46double add_powerR = 0;//出力微調整用変数 47double add_powerL = 0;//同上 48double F_time=0; 49double V_wait=0; 50double V_wait2=0; 51boolean direc = false; 52boolean A_stackL = false;//スタック認識フラグ 53boolean A_stackR = false;//同上 54boolean L1_blo = false;//障害物認識フラグ 55boolean R1_blo = false;//同上 56boolean L2_blo = false;//同上 57boolean R2_blo = false;//同上 58boolean rnd_rst=false;//RW 時乱数入れ替え用フラグ 59boolean rnd_rst2=false;//OA 時乱数入れ替え用フラグ 60void Selection(void){ 61 Selection_val = random(0,2); 62} 63 64void stepper_control(); 65void led_control(); 66 67TimedAction motorAction = TimedAction(0,stepper_control); 68TimedAction ledAction = TimedAction(1000,led_control); 69 70void setup(){ 71 72#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000) 73 clock_prescale_set(clock_div_1); 74#endif 75 // END of Trinket-specific code. 76 77 strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) 78 strip.show(); // Turn OFF all pixels ASAP 79 strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255) 80 81 82 83// loop() function -- runs repeatedly as long as board is on --------------- 84 85Serial.begin(115200); 86pinMode(LM1,OUTPUT); 87pinMode(LM2,OUTPUT); 88pinMode(RM1,OUTPUT); 89pinMode(RM2,OUTPUT); 90pinMode(LPWM,OUTPUT); 91pinMode(RPWM,OUTPUT); 92pinMode(TS_F,INPUT); 93pinMode(TS_B,INPUT); 94pinMode(RSSI_Read,INPUT); 95randomSeed(analogRead(4)); 96wdt_enable(WDTO_1S); 97} 98 99void loop(){ 100 motorAction.check(); 101 ledAction.check(); 102} 103 104void stepper_control(){ 105 Receive(); 106 if(packet[17]==0xAA){ 107 add_powerR=packet[15] - Motor_Out; 108 add_powerL=packet[16] - Motor_Out; 109} 110 111 112 if(packet[15]==0x47){//GO 信号受け取り 113 while(1){ 114 time=500; 115 Obstacle_Sensor(); 116 wdt_reset();//これが一定時間(1s)実行されなければリセットされる 117 if (Target_Discovery() == 0){//ボールを見つけたら 1,それ以外は 0 118 Obstacle_avoidance(); 119 } 120 else{ 121 Receive();//受信関数 122 if(packet[15]==0x53){//STOP 信号受け取り 123 delay(1); 124 TOKEN=0; 125 break; 126 } 127 Motor_Drive('S');//ボールを発見したら停車 128 } 129 Receive();//受信関数 130 if(packet[15]==0x53){//STOP 信号受け取り 131 delay(1); 132 break; 133 } 134 } 135 } 136 else{ 137 wdt_reset();//これが一定時間(1s)実行されなければリセットされる 138 Motor_Drive('S'); 139 delay(1); 140 return; 141} 142} 143void led_control(){ 144 colorWipe(strip.Color(255, 0, 0), 25); // Red 145 colorWipe(strip.Color( 0, 255, 0), 25); // Green 146 colorWipe(strip.Color( 0, 0, 255), 25); // Blue 147} 148 149 150void colorWipe(uint32_t color, int wait) { 151 for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip... 152 strip.setPixelColor(i, color); // Set pixel's color (in RAM) 153 strip.show(); // Update strip to match 154 delay(wait); // Pause for a moment 155 } 156} 157void Motor_Drive(int a) 158 { 159 if ( a == 'F') 160 { 161//前進 162 digitalWrite(LM1,HIGH); 163 digitalWrite(LM2,LOW); 164 digitalWrite(RM1,HIGH); 165 digitalWrite(RM2,LOW); 166} 167 else if ( a == 'B' ) 168 { 169//後進 170 digitalWrite(LM1,LOW); 171 digitalWrite(LM2,HIGH); 172 digitalWrite(RM1,LOW); 173 digitalWrite(RM2,HIGH); 174} 175 else if ( a == 'S') 176 { 177 digitalWrite(LM1,HIGH); 178 digitalWrite(LM2,HIGH); 179 digitalWrite(RM1,HIGH); 180 digitalWrite(RM2,HIGH); 181} 182 else if ( a == 'L' ) 183 { 184 digitalWrite(LM1,LOW); 185 digitalWrite(LM2,HIGH); 186 digitalWrite(RM1,HIGH); 187 digitalWrite(RM2,LOW); 188} 189 else if ( a == 'R' ) 190{ 191 digitalWrite(LM1,HIGH); 192 digitalWrite(LM2,LOW); 193 digitalWrite(RM1,LOW); 194 digitalWrite(RM2,HIGH); 195} 196} 197 void Motor_Power(int a){ 198 int b,c; 199 if(advancea==1){ 200 b=a+add_powerR; 201 c=a+add_powerL; 202} 203 else{ 204 b=a; 205 c=a; 206} 207 b=constrain(b,70,254); 208 c=constrain(c,70,254); 209 analogWrite(RPWM, b); 210 analogWrite(LPWM, c); 211} 212int Random_value(int Dom){//ランダム値決定用 213int a =random(10000); 214Dom=a%Dom; 215return (Dom); 216} 217int wait_sec(int waitsec) {//移動時間倍数決定 218if (waitsec >= 0 && waitsec < 3259) return(1) ; //32.59% 219if (waitsec >= 3259 && waitsec < 4677) return(2) ; //14.18% 220if (waitsec >= 4677 && waitsec < 5549) return(3) ; //8.72% 221if (waitsec >= 5549 && waitsec < 6166) return(4) ; //6.17% 222if (waitsec >= 6166 && waitsec < 6638) return(5) ; //4.72% 223if (waitsec >= 6638 && waitsec < 7017) return(6) ; //3.79% 224if (waitsec >= 7017 && waitsec < 7332) return(7) ; //3.15% 225if (waitsec >= 7332 && waitsec < 7600) return(8) ; //2.68% 226if (waitsec >= 7600 && waitsec < 7833) return(9) ; //2.33% 227if (waitsec >= 7833 && waitsec < 8038) return(10) ; //2.05% 228if (waitsec >= 8038 && waitsec < 8221) return(11) ; //1.83% 229if (waitsec >= 8221 && waitsec < 8386) return(12) ; //1.65% 230if (waitsec >= 8386 && waitsec < 8536) return(13) ; //1.50% 231if (waitsec >= 8536 && waitsec < 8673) return(14) ; //1.37% 232if (waitsec >= 8673 && waitsec < 8799) return(15) ; //1.26% 233if (waitsec >= 8799 && waitsec < 8916) return(16) ; //1.17% 234if (waitsec >= 8916 && waitsec < 9024) return(17) ; //1.08% 235if (waitsec >= 9024 && waitsec < 9125) return(18) ; //1.01% 236if (waitsec >= 9125 && waitsec < 9220) return(19) ; //0.95% 237if (waitsec >= 9220 && waitsec < 9309) return(20) ; //0.89% 238if (waitsec >= 9309 && waitsec < 9393) return(21) ; //0.84% 239if (waitsec >= 9393 && waitsec < 9472) return(22) ; //0.79% 240if (waitsec >= 9472 && waitsec < 9547) return(23) ; //0.75% 241if (waitsec >= 9547 && waitsec < 9618) return(24) ; //0.71% 242if (waitsec >= 9618 && waitsec < 9686) return(25) ; //0.68% 243if (waitsec >= 9686 && waitsec < 9751) return(26) ; //0.65% 244if (waitsec >= 9751 && waitsec < 9813) return(27) ; //0.62% 245if (waitsec >= 9813 && waitsec < 9872) return(28) ; //0.59% 246if (waitsec >= 9872 && waitsec < 9929) return(29) ; //0.57% 247if (waitsec >= 9929) return(30) ; //0.55% 248return(-1) ; 249} 250void Random_Walk(int c)// 251{ 252const int wait = c; //30 回転 253const int wait2 = 6000;//移動時間基準値 254double a,b; 255if(rnd_rst==false){//ランダム値決定 256rnd_f =Random_value(9999); //移動距離決定 257rnd_d =Random_value(2); //回転方向決定 258rnd_c =Random_value(3)+1; //回転角度決定 259rnd_rst =true; 260a=wait_sec(rnd_f); 261F_time =wait2 * a ;// 262V_wait = wait * rnd_c;//deg 30 60 90 263} 264if(millis() - before <=V_wait && advancea==0 && rnd_d == 0){//左 30 回転 265Motor_Drive('L'); 266Motor_Power(Motor_Out2); 267} 268else if(millis() - before <=V_wait + 2000 && advancea==0 && rnd_d ==0){//前進時 269 270advancea=1; 271before=millis(); 272rnd_rst =false; 273} 274else if(millis() - before <=V_wait && advancea==0 && rnd_d ==1){//右 30 回転 275Motor_Drive('R'); 276Motor_Power(Motor_Out2); 277} 278else if(millis() - before <=V_wait + 2000 && advancea==0 && rnd_d ==1){//前進時 279advancea=1; 280before=millis(); 281rnd_rst =false; 282} 283//前進 284else if(millis() - before <= F_time && advancea==1){ //直進:障害物軽回避 285stackL=0; 286stackR=0; 287direc=false; 288Motor_Drive('F'); 289Motor_Power(Motor_Out); 290} 291//後進 292else if(millis() - before <= F_time && advancea==2){ 293stackL=0; 294stackR=0; 295direc=true; 296Motor_Drive('B'); 297Motor_Power(Motor_Out); 298} 299//初期化&同期 300else{ 301advancea=0; 302before=millis(); 303stackL = 0; 304stackR = 0; 305rnd_rst=false; 306} 307return; 308} 309void No_Delay_Avoid(){ 310const int wait = 400;//回転時間基準値 311if(rnd_rst2==false){//ランダム値決定 312before3=millis(); 313rnd_rst2 =true; 314} 315if(L1_blo==true){//左前方障害物フラグがオンのとき実行 316if(millis()-before3<=wait){ 317Motor_Drive('R'); 318Motor_Power(Motor_Out2); 319} 320else{ 321before=before+wait; 322L1_blo=false; 323rnd_rst2 =false; 324return; 325} 326} 327else if(R1_blo==true){ 328if(millis()-before3<=wait){ 329Motor_Drive('L'); 330Motor_Power(Motor_Out2); 331} 332else{ 333before=before+wait; 334R1_blo=false; 335rnd_rst2 =false; 336return; 337} 338} 339else if(L2_blo==true){ 340if(millis()-before3<=wait*0.5){ 341Motor_Drive('R'); 342Motor_Power(Motor_Out2); 343} 344else{ 345before=before+wait; 346L2_blo=false; 347rnd_rst2 =false; 348return; 349} 350} 351else{ 352if(millis()-before3<=wait*0.5){ 353Motor_Drive('L'); 354Motor_Power(Motor_Out2); 355} 356else{ 357before=before+wait; 358R2_blo=false; 359rnd_rst2 =false; 360return; 361} 362} 363return; 364} 365 366・障害物回避のプログラム 367・ターゲット探索のプログラム 368 369//シリアルモニタ用プログラム 370if (packet[0] == 0x7e){ 371 for ( int i = 0 ; i < 30 ; i++ ){ 372 delay(1); 373} 374} 375else{ 376 delay(1); 377} 378return 0; 379}//受信関数ここまで。

試したこと

レヴィーウォークで動くロボットのプログラムにLEDテープを光らせるプログラムを書き込み2つのプログラムを動かす(TimedAction)を追加したのですがLEDが光るだけでロボットは動かない。逆にロボットが動いた場合はLEDが光らなくなる。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

thkana

2021/12/23 12:19

どういう基準で掲載プログラムを削ったのかわからないけれど、少なくともこれではプログラムの動作を追えません。 それと、他の環境に移植するつもりがないのなら、無用な#ifdefは削った方がいいです。読みにくい。
guest

回答1

0

早く全文が出ないか待っているんだが・・・

このスケッチはLEDテープの部分無しでちゃんとロボットは動くのですか?

RPWM = 5;

RSSI_Read = 5;

pinMode(RPWM,OUTPUT);

pinMode(RSSI_Read,INPUT);

このあたりにも原因がありそうだけど、RSSI_Readがゴミなのか使っているのかが判断出来ない。
因みにAVRマイコンならdigitalReadすれば、PWMは停止させる筈。

それと私見だが、そんなちまいライブラリなんか使わずにそのまま

void loop(){ static uint32_t time; stepper_control(); if(millis()-time>=1000){ time=millis(); led_control(); } }

のように自分で書いた方が良い。何でこの程度の事でライブラリを使ったのか?
(ライブラリを理解しないまま使うのは一番良くない)

更に言えば、ステッピングモータを使っていないのにstepper_controlの名前も如何なものか?

投稿2021/12/24 16:31

nac_tnk

総合スコア463

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問