You Are Here:

Community: Developer Discussion Boards

#1 Old problem with thread - 2008-09-24, 13:22

Join Date: Jun 2008
Posts: 45
maclun123
Offline
Registered User
I have found few examples and looked through different problems on DiBo. And I still cant figure out what is wrong with my program.

Without threads my program works fine, but trying to implement threading it stops responding and after some time it either restarts or hangs with black screen.

Part of my code:
Code:
import ...
import thread

class Logger(object):
...
# GPS STUFF - INITIALIZATION, SWITCHING OFF and READING ###########################################
	def gps_on(self):
		self.printf('initiating GPS...')

		try:
			positioning.select_module(positioning.default_module())
			positioning.set_requestors([{"type":"service","format":"application","data":"gps_app"}])
			positioning.position(course = 1,satellites = 1,callback = self.gps, interval = 500000,partial = 0)
			e32.ao_sleep(3)
			self.printf(u'GPS initiated!')
			self.GPS_ON = True
		except:
			appuifw.note(u'Problem with GPS','error')
			
		self.update_debug_info()

	def gps_off(self):
		self.GPS_ON = False						#if exception, then GPS OFF as well. so no use to put this in between 'TRY'
		try:
			positioning.stop_position()
			self.printf('GPS turned OFF')
		except:
			appuifw.note(u'Problem with switching off GPS','error')
			
		self.update_debug_info()

# FOR READING GPS DATA
	def gps(self,event):
		self.gps_data = event
# ###################################################################################

# SENSORS STUFF - CONNECTING, DISCONNECTING and READING ##########################################
	def connect_sensors(self):
		connected_sensors = 0
		try:
			sensor_rot = sensor.sensors()['RotSensor']
			self.N95_sensor_rot = sensor.Sensor(sensor_rot['id'],sensor_rot['category'])
			self.N95_sensor_rot.set_event_filter(sensor.RotEventFilter())
			self.N95_sensor_rot.connect(self.update_orientation_sensor_data)
			self.printf('Sensor ROT connected!')
			connected_sensors += 1
		except:
			self.printf('couldn\'t connect ROT sensor!')
			
		try:
			sensor_acc = sensor.sensors()['AccSensor']
			self.N95_sensor_acc = sensor.Sensor(sensor_acc['id'], sensor_acc['category'])
			self.N95_sensor_acc.connect(self.update_axis_sensor_data)
			self.printf('Sensor ACC connected!')
			connected_sensors += 1
		except:
			self.printf('couldn\'t connect ACC sensor!')
	
		if (connected_sensors == 2):
			self.SENSORS_ON = True
		elif (connected_sensors == 1):
			self.printf('ONLY one sensor connected!')
			self.SENSORS_ON = True
		else:
			self.printf('No sensor connected!')

	def disconnect_sensors(self):
		if self.SENSORS_ON:
			self.SENSORS_ON = False
			try:
				self.N95_sensor_rot.disconnect()
			except:
				pass
			try:
				self.N95_sensor_acc.disconnect()
			except:
				pass
				
# FOR READING ACCELEROMETER DATA
	def update_orientation_sensor_data(self,status):
		if status == sensor.orientation.TOP:
			self.sensorState = u"BOTTOM"
		elif status == sensor.orientation.BOTTOM:
			self.sensorState = u"TOP"
		elif status == sensor.orientation.LEFT:
			self.sensorState = u"RIGHT"
		elif status == sensor.orientation.RIGHT:
			self.sensorState = u"LEFT"
		elif status == sensor.orientation.FRONT:
			self.sensorState = u"FRONT"
		elif status == sensor.orientation.BACK:
			self.sensorState = u"BACK"

	def update_axis_sensor_data(self,data):
		self.xyz = [data['data_1'], data['data_2'], data['data_3']]
#########################################
	def start_logging(self):
		if not self.is_running:
			if self.GPS_ON:
				self.is_running = True
			else:
				self.gps_on()
				self.is_running = True
			
			if not self.SENSORS_ON:
				self.connect_sensors()
			
			self.printf('logging...')
			app_lock.signal()
			
			self.update_debug_info()
#### HERE THREAD IS CCREATED
			thread.start_new_thread(self.log_thread, ())

	def stop_logging(self):
		if self.is_running:
			self.is_running = False
			self.printf('stoped logging.')
			app_lock.signal()
		else:
			self.printf('nothing to stop...')
			
		self.update_debug_info()

	def log_thread(self):
		lockTHR = thread.allocate_lock()
		while logger.is_running:
			lockTHR.acquire()
			e32.ao_sleep(1)
			self.printf(str(time.ctime(time.time())))
			self.write_log('')

			lockTHR.release()

	def write_log(self,pt):
		# GPS stuff
		sat = self.gps_data['satellites']['used_satellites']
		gpsTime = self.gps_data['satellites']['time']
		pos_lat = self.gps_data['position']['latitude']
		pos_long = self.gps_data['position']['longitude']
		speed = self.gps_data['course']['speed']
		
		# CAMERA status
		apps = appswitch.application_list(False)
		if (u'Camera' in apps):
			self.cameraState = u'ON'
		else:
			self.cameraState = u'OFF'

		gsm_loc = location.gsm_location()
		logTime = time.time()
		
		if self.short_log:
			log = str(logTime) + ';'
			try:
				log += str(gsm_loc[0]) + ';'
			except:
				log += u'N/A;'
			try:
				log += str(gsm_loc[3]) + ';'
			except:
				log += 'N/A;'

			log += u'' + self.sensorState + ';(' + str(self.xyz[0]) + ',' + str(self.xyz[1]) + ',' + str(self.xyz[2]) + ');'

			if self.GPS_ON:
				log += str(pos_lat) + ';' + str(pos_long) + ';' + str(speed) + ';'
			else:
				log += '0;0;0;'
			log += self.cameraState 
			log += pt + '\r\n'
		else:
			log = str(time.ctime(logTime)) + '; ' + str(logTime) + '; '
			try:
				log += u'Country:' + str(gsm_loc[0]) + '; cellID:'
			except:
				log += u'Country:N/A; cellID:'
			try:
				log += str(gsm_loc[3]) + '; '
			except:
				log += 'N/A; '

			log += u'' + self.sensorState + '; (' + str(self.xyz[0]) + ',' + str(self.xyz[1]) + ',' + str(self.xyz[2]) + '); '

			if self.GPS_ON:
				log += u'GPS: ' + str(sat) + '; ' + str(pos_lat) + '; ' + str(pos_long) + '; ' + str(speed) + '; ' 
			else:
				log += 'GPS: 0; 0; 0; '
			
			log += u'Camera: ' + self.cameraState 
			log += pt + '\r\n'
		
		if self.showLog:
			self.printf(log)
		
		if self.is_running:				#to protect from writing after pushing exit key
			try:
				f = open(u'e:\\Python\\log\\' + self.log_file + '.txt','a')
				f.writelines(log)
				f.close()
			except:
				self.printf(str(time.ctime(logTime)) + u"\nCannot save logto file: " + self.log_file)
...
####END OF CLASS####
global mainLoop, logging
mainLoop = True
logger = Logger()
####################
canvas.bind(key_codes.EKey1, logger.update_debug_info)
canvas.bind(key_codes.EKey2, logger.display_debug_info)
canvas.bind(key_codes.EKey4, logger.turn_on_viewfinder)
canvas.bind(key_codes.EKey5, logger.turn_off_viewfinder)

while mainLoop:
	app_lock = e32.Ao_lock()
	logger.setMenu()
	app_lock.wait()
When I remove:
Code:
self.write_log('')
from Thread, then program is responsive.

Generally all methods are in the Logger class - for GPS and Accelerometer, including callbacks.

Complete file can be found here: http://www.student.dtu.dk/~s053985/files/
Reply With Quote

#2 Old Re: problem with thread - 2008-09-24, 19:41

Join Date: Jan 2004
Posts: 368
Location: Helsinki
Send a message via Skype™ to miohtama
miohtama's Avatar
miohtama
Offline
Regular Contributor
GPS and other Symbian server based stuff can be only called from the thread they are created in (Symbian OS limitation).

Also, unless it has been fixed, positioning module only works with the main thread.

Does this help?

-Mikko


Mikko Ohtamaa
Twinapex Research
http://www.twinapex.com
Reply With Quote

#3 Old Re: problem with thread - 2008-09-24, 23:41

Join Date: Jun 2008
Posts: 45
maclun123
Offline
Registered User
Ok, if I understood correctly, it is not possible to make GPS reading(writing data to file from variables) in the thread, right? (at least with positioning module - is there other easy way of using built-in GPS in N95?)

What about accelerometer? Are there any restrictions in regards to threading? I am using 'sensor' module.

Thanks Mikko for response.

mac
Last edited by maclun123 : 2008-09-25 at 00:19.
Reply With Quote

#4 Old Re: problem with thread - 2008-09-25, 23:18

Join Date: Jan 2004
Posts: 368
Location: Helsinki
Send a message via Skype™ to miohtama
miohtama's Avatar
miohtama
Offline
Regular Contributor
Quote:
Originally Posted by maclun123 View Post
What about accelerometer? Are there any restrictions in regards to threading? I am using 'sensor' module.
You cannot share any native Symbian stuff between threads: files, services, sockets, etc. due to operating system restrictions.


Mikko Ohtamaa
Twinapex Research
http://www.twinapex.com
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help with thread gigglie General Symbian C++ 0 2008-06-20 07:39
Multi Thread problem Segev General Symbian C++ 4 2007-05-03 20:38
Thread Problem on device luoyingzhou General Symbian C++ 8 2007-04-23 09:35
Problem Accessing Database within a Thread function MohammadHendy General Symbian C++ 0 2005-12-15 15:18
Problem with the Nokia 6600, Bluetooth and Monty Thread tenfourty Mobile Java General 15 2005-09-30 15:15

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia