Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 6 months ago.
2つのプログラムを同時に動作させる方法
御世話になっております。
申し訳ありません、2つのプログラムを同時に動作させる方法について教えてください。
サーボモータ4個を6行から47行目までの関数を作って動作させるにあたり 59行目から61行目と、67行目から69行目までを同時に動作させたいのですが 良い方法はありませんでしょうか。
1 #include "mbed.h" 2 3 PwmOut LeftShoulder(p21),LeftElbow(p22); 4 PwmOut RightShoulder(p24),RightElbow(p25); 5 6 int LeftArmSo0() 7 { 8 LeftShoulder.pulsewidth_us(2389); 9 LeftElbow.pulsewidth_us(1067); 10 wait(0.5); 11 return 0; 12 } 13 int LeftArmSi1() 14 { 15 LeftShoulder.pulsewidth_us(1611); 16 LeftElbow.pulsewidth_us(667); 17 wait(0.5); 18 return 0; 19 } 20 int LeftArmDo3() 21 { 22 LeftShoulder.pulsewidth_us(778); 23 LeftElbow.pulsewidth_us(1389); 24 wait(0.5); 25 return 0; 26 } 27 int RightArmSo0() 28 { 29 RightShoulder.pulsewidth_us(2222); 30 RightElbow.pulsewidth_us(1500); 31 wait(0.5); 32 return 0; 33 } 34 int RightArmSi1() 35 { 36 RightShoulder.pulsewidth_us(1278); 37 RightElbow.pulsewidth_us(2278); 38 wait(0.5); 39 return 0; 40 } 41 int RightArmDo3() 42 { 43 RightShoulder.pulsewidth_us(700); 44 RightElbow.pulsewidth_us(1833); 45 wait(0.5); 46 return 0; 47 } 48 49 int main() 50 { 51 while(1) 52 { 53 LeftShoulder.pulsewidth_us(2500); 54 LeftElbow.pulsewidth_us(1500); 55 RightShoulder.pulsewidth_us(500); 56 RightElbow.pulsewidth_us(1500); 57 wait(2); 58 59 LeftArmSo0(); 60 LeftArmSi1(); 61 LeftArmDo3(); 62 63 LeftShoulder.pulsewidth_us(2500); 64 LeftElbow.pulsewidth_us(1500); 65 wait(2); 66 67 RightArmSo0(); 68 RightArmSi1(); 69 RightArmDo3(); 70 } 71 }
1 Answer
7 years, 6 months ago.
こんにちは。
まず、ソースをポストするときには
< < code > > ソース < < / code > >
という書式でポストしていただくと
読めるコードになると思います。
※ < と > の間にスペースはいれません。
二つのプログラムを同時に動作させる
ということを厳密に受け止めると
マルチタスクOSを使用するか、
FPGAのようなハードウェアを使用することが
必要になるかと思います。
“だいたい同時に”ということであれば
下記のようなアプローチは如何でしょうか?
(1) 各パーツ関数にある wait(0.5) を外す。
(2) main関数内で下記のように並び替える。
int main()
{
while(1) {
LeftShoulder.pulsewidth_us(2500) ;
LeftElbow.pulsewidth_us(1500) ;
RightShoulder.pulsewidth_us(500) ;
RightElbow.pulsewidth_us(1500) ;
wait(2) ;
LeftArmSo0() ;
RightArmSo0() ;
wait(0.5) ;
LeftArmSi1() ;
RightArmSi1() ;
wait(0.5) ;
LetArmDo3() ;
RightArmDo3() ;
wait(0.5) ;
}
}
moto
お世話になります。
分かりずらい問い合わせになってしまい申し訳ありません。 回答いただきましてありがとうございます。
教えていただいた内容で作り直してみます。
よろしければ マルチタスクOSを使用した場合どうなるのかも 教えていただけませんでしょうか。
posted by 07 May 2018実験できるハードウェアが無いので printf だけでテストプログラムを書いてみました。
プログラムは mbed OS Blinky LED HelloWorld をテンプレートにして作成後、main.cpp を
以下のようにモディファイしました。
この場合、右と左は独立に動いてしまいますので、なんらかの同期手段を用意する必要があると思います。
恐らく main の中で、それぞれの thread に対して同期をとるような手法が必要になると思いますが、
そのあたりは "thread" "同期" などでググって研究してみていただけたならと思います。
#include "mbed.h"
Thread left_arm ;
Thread right_arm ;
void LeftArmHome(void)
{
printf("Left Arm Home position\n") ;
// LeftShoulder.pulsewidth_us(2500);
// LeftElbow.pulsewidth_us(1500);
}
void LeftArmSo0(void)
{
printf("Left So0\n") ;
}
void LeftArmSi1(void)
{
printf("Left Si1\n") ;
}
void LeftArmDo3(void)
{
printf("Left Do3\n") ;
}
void RightArmHome(void)
{
printf("\t\t\tRight Arm Home position\n") ;
// RightShoulder.pulsewidth_us(500);
// RightElbow.pulsewidth_us(1500);
}
void RightArmSo0(void)
{
printf("\t\t\tRight So0\n") ;
}
void RightArmSi1(void)
{
printf("\t\t\tRight Si1\n") ;
}
void RightArmDo3(void)
{
printf("\t\t\tRight Do3\n") ;
}
void left_arm_thread(void)
{
while(1) {
LeftArmHome() ;
wait(2) ;
LeftArmSo0() ;
wait(0.5) ;
LeftArmSi1() ;
wait(0.5) ;
LeftArmDo3() ;
wait(2) ;
}
}
void right_arm_thread(void)
{
while(1) {
RightArmHome() ;
wait(2) ;
RightArmSo0() ;
wait(0.5) ;
RightArmSi1() ;
wait(0.5) ;
RightArmDo3() ;
wait(2) ;
}
}
int main() {
left_arm.start(left_arm_thread) ;
right_arm.start(right_arm_thread) ;
while(1) {
}
}
Assigned to 7 years, 6 months ago.
This means that the question has been accepted and is being worked on.