久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费
標題:
ProtoThreads多線程實例
[打印本頁]
作者:
51黑dd
時間:
2016-4-9 20:14
標題:
ProtoThreads多線程實例
發一個我前段時間用ProtoThreads寫的一個讀取Serial角度并用模擬的方式驅動舵機
關于ProtoThreads的介紹:
http://www.zg4o1577.cn/bbs/dpj-47836-1.html
代碼如下
#include <PT_timer.h>
#include <pt.h>
int servopin=8;//定義舵機接口數字接口7
int myangle=100;//定義角度變量
int val=0;
static struct pt pt1,pt2;
PT_timer servotimer;
static int servoMove(struct pt *pt)
{
PT_BEGIN(pt);
while(1)
{
servotimer.setTimer(25);
PT_WAIT_UNTIL(pt,servotimer.Expired());
int pulsewidth;//定義脈寬變量
myangle%=156;//視舵機而定,防止越界
pulsewidth=myangle*11+500;//將角度轉化為500-2205的脈寬值
digitalWrite(servopin,HIGH);//將舵機接口電平至高
delayMicroseconds(pulsewidth);//延時脈寬值的微秒數
digitalWrite(servopin,LOW);//將舵機接口電平至低
}
PT_END(pt);
}
String inString = "";
static int angleRead(struct pt *pt)
{
PT_BEGIN(pt);
while(1) {
PT_WAIT_UNTIL(pt, Serial.available()>0);
int inChar = Serial.read();
if (isDigit(inChar))
inString += (char)inChar;
if (inChar == ' '||inChar=='\n') {
myangle=inString.toInt();
Serial.print("myangle=");
Serial.println(myangle);
inString = "";
}
}
PT_END(pt);
}
void setup()
{
pinMode(servopin,OUTPUT);//設定舵機接口為輸出接口
PT_INIT(&pt1);
PT_INIT(&pt2);
Serial.begin(9600);
}
void loop()//
{
servoMove(&pt1);
angleRead(&pt2);
}
復制代碼
ServoMove呢就是自己模擬出來的舵機函數,為什么不用servo庫呢?因為arduino自身的servo庫有很多限制,第一PWM不能用了,第二最小角度只有1度,當然這個程序里舵機的精度也是1度,只要把相應的變量改成float就能精確到小數了,不過最大的好處是舵機腳可以任意設定
servomove中限定了最大角度為155度,這個與舵機有關
angleRead中使用了stringtoInt,每次可以輸入一個角度,角度可以以 空格 或 回車 結尾(操蛋的VC2010 SerialMonitor發送數據不帶 回車的。。那就空格了)
程序中使用了定時器,自己用C++寫的,第一次寫庫,照葫蘆畫瓢了。。。
感謝czad的擴展庫翻譯:
http://www.zg4o1577.cn/bbs/dpj-47837-1.html
這個庫用起來非常簡單:
PT_Timer t;//定義一個定時器
t.setTimer(unsinged long time) //定時時間,單位ms
t.Expired()//判斷定時器是否溢出,是返回值>0
在上面的代碼中,一個舵機周期是20ms,前面的約2.5毫秒是信號周期,剩下的10多ms全是無用的低電平,然而又必不可少,所以果斷用定時器取代delay
ok
pt.h
/*
* Copyright (c) 2004-2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: pt.h,v 1.6 2006/06/03 11:29:43 adam Exp $
*/
/**
* \addtogroup pt
* @{
*/
/**
* \file
* Protothreads implementation.
* \author
* Adam Dunkels <adam@sics.se>
*
*/
#ifndef __PT_H__
#define __PT_H__
#include "lc.inc"
struct pt {
lc_t lc;
};
#define PT_WAITING 0
#define PT_EXITED 1
#define PT_ENDED 2
#define PT_YIELDED 3
/**
* \name Initialization
* @{
*/
/**
* Initialize a protothread.
*
* Initializes a protothread. Initialization must be done prior to
* starting to execute the protothread.
*
* \param pt A pointer to the protothread control structure.
*
* \sa PT_SPAWN()
*
* \hideinitializer
*/
#define PT_INIT(pt) LC_INIT((pt)->lc)
/** @} */
/**
* \name Declaration and definition
* @{
*/
/**
* Declaration of a protothread function.
*
* This macro is used to declare a protothread function. Protothreads
* function should be declared with this macro, but can also be
* declared as regular C functions that return an integer value.
*
* \param name_args The name and arguments of the C function
* implementing the protothread.
*
* \hideinitializer
*/
#define PT_THREAD(name_args) char name_args
/**
* Declare the start of a protothread inside the C function
* implementing the protothread.
*
* This macro is used to declare the starting point of a
* protothread. It should be placed at the start of the function in
* which the protothread runs. All C statements above the PT_BEGIN()
* invokation will be executed each time the protothread is scheduled.
*
* \param pt A pointer to the protothread control structure.
*
* \hideinitializer
*/
#define PT_BEGIN(pt) { char PT_YIELD_FLAG = 1; LC_RESUME((pt)->lc)
/**
* Declare the end of a protothread.
*
* This macro is used for declaring that a protothread ends. It must
* always be used together with a matching PT_BEGIN() macro.
*
* \param pt A pointer to the protothread control structure.
*
* \hideinitializer
*/
#define PT_END(pt) LC_END((pt)->lc); PT_YIELD_FLAG = 0; \
PT_INIT(pt); return PT_ENDED; }
/** @} */
/**
* \name Blocked wait
* @{
*/
/**
* Block and wait until condition is true.
*
* This macro blocks the protothread until the specified condition is
* true.
*
* \param pt A pointer to the protothread control structure.
* \param condition The condition.
*
* \hideinitializer
*/
#define PT_WAIT_UNTIL(pt, condition) \
do { \
LC_SET((pt)->lc); \
if(!(condition)) { \
return PT_WAITING; \
} \
} while(0)
/**
* Block and wait while condition is true.
*
* This function blocks and waits while condition is true. See
* PT_WAIT_UNTIL().
*
* \param pt A pointer to the protothread control structure.
* \param cond The condition.
*
* \hideinitializer
*/
#define PT_WAIT_WHILE(pt, cond) PT_WAIT_UNTIL((pt), !(cond))
/** @} */
/**
* \name Hierarchical protothreads
* @{
*/
/**
* Block and wait until a child protothread completes.
*
* This macro schedules a child protothread. The current protothread
* will block until the child protothread completes.
*
* \note The child protothread must be manually initialized with the
* PT_INIT() function before this function is used.
*
* \param pt A pointer to the protothread control structure.
* \param thread The child protothread with arguments
*
* \sa PT_SPAWN()
*
* \hideinitializer
*/
#define PT_WAIT_THREAD(pt, thread) PT_WAIT_WHILE((pt), PT_SCHEDULE(thread))
/**
* Spawn a child protothread and wait until it exits.
*
* This macro spawns a child protothread and waits until it exits. The
* macro can only be used within a protothread.
*
* \param pt A pointer to the protothread control structure.
* \param child A pointer to the child protothread's control structure.
* \param thread The child protothread with arguments
*
* \hideinitializer
*/
#define PT_SPAWN(pt, child, thread) \
do { \
PT_INIT((child)); \
PT_WAIT_THREAD((pt), (thread)); \
} while(0)
/** @} */
/**
* \name Exiting and restarting
* @{
*/
/**
* Restart the protothread.
*
* This macro will block and cause the running protothread to restart
* its execution at the place of the PT_BEGIN() call.
*
* \param pt A pointer to the protothread control structure.
*
* \hideinitializer
*/
#define PT_RESTART(pt) \
do { \
PT_INIT(pt); \
return PT_WAITING; \
} while(0)
/**
* Exit the protothread.
*
* This macro causes the protothread to exit. If the protothread was
* spawned by another protothread, the parent protothread will become
* unblocked and can continue to run.
*
* \param pt A pointer to the protothread control structure.
*
* \hideinitializer
*/
#define PT_EXIT(pt) \
do { \
PT_INIT(pt); \
return PT_EXITED; \
} while(0)
/** @} */
/**
* \name Calling a protothread
* @{
*/
/**
* Schedule a protothread.
*
* This function shedules a protothread. The return value of the
* function is non-zero if the protothread is running or zero if the
* protothread has exited.
*
* \param f The call to the C function implementing the protothread to
* be scheduled
*
* \hideinitializer
*/
#define PT_SCHEDULE(f) ((f) == PT_WAITING)
/** @} */
/**
* \name Yielding from a protothread
* @{
*/
/**
* Yield from the current protothread.
*
* This function will yield the protothread, thereby allowing other
* processing to take place in the system.
*
* \param pt A pointer to the protothread control structure.
*
* \hideinitializer
*/
#define PT_YIELD(pt) \
do { \
PT_YIELD_FLAG = 0; \
LC_SET((pt)->lc); \
if(PT_YIELD_FLAG == 0) { \
return PT_YIELDED; \
} \
} while(0)
/**
* \brief Yield from the protothread until a condition occurs.
* \param pt A pointer to the protothread control structure.
* \param cond The condition.
*
* This function will yield the protothread, until the
* specified condition evaluates to true.
*
*
* \hideinitializer
*/
#define PT_YIELD_UNTIL(pt, cond) \
do { \
PT_YIELD_FLAG = 0; \
LC_SET((pt)->lc); \
if((PT_YIELD_FLAG == 0) || !(cond)) { \
return PT_YIELDED; \
} \
} while(0)
/** @} */
#endif /* __PT_H__ */
/** @} */
復制代碼
附上
ProtoThreads Timer.rar
(13.23 KB, 下載次數: 11)
2016-4-9 20:12 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.zg4o1577.cn/bbs/)
Powered by Discuz! X3.1
主站蜘蛛池模板:
日本久久久久久
|
亚洲精品中文字幕中文字幕
|
久久国产一区二区三区
|
欧美日韩专区
|
日韩性生活网
|
在线观看视频福利
|
91色站
|
成人亚洲网站
|
日批的视频
|
亚洲免费精品
|
日本免费一区二区三区四区
|
亚洲精品www.
|
视频一区二区中文字幕
|
亚洲视频免费观看
|
久久91av
|
久久久久久国产
|
日韩av在线一区
|
精品视频在线免费观看
|
一区二区三区 在线
|
99re99
|
日韩视频在线播放
|
久久精品在线
|
日本视频中文字幕
|
国际精品鲁一鲁一区二区小说
|
av成年人网站
|
成人在线播放
|
亚洲精品视频久久
|
h视频在线观看免费
|
欧美在线一区二区三区
|
亚洲成人综合网站
|
日韩中文一区二区三区
|
亚洲97
|
一级在线观看
|
福利国产
|
久久国产日韩欧美
|
91在线精品视频
|
天啪
|
欧美成视频在线观看
|
欧美一区二区三区在线视频
|
久草新视频
|
日韩在线一区二区三区
|