[Android 結合 Google Chart API] 機器人原地轉一圈產生雷達圖

機器人原地轉一圈之後,將超音波感測器值透過藍牙送給Android 手機,再呼叫 Google Chart API 雲端圖表功能來取得雷達圖。可以畫出非常精美的圖表。重點在於靈活應用 Google Chart API  的語法就可以囉!

直接點選以下連結就可以看到雷達圖,詳細參數設定請參閱 Google Chart API 官網或以下延伸閱讀

https://chart.googleapis.com/chart?cht=r&chs=320×320&chxt=x&chxl=0:|0|45|90|135|180|225|270|315&chd=t:60,40,30,100,76,99,22,57,80

本範例是結合樂高機器人的超音波感測器來達到簡易地圖掃描的效果,歡迎試試看。以下範例是2014年淡江大學智慧型行動裝置整合機器人控制課程的作業之一。感謝電機系周煜同學(已畢業)做出相當好的版本呢。

 

12345

延伸閱讀:

Google Chart API 教學

Google Chart 隨機產生折線圖

淡江電機 Android 機器人整合課程網站

140523淡江電機 Android 行動裝置整合機器人控制課程,期末專題展示

Code Android端

package com.example.nxtsense;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Set;
import java.util.UUID;



import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
	private BluetoothAdapter adapter;
	private BluetoothSocket nxtSocket;
	public DataInputStream nxtDataIn;
	public DataOutputStream nxtDataOut;
    public final int MODE_CONNECT_NXT = 0, MODE_CONTROL = 1;
    private int mode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        adapter = BluetoothAdapter.getDefaultAdapter();
    	if(adapter==null)
        {
        	Toast.makeText(this, "No Bluetooth adapter found", Toast.LENGTH_SHORT).show();
        	this.finish();
        }
    	if(!adapter.isEnabled())
			startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1);
    	
    	try {
			setMode(MODE_CONNECT_NXT);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	Button getvalue=(Button)findViewById(R.id.get);
    	getvalue.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					CommandNXT();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
        
    }

    public void CommandNXT() throws IOException{
    	String y="";
    		nxtDataOut.writeInt(1);
    		nxtDataOut.flush();
    	
    	//	int x=nxtDataIn.readInt();
    		y=String.valueOf(nxtDataIn.readInt())+","
    				+String.valueOf(nxtDataIn.readInt())+","
    				+String.valueOf(nxtDataIn.readInt())+","
    				+String.valueOf(nxtDataIn.readInt())+","
    				+String.valueOf(nxtDataIn.readInt())+","
    				+String.valueOf(nxtDataIn.readInt())+","
    				+String.valueOf(nxtDataIn.readInt());
    	
					//TextView tv=(TextView)findViewById(R.id.value);
			    	//tv.setText(String.valueOf(x));
		
    	String myURL =" https://chart.googleapis.com/chart?cht=r&chs=320x320&chxt=x&chxl=0:|0|45|90|135|180|225|270|315&chd=t:"+y;         
        WebView myBrowser=(WebView)findViewById(R.id.mybrowser);  
  
        WebSettings websettings = myBrowser.getSettings();   
        websettings.setJavaScriptEnabled(true);  
         
        myBrowser.setWebViewClient(new WebViewClient());  
  
        myBrowser.loadUrl(myURL);  	
    	
    	
    	
    }
    public void setMode(int _mode) throws IOException
    {
    	Button connect=(Button)findViewById(R.id.buttonConnect);
    	mode = _mode;
    	if(mode==MODE_CONNECT_NXT)
    	{
    		
    		connect.setOnClickListener(new Button.OnClickListener() {
    			public void onClick(View arg0) {
    				connectNxt();
    				}
            });
    	}
    	
    	else if(mode==MODE_CONTROL)
    	{
    		connect.setEnabled(false);
    	}
    	else 
    		throw new IllegalArgumentException();
    }
    
    private void connectNxt()
    {
    	if(mode!=MODE_CONNECT_NXT) //檢查模式
    		throw new IllegalArgumentException();
    	
    	String name;
    	BluetoothDevice nxt = null;
    	
    	if((name = ((EditText) findViewById(R.id.editNxtName)).getText().toString()).equals("")) //檢查是否為空字串
    	{
    		Toast.makeText(this, "Please provide the name of your NXT", Toast.LENGTH_SHORT).show();
    		return;
    	}
    	
        Set<BluetoothDevice> devicesSet = adapter.getBondedDevices(); //取得裝置清單
        
        if(devicesSet.size()==0) //找不到裝置
        {
        	Toast.makeText(this, "No devices found", Toast.LENGTH_SHORT).show();
        	return;
        }
        
        for (BluetoothDevice device : devicesSet) //搜尋裝置
        {
            if (device.getName().equals(name))
            {
            	nxt = device;
                break;
            }
        }
        
        if(nxt==null) //找不到裝置
        {
        	Toast.makeText(this, "NXT not found", Toast.LENGTH_SHORT).show();
        	return;
        }
        
        try
        {
        	//建立nxt socket
			nxtSocket = nxt.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
			nxtSocket.connect();
			nxtDataOut = new DataOutputStream(nxtSocket.getOutputStream());
			nxtDataIn = new DataInputStream(nxtSocket.getInputStream());
		}
        catch(IOException e)
		{
        	Toast.makeText(this, "Connection failure", Toast.LENGTH_SHORT).show();
        	return;
		}

    	Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
		try {
			setMode(MODE_CONTROL);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }




   

}
Code. Android receive ultrasonic value from Lego robot

Code 機器人端使用 leJOS

import lejos.nxt.*;
import lejos.nxt.comm.*;
import lejos.util.Delay;

import java.io.*;
public class motor_control{
	public static void main(String args[]) throws IOException{
		Button.ESCAPE.addButtonListener(new ButtonListener(){
				public void buttonPressed(Button b){
					System.exit(0);
					}
				public void buttonReleased(Button b){
					
				}
		});
		System.out.println("Waiting");
		BTConnection btc = Bluetooth.waitForConnection(0, NXTConnection.RAW);
		DataInputStream dis = btc.openDataInputStream();
		DataOutputStream dos = btc.openDataOutputStream();
		System.out.println("Connected");
		UltrasonicSensor ultrasonic1 = new UltrasonicSensor(SensorPort.S2);
		int Ultrasonicvalue1;
		while(true){
			int a=dis.readInt();
			if(a==1){
				for(int i=0;i<7;i++){
					Motor.A.setSpeed(100);
					Motor.B.setSpeed(100);
					Motor.A.backward();
					Motor.B.forward();
					Delay.msDelay(822);
					Ultrasonicvalue1=ultrasonic1.getDistance();
		//	System.out.println(Ultrasonicvalue1);
			
				try {
					dos.writeInt(Ultrasonicvalue1);
					dos.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
			}
				Motor.A.setSpeed(0);
				Motor.B.setSpeed(0);
				Motor.A.backward();
				Motor.B.backward();
			}
		}
			}
}
Code. Robot send out ultrasonic value to Android (leJOS)

About CAVEDU 阿吉 - 雜工 (2521 Articles)

CAVEDU 教育團隊 打雜

Contact: Website

Leave a comment

Your email address will not be published.


*