×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    45

    problem with thread

    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/

  2. #2
    Regular Contributor
    Join Date
    Jan 2004
    Location
    Helsinki
    Posts
    376

    Re: problem with thread

    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

    http://mfabrik.com
    http://blog.mfabrik.com

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    45

    Re: problem with thread

    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-24 at 23:19.

  4. #4
    Regular Contributor
    Join Date
    Jan 2004
    Location
    Helsinki
    Posts
    376

    Re: problem with thread

    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

    http://mfabrik.com
    http://blog.mfabrik.com

Similar Threads

  1. Need Help with thread
    By gigglie in forum Symbian
    Replies: 0
    Last Post: 2008-06-20, 06:39
  2. Multi Thread problem
    By Segev in forum Symbian
    Replies: 4
    Last Post: 2007-05-03, 19:38
  3. Thread Problem on device
    By luoyingzhou in forum Symbian
    Replies: 8
    Last Post: 2007-04-23, 08:35
  4. Problem Accessing Database within a Thread function
    By MohammadHendy in forum Symbian
    Replies: 0
    Last Post: 2005-12-15, 14:18
  5. Problem with the Nokia 6600, Bluetooth and Monty Thread
    By tenfourty in forum Mobile Java General
    Replies: 15
    Last Post: 2005-09-30, 14:15

Posting Permissions

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