Class: SpheroClient

Inherits:
Object
  • Object
show all
Includes:
SpheroUtilities
Defined in:
lib/rubysphero.rb

Overview

mod

Constant Summary collapse

COMMS_RETRY =
5
COLOURS =
{ 	:blue 	=> 	[0x00,0x00,0xFF],
:green 	=> 	[0x00,0xFF,0x00],
:red	=>	[0xFF,0x00,0x00],
:orange	=>	[0xFF,0x8C,0x00],
:yellow	=>	[0xFF,0xFF,0x00],
:white	=>	[0xFF,0xFF,0xFF],
:black	=>	[0x00,0x00,0x00],
:pink	=>	[0xFF,0x00,0xFF],
:grey	=>	[0x80,0x80,0x80]}

Instance Method Summary collapse

Methods included from SpheroUtilities

#add_checksum, #do_checksum, #logd, #print_format_bytes

Constructor Details

#initialize(bluetooth_address) ⇒ SpheroClient

def



216
217
218
219
# File 'lib/rubysphero.rb', line 216

def initialize(bluetooth_address)
	@sequence_val=0 
	return open(bluetooth_address)
end

Instance Method Details

#closeObject

def



230
231
232
# File 'lib/rubysphero.rb', line 230

def close
	@connection.close
end

#get_sequenceObject

def



207
208
209
210
211
212
213
214
# File 'lib/rubysphero.rb', line 207

def get_sequence
	@sequence_val+=1
	if @sequence_val > 255
		@sequence_val=0 
	end # if
	logd("Getting seq: #{@sequence_val}")
	return @sequence_val
end

#open(bluetooth_address) ⇒ Object

class



221
222
223
224
225
226
227
228
# File 'lib/rubysphero.rb', line 221

def open(bluetooth_address)
	begin
		@connection   = Serial.new bluetooth_address, 115200 ,8
	rescue RubySerial::Exception
		open bluetooth_address
	end
	return @connection
end

#orientationObject

def



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rubysphero.rb', line 97

def orientation
		
	set_back_led_output(255)
	set_colour(:black)
	1.step(720,5) do | heading | 	
		roll(heading , 0)
		
		inputted_text=gets.chomp
		
		if inputted_text =="" then
			# spin
		elsif inputted_text==" "			
			set_back_led_output(0)
			set_heading(heading)
			set_colour(:white)
			return true
		else 
			sleep 1	
		end # if 
	end # upto 	
	return false
end

#pingObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rubysphero.rb', line 57

def ping
	logd()
	logd "Building request: ping"
	request=SpheroRequest.new(:synchronous)

	request.did=0x00 
	request.cid=0x01 # Ping
	request.seq=get_sequence
	request.dlen=0x01
	
	return send_and_check(request)
end

#read_data(length) ⇒ Object

def



89
90
91
92
93
94
95
# File 'lib/rubysphero.rb', line 89

def read_data(length)
	bytes=@connection.read(length).unpack("C*")
	logd("Wire read finished.")
	response = SpheroResponse.new( bytes)

	return response
end

#roll(heading_raw = 0, speed = 0xFF) ⇒ Object

def



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/rubysphero.rb', line 184

def roll(heading_raw=0, speed=0xFF)
	logd()
	logd( "Building request: roll")

	request=SpheroRequest.new(:synchronous)
	heading = heading_raw%359
	logd( "Heading: #{heading}")

	request.did=0x02 
	request.cid=0x30 # Roll
	request.seq=get_sequence
	request.dlen=0x05

	state=0x01
	
	request.push_data speed
	request.push_data(heading , :a16bit)
	request.push_data state
	
	return send_and_check(request)	

end

#send_and_check(request) ⇒ Object

def



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rubysphero.rb', line 70

def send_and_check(request)
	send_data(request.build_packet)

	COMMS_RETRY.times do
		response = read_data(request.length)
		if request.seq == response.echoed_seq then
			logd("Sent and received Sequences MATCH.")	
			return true
		end # if 
		logd("Sequences do not match. Sent:#{request.seq} Rec:#{response.echoed_seq} ")
	end # times
	return false
end

#send_data(data) ⇒ Object

data



84
85
86
87
# File 'lib/rubysphero.rb', line 84

def send_data(data)
	logd("Wire send next.")
	@connection.write data
end

#set_back_led_output(brightness) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rubysphero.rb', line 121

def set_back_led_output(brightness)
	logd()
	logd "Building request: set back led output b"
	
	request=SpheroRequest.new(:synchronous)

	request.did=0x02 
	request.cid=0x21 
	request.seq=get_sequence
	request.dlen=0x02
	request.push_data brightness 
	
	return send_and_check(request)	

end

#set_colour(chosen_colour) ⇒ Object

def



156
157
158
159
160
# File 'lib/rubysphero.rb', line 156

def set_colour(chosen_colour) 
	logd
	logd("Locating RGB values for: #{chosen_colour}")
	return set_colour_rgb(COLOURS[chosen_colour][0],COLOURS[chosen_colour][1],COLOURS[chosen_colour][2])
end

#set_colour_rgb(red_value, green_value, blue_value) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rubysphero.rb', line 163

def set_colour_rgb(red_value,green_value,blue_value)
	logd
	logd "Building request: colour"
	
	request=SpheroRequest.new(:synchronous)


	request.did=0x02 
	request.cid=0x20 # Set RGB Output
	request.seq=get_sequence
	request.dlen=0x05
	request.push_data red_value 
	request.push_data green_value
	request.push_data blue_value  
	flag=0x01
	request.push_data flag 
	
	return send_and_check(request)

end

#set_heading(heading_raw) ⇒ Object

def



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rubysphero.rb', line 137

def set_heading(heading_raw) 
	logd
	logd( "Building request: Set Heading")

	request=SpheroRequest.new(:synchronous)
	heading = heading_raw%359
	logd( "Heading: #{heading}")

	request.did=0x02 
	request.cid=0x01
	request.seq=get_sequence
	request.dlen=0x03
	
	request.push_data(heading , :a16bit)
	
	return send_and_check(request)	

end