2011-09-24

leJOS新書-Brain Bagnall"Intelligence Unleashed: Creating LEGO NXT Robots with Java"

昨天收到 Amazon DM,leJOS發展團隊的關鍵人物 Brain Bagnall 在繼 2007年的“Maximum LEGO NXT: Building Robots with Java Brains”之後又有一本關於leJOS project的新書上市。



新書書名為:

Intelligence Unleashed: Creating LEGO NXT Robots with Java

在Amazon上的內容介紹為:

With user-friendly instructions for the leJOS NXJ software, this accessible manual is the perfect launch into LEGO Mindstorms NXT, an incredible kit for building and programming robots. Using Java, the most popular and easy-to-use programming language available, this book helps engineers and amateurs alike design and build their own customized robots, programmed however they desire. A diverse set of projects is accompanied by building tips, programming code, complete 3D-rendered building instructions, and hundreds of illustrations. The NXT intelligent brick’s Bluetooth capabilities are introduced, as are the newest available parts, from GPS to the RFID sensor, in this useful handbook that serves as the perfect compliment to the LEGO NXT kit.

以上看不出來有什麼吸引人的章節,倒是leJOS官網的簡介,還較能瞭解這本新書的重點內容:
As the title suggests, the projects focus on robotics intelligence and advanced capabilities: Segway, Ballbot, Monte Carlo localization, realtime map display, outdoor GPS, robotic arm, the new navigation model, A* Search, image analysis and much more! There are 29 chapters with complete plans for 10 unique robots.

前一本書在Amazon的書評有一些負面的意見,主要原因是當時leJOS還在發展階段,版本尚未穩定,部份內容並未能與實際的最新版本同步,造成讀者使用上的困擾。
之後有了第二版的上市消息,不過預購之後等了將近兩年還是無疾而終,如今這本書能夠上市倒還真的有購買的衝動,只是需要衡量有沒有多餘的時間能夠再次投入。

物件導向語言,個人覺得是最適合來建構機器人行為的發展平台,雖然有點門檻,不過深入研究之後後續的應用就無限寬廣了。
目前市面上的相關書籍並不多,熱情的CAVE團隊在去年也出版了一本“

機器人程式設計與實作:使用Java

”,曾獲贈一本,目前當作工具書用途,還蠻實用的。

2011-09-18

Arduino announce new products at Maker Faire in NYC.


消息來源:arduino.cc blog

Arduino 1.0,
we finally froze the Arduino API, the IDE and the layout of the boards. We’ve made some minor additions to the Arduino connectors to make them more flexible. Tomorrow you will be able to download the release candidate and in 1 month of frantic testing with the community, the platform will be ready and stable.


Arduino Leonardo,
a low cost Arduino board with the Atmega32u4. It has the same shape and connectors as the UNO but it has a simpler circuit. On the software side it has a nifty USB driver able to simulate a mouse , a keyboard, a serial port (with more drivers coming later). As usual for Arduino, everything will be released as open source (Core, Bootloader, Hardware).
UNO的升級版?


Arduino Due,
a major breakthrough for Arduino because we’re launching an Arduino board with a 32bit Cortex-M3 ARM processor on it. We’re using the SAM3U processor from ATMEL running at 96MHz with 256Kb of Flash, 50Kb of Sram, 5 SPI buses, 2 I2C interfaces, 5 UARTS, 16 Analog Inputs at 12Bit resolution and much more.


Instead of just releasing the finished platform we are opening the process to the community early on. We’re going to be demoing the board and giving away some boards to a selected group of developers who will be invited to shape the platform while it’s been created. After Maker Faire, we will begin selling a small batch of Developer Edition boards on the Arduino store (store.arduino,cc) for members of the community who want to be join the development effort. We plan a final and tested release by the end of 2011


Arduino Wifi Shield. It adds Wi-Fi communication capabilities to any Arduino. Instead of using any of the classic WiFi modules on the market we wanted to have something that will provide the maximum level of hackability to the user. The shield is based on a wifi micro module made by H&D Wireless coupled with a powerful AVR32 processor that carries the full TCP-IP stack leaving room to add your own protocols and customisations. We’ve also worked hard to make sure that you will be able to migrate your code from the Ethernet Shield with minor changes.
最期待的是這個Shield,終於有機會能夠讓 NXT 使用無線網路 :-)




2011-09-15

使用NXT控制Arduino Network Device



在Arduino
I2C Wire.h Multi-Masters功能實作
中以Arduino+Gravitech
7-SEG Shield
組合成為一個I2C Slave Device,可以提供其它的Arduino使用I2C Master方式控制LED的顯示,藉以驗證Wire.h具備多重I2C Master的處理功能。


而原先的構想是任何可以進行I2C通訊的裝置,應該都可以控制這個I2C Slave Device,包含NXT在內,而事實上NXT對Arduino的I2C通訊也經由實際測試,運作正常。

不過經由多次的測試,NXT始終無法如同Arduino一般能夠正常的存取這個I2C Salve
Device
,而比較之前的測試Case,唯一的差別就在於Gravitech 7-SEG
Shield
擴充板所內建對於SDA及SCL的4.7k ohm pull-up,實在是百思不解問題真正所在。




Arduino Network Device建構

(Photo)



而為了讓NXT可以使用Gravitech 7-SEG Shield的功能,所以改造原來的I2C Slave
Device
成為Network Device。


不再使用I2C而改為Ethernet TCP/IP方式,:

(1).
以Arduino UNO + Gravitech 7-SEG Shield +
Arduino Ethernet
Shield
組合成為 Telnet Server。


(2).
另外使用 Arduino UNO + Arduino Ethernet Shield組合成為Telnet Client。

至於NXT則透過I2C傳送指令與資料給Telnet Client,由Client端UNO組合成封包之後經由網路線傳送至Telnet Server接收,而Server端的UNO解析之後再以I2C控制Gravitech 7-SEG Shield執行指定的功能,有點複雜但可玩性十足。

(
架構圖)



(Demo Video)









NXT對於Network Device控制流程

整體架構共分成三個部份:

(1). NXT to Telnet Client
:

將Telnet Client作為I2C Slave Device,規劃相關功能對應的Register Address及Data需求:

0x00
-
回傳裝置資訊,這是用來測試I2C通訊功能是否正常。

0x41+
三色LED顯示指令(R/G/B)  -
控制Gravitech 7-SEG
Shield
顯示指定顏色。


0x42+
英數字串 - 控制Gravitech 7-SEG
Shield
將字串顯示在七段式LED。


0x43+
整數值字串 - 控制Gravitech 7-SEG
Shield
將數字顯示在七段式LED。


因此,NXT即將前述的指令以LowspeedWrite()傳送給Client端的UNO,而對於UNO即會觸發onReceive()的handler進行接收。



(2). Telnet Client to Server
:

Client端UNO負責將接收到的NXT
request
解析並製作成資料封包後,使用Ethernet.h library的功能,透過Arduino Ethernet Shield以網路線傳送至Server端,而Server端的UNO同樣也是以Ethernet.h的功能接收封包資料。




(3). Telnet Server to
Gravitech 7-SEG Shield

Server端經解析來自Client端的資料之後,即分別使用Digital I/O控制三色LED的顏色顯示以及以I2C方式控制七段式LED顯示字串資料。






後記

(1).
Ethernet.h雖然區分成server與client兩個classes的功能,但不是限制只能在個別角色中才能使用的methods,尤其在server端反而會用到很多Client
class
的功能來控制資料的接收,這一點是官方說明文件比較陽春的地方。

(2).
除了本case所使用的telnet(port 23)協定之外,外部很常見的http協定(port 80)就可以讓NXT結合Web server應用透過browser來控制,這是另外一個有趣的應用領域。



範例程式

(1) NXT
端程式:NXT_7SEG_DEMO.nxc

(2) Telnet Client
端程式:TelnetClient_I2CSlave.pde

(3) Telnet Server
端程式:TelnetServer_7SEGLED.pde

  • 留言者: ㄚ仁
  • Email: pciahb@yahoo.com.tw
  • 網址:
  • 日期: 2012-07-14 01:14:22
您好 想請教您一個問題 我將ethernet shield 接到Hub 然後把Arduino的範例程式(Web Server)燒進去我的Arduino UNO 後 輸入IP到網址列 卻沒有辦法連結到那個IP 請問是我IP輸入錯誤嗎? 如果是 那請問我應該如何得知我的IP應該輸入甚麼才恰當呢? 另外 範例程式裡有一個Web Client 程式碼裡面有一個IP是http://173.194.33.104/ 我有看到youtube裡的影片有關Ethernet的介紹 也看到他將http://173.194.33.104/這個IP輸入到網址列後 會連到GOOGLE 但是我自己測試卻不會 請問這兩件事之間有關連嗎? 我知道這和您這篇文章可能沒有任何關係 很抱歉 但我看了許多網站 都沒有辦法解決我的問題 因此請教您 煩請您撥空指教 感激不盡!
[版主回覆07/14/2012 12:30:59]Google的IP常常在變, 所以先使用命令提示字元去ping www.google.com, 就可以解析到正確的IP.
我剛才ping 到的IP是 74.125.31.147

2011-09-06

Arduino I2C Wire.h Multi-Masters功能實作(I2C Slave Device)



Gravitech的Arduino
7SEG Shield
為具有三個I2C裝置的Arduino擴充板,包含:


(1) 以SAA1064晶片驅動的4位數7段LED (I2C address: 0x38)、

(2) TMP75數位溫度感應器 (I2C address:0x49)、

(3) 24LC128 EEPROM (I2C address:0x50)。


關於各I2C裝置的用法,可參考原廠Datasheet,同時也可以參照Gravitech所提供的Sample
Code
,示範由TMP75讀取溫度值然後顯示在LED的作法。




Arduino多功能I2C裝置實作




在前篇” NXT與Arduino多重I2C Master之實作測試”對於Arduino Wire.h Library I2C Multi-Master功能的實作,這裡再以相同的應用方式改以Arduino UNO加上這個Shield組合而成為一個”多功能I2C裝置”作為實作範例,

並規劃
UNO兼具以下之I2C Slave與Master雙角色功能:




※由UNO擔任Slave角色,負責接收與回應來自其他外部裝置對於”多功能I2C裝置”的I2C操作需求。

※當UNO處理外部I2C訊息時,若需要控制擴充板裝置(SAA1064、TMP75)的運作, 則UNO轉換成為Master角色,傳送I2C指令操作指定裝置。



因此就”多功能I2C裝置”而言,UNO就如同擔任代理人(Delegate) 角色,負責處理外部使用需求的介面。

依據這樣的應用,規劃”多功能I2C裝置”的功能如下:

 

(1)
於UNO Sketch setup()先進行TMP75及SAA1064的初始化設定(Master角色),

完成之後以0x31
address
加入I2C bus, 在loop()即進入接收外部I2C通訊的待命狀態(Slave角色)。




(2)
於receiveEvent ()處理外部裝置使用LED顯示數字(Register
address: 0x42
)
與 字串(Register
address: 0x43
)
的功能,包含:數字、字母的LED字型轉換,以及字串以跑馬燈方式顯示。




(3)
於requestEvent()處理回復裝置資訊的需求(Register
address: 0x00
)
。




(4)
對於TMP75數位溫度計考慮讀取效率,由外部裝置直接以Slave address(0x49)進行讀取(回復2個Bytes),不間接透過UNO處理。




Arduino I2C
Slave Device
處理Master request須知




以Arduino UNO作為Slave device,對於處理與回應外部I2C request的Sketch程式碼,需要留意以下注意事項:



(1)
在loop()中,應該專職於Slave的角色,除非是要處理來自Master的要求,不要主動去啟始對其他Slave Device的I2C通訊,否則會造成外部裝置(Master)無法通訊的狀況。



(2)
在receiveEvent ()中,只進行資料的接收,後續的需求處理,應該在接收完成後回到loop()再進行,以免干擾整體的通訊過程。



(3)
在requestEvent()中,只處理資料的回復,必須維持以Slave角色完成通訊過程的完整性,如果要求回復的資料需要去向另一個Slave Device request,就必須拆分成兩次通訊過程來完成。




以NANO作為外部裝置測試







(1)
要求UNO Slave (0x31)
回傳它的裝置資訊 (Register address: 0x00) ,會觸發requestEvent() 回傳”UNO-Ox31”的字串。




(2)
要求TMP75 (0x49)
回傳所測量的溫度值。




(3)
要求UNO Slave (0x31)
在LED顯示數值資料(Register
address: 0x42
)
,會觸發receiveEvent() 接收顯示指令及數值,先判斷設定status flag (isDisplayNumber=true) 之後,回到loop () 即依據status flag啟始對SAA1064的數字顯示要求。




(4)
要求UNO Slave (0x31)
在LED以跑馬燈方式顯示字串(Register
address: 0x43
)
,會觸發receiveEvent () 接收顯示指令及字串,先判斷設定status flag (isDisplayText=true) 之後,回到loop () 即啟始對SAA1064的字串顯示要求。




Sketch程式:

(1)  Arduino I2C Slave Device -
Arduino_I2C_Device.pde

(2)  NANO
外部裝置 - NANO_CTRL_4SEG7.pde







2011-08-24

NXT與Arduino多重I2C Master之實作測試




Arduino
有許多的I2C週邊或擴充板,如何來讓NXT也能使用這些週邊? 若是能夠藉由Arduino主板來存取,那就大大的提昇了這些週邊的使用範圍與彈性。

對於這樣的應用,首先要確認的是:Wire.h library能否支援多個Master的運作,

更明確的說法是:Arduino主板是否可以同時身兼Master與Salve。



當它與NXT進行通訊時須擔任Salve,但當要存取I2C週邊時又得作為Master。




幾週前由Digital Cave這個網站,查到了以下對於Multi Master的說明資料:

The I2C protocol
is a multi-master protocol; however, the Wire library in the Arduino
distribution does not make it clear on how to implement this functionality.

…

 the documentation on the Arduino webpage
is slightly misleading in how
you would go about doing it. The documentation as currently written states that
for Wire.begin(address), the address is optional and 'if not specified, join
the bus as a master'. However, what is not stated is that if you transmit something from a device which has already
joined as a slave, it will assume the role of Master when it sends the data to another slave.


以上算是 Wire.h library的Missing Manual。

為了驗證以上的說法,延續之前NXT與Arduino之間的I2C通訊測試,實作了以下的測試Case:

(1)
使用兩部UNO,都模擬成I2C Slave裝置,其Address分別是0x31與0x33,而upload的sketch功能皆相同:

當它們接收到0x42+0x01/0x02的控制指令資料時,就會去開/關接在D13的LED(0x31為紅色,0x33為綠色),此外,又多增加了一個0x43的控制指令,附加的指令資料包含:另一部Slave的位址、要傳送的控制指令值以及LED的開關指令值。

(2)
將以上兩部UNO與NXT串接成I2C通訊架構。




首先由NXT以Lowspeedwrite()傳送[0x62, 0x43, 0x33, 0x42,
0x01]
的資料,


這時候0x31 Slave會接收到[0x43, 0x33, 0x42,
0x01]
的控制指令,


當它處理完onReceive() Handler之後,

接著它就會扮演起Master角色,send()出[0x33, 0x42, 0x01]的資料,

這時候0x33 Slave就會接收到[0x42, 0x01]的資料而去執行開啟LED(綠色)的指令。

所以依據以上的測式情境,0x31 Slave就是身兼Slave與Master的雙重角色。



NXT
新增的Function與呼叫程式碼:

void sendTransferCommand(const byte
deviceAddress,

                       const byte
deviceRegister,const byte anotherDevice,

                       const byte
anotherRegister, const byte anotherCommand)

{



     byte I2C_req_buf[];




     ArrayBuild(I2C_req_buf,
deviceAddress, deviceRegister,

              anotherDevice,
anotherRegister, anotherCommand);




     while(I2CCheckStatus(I2C_PORT)
== STAT_COMM_PENDING);




     LowspeedWrite(I2C_PORT,
0, I2C_req_buf);




}

…

sendTransferCommand(ARDUINO_RED_SEND_ADDRESS,

                   ARDUINO_TRANSFER_CMD,

                   ARDUINO_GREEN_ADDRESS,

                   ARDUINO_COMMAND_REG,
LED_ON);




Arduino
新增的Function與呼叫程式碼:

void loop()




{




     if
(isTransfer) {




       Wire.beginTransmission(anotherAddress);




      
Wire.send(anotherRegister);




      
Wire.send(anotherCommand);




      
Wire.endTransmission();




      
isTransfer = false;




     } 




}

void receiveEvent(int howMany)




{ 




   
requestRegister = Wire.receive();




    if
(requestRegister == 0x42) {




      
requestCommand = Wire.receive();




       if
(requestCommand == LED_ON) {




          digitalWrite(LED_PIN, HIGH);




       }




       else
if (requestCommand == LED_OFF) {




              
digitalWrite(LED_PIN, LOW);




           
}




    }




    else if
(requestRegister == 0x43) {




           
anotherAddress= Wire.receive();




           
anotherRegister = Wire.receive();




           
anotherCommand= Wire.receive();




           
isTransfer = true;




         }




}




2011-08-21

Mindstorms NXT與Arduino之間的I2C通訊





建立NXT與Arduino之間的I2C通訊,主要重點在於瞭解:

NXT Master
端程式如何傳送
以及 Arduino Slave端如何接收與回應。


I2C Address
對於7-bits定址當以一個byte(8-bits)來傳送時,需要將位址放在較高的7個bits(bit 1-bit 7),bit 0(LSB)是用來通知Slave目前Master是要傳送(0)或讀取(1),舉例:
若Slave Arduino的I2C 位址為0x31(Wire.begin(0x31))時,則NXT端Slave的 8-bits I2C位址就須設成0x62(0x31 << 1)。

NXC I2C Buffer
NXC以LowspeedWrite(port,retrunLength, I2C_input_buffer )來啟始並傳送資料至Slave,傳送的資料存放在I2C_input_buffer的陣列中,第一個元素所存的就是Slave的8-bits address,第二個元素及以後的資料就是要傳送至Slave的資料,而Slave端Arduino會觸發Wire.onReceive (handler)所註冊的handler接收來自NXT所傳送的資料。
而當NXC以LowspeedRead(port, bufferLength,I2C_output_buffer)要求Slave Arduino回傳資料時,會觸發Wire.onRequest (handler)所註冊的handler,所以要回傳給NXT的資料需要在onRequest
 handler
使用Wire. send()傳送。

測試案例
將Arduino模擬成NXT的I2C裝置,除了可以回傳裝置資訊外,另外若寫入0x42再加上LED的控制指令,可以開關Arduino上紅色與綠色的LED。

就如同前面所提,由onReceive handler接收來自NXC input buffer的資料,若判斷接收到的第一個byte是0x42則表示後續還須接收一個byte的LED控制指令,而若為要求回應裝置資訊的指令(0x00, 0x08, 0x10)時,則在NXT傳送 LowspeedRead()時於onRequest()handler中回傳資料。
在進行這個測試時,原來使用的接線方式與”兩部Arduino之間的I2C通訊”相同,但就是一直連不上,後來才發現問題出在Pull-up,拿掉後就一切OK了
更改後的接線圖如下:


2011-08-20

兩部Arduino之間的I2C通訊



Arduino I2C通訊使用A4(SDA)與A5(SCL)兩個腳位,API library則是<Wire.h>。

兩部Arduino之間的I2C通訊,需要有一方擔任Master負責主控資料通訊,另一方擔任Slave負責接收及回應來自Master的需求,而Master是依據Slave在I2C Bus中的位址來傳送與要求回應資料。

在<Wire.h>中Slave Arduino使用begin(address)來宣告加入I2C bus的位址,之後Master Arduino就可以使用:

(1)
beginTransmission(address)+send(data)+endTransmission()
傳送資料至Slave Arduino。

(2)
requestFrom(address,count)
向Slave Arduino要求回應資料。

Slave Arduino
處理來自Master
Arduino
的資料通訊需求是透過:


(1)
以onReceive(handler) 註冊處理當接收到來自Master Arduino send(data)所傳送資料時會觸發的處理程序(handler)。

(2)
以onRequest(handler) 註冊處理當接收到來自Master Arduino requestFrom(address,count)要求回應資料時會觸發的處理程序(handler)。






I2C bus address




<Wire.h>使用7-bits定址,參考有些I2C裝置在data sheet所載其實是8-bits定址,所以呼叫<Wire.h>函數時須要右移一個bit(除以2),像Mindsensors與HiTechnic裝置的手冊都是8-bits定址,還好是如果使用<NXTI2CDevice.h>會處理掉定址問題。

不過,如果將Arduino作為I2C Slave而NXT作為I2C Master,定址問題就很重要。






Arduino to Arduino I2C通訊線路接線




兩部Arduino之間的I2C通訊接線方式除了連接兩方的SDA(A4)與SCL(A5)之外,還須對這兩條線加pull-up,我是使用4.7k歐姆,另外還須要common
ground
,總之就是要確保電壓準位。


以下就是接線圖:








而測試案例則以Slave Arduino模擬NXT的I2C register layout,由Master Arduino讀取其裝置資訊




Slave Arduino程式



#include
<Wire.h>




#define I2C_SLAVE_ADDRESS 0x31




uint8_t softwareVersion[9] =
"V1.00   ";




uint8_t vendorID[9] = "Arduino
";




uint8_t deviceID[9= "UNO     ";




byte  requestRegister;




void setup()




{ 




    
Wire.begin(I2C_SLAVE_ADDRESS);




    
Wire.onReceive(receiveEvent);




    
Wire.onRequest(requestEvent);




}






void loop() { }




 




void receiveEvent(int howMany)




{




   
requestRegister = Wire.receive();




}




 




void requestEvent()




{




   
if (requestRegister == 0x00) {




      
Wire.send(softwareVersion, 8);




   
}




   
else if (requestRegister == 0x08) {




            Wire.send(vendorID, 8);




        
}




        
else if (requestRegister == 0x10) {




                 Wire.send(deviceID, 8);




              }




}






Master端程式



#include
<Wire.h>




const byte ARDUINO_SLAVE= 0x31;




const byte SOFTWARE_VERSION = 0x00;




const byte VENDOR_ID= 0x08;




const byte DEVICE_ID= 0x10;




const byte NUM_OF_BYTES= 8;




 




void setup()




{




   
Serial.begin(9600);




   
Wire.begin();   




}




 




void loop()




{     




   
requestFromSlave(SOFTWARE_VERSION);




   
delay(1000);




   
requestFromSlave(VENDOR_ID);




   
delay(1000);




   
requestFromSlave(DEVICE_ID);




   
delay(1000);




}




 




void requestFromSlave(const byte
slaveRegister)




{




   
byte nbytesReady= 0;




   
char strBuffer[9] = "       
";




 




   
Wire.beginTransmission(ARDUINO_SLAVE);




   
Wire.send(slaveRegister);




   
Wire.endTransmission();




   




   
Wire.requestFrom(ARDUINO_SLAVE, NUM_OF_BYTES);




   
nbytesReady = Wire.available();




   




   
for  (byte xi=0;
xi





        
strBuffer[xi] = Wire.receive();




   
Serial.println(strBuffer);          




}