Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    [c++] Accedere ad una variabile

    Non sono per niente esperto in c++ ma mi serverebbe fare una elaborazione di una variabile ma non riesco ad accederci.
    Il programma accede ad una periferia usb dove è attaccato un accelerometro:

    codice:
    #include "stdafx.h"
    #include "..\..\..\phidget21.h"
    
    double past_samples[3];
    
    //callback that will run if the Accelerometer is attached to the computer
    int __stdcall AttachHandler(CPhidgetHandle IFK, void *userptr)
    {
    	int serialNo;
    	//questa funzione immette in serialNO il numero di serie dello strumento collegato
    	CPhidget_getSerialNumber(IFK, &serialNo);
    	printf("Accelerometer %10d attached!\n\n", serialNo);
    
    	return 0;
    }
    
    //callback that will run if the Accelerometer is detached from the computer
    int __stdcall DetachHandler(CPhidgetHandle IFK, void *userptr)
    {
    	int serialNo;
    	CPhidget_getSerialNumber(IFK, &serialNo);
    	printf("Accelerometer %10d detached! \n", serialNo);
    
    	return 0;
    }
    
    //callback that will run if the Accelerometer generates an error
    int __stdcall ErrorHandler(CPhidgetHandle IFK, void *userptr, int ErrorCode, const char *unknown)
    {
    	printf("Error handled. %d - %s \n", ErrorCode, unknown);
    	return 0;
    }
    
    //callback that will run if the acceleration changes by more than the Acceleration trigger.
    //Index - Index of the axis that is generating the event, Value - the value read by the accelerometer axis
    int __stdcall accel_AccelChangeHandler(CPhidgetAccelerometerHandle WGT, void *userptr, int Index, double Value)
    {
    	printf("Axis: %i -- %6f\n", Index, Value);
    
    	return 0;
    }
    
    //Display the properties of the attached phidget to the screen.  
    //We will be displaying the name, serial number, version of the attached device and number of Axes on the accelerometer.
    int display_properties(CPhidgetHandle phid)
    {
    	int serialNo, version;
    	const char* ptr;
    	int numAxes;
    	
    	//Ritorna il tipo di dispositivo
    	CPhidget_getDeviceType(phid, &ptr);
    	//Ritorna il numero di serie
    	CPhidget_getSerialNumber(phid, &serialNo);
    	//Ritorna la versione
    	CPhidget_getDeviceVersion(phid, &version);
    	//Ritorna il numero di assi
    	CPhidgetAccelerometer_getAxisCount((CPhidgetAccelerometerHandle)phid, &numAxes);
    
    	printf("%s\n", ptr);
    	printf("Serial Number: %10d\nVersion: %8d\n", serialNo, version);
    	printf("Number of Axes: %i\n", numAxes);
    		
    	return 0;
    }
    
    int accelerometer_simple()
    {
    	int result, numAxes;
    	const char *err;
    
    	//Declare an accelerometer handle
    	CPhidgetAccelerometerHandle accel = 0;
    
    	//create the accelerometer object
    	//Mette nell'indirizzo di memoria di accel l'accelerometro collegato
    	CPhidgetAccelerometer_create(&accel);
    
    	//Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
    	CPhidget_set_OnAttach_Handler((CPhidgetHandle)accel, AttachHandler, NULL);
    	CPhidget_set_OnDetach_Handler((CPhidgetHandle)accel, DetachHandler, NULL);
    	CPhidget_set_OnError_Handler((CPhidgetHandle)accel, ErrorHandler, NULL);
    	
    	//open the acclerometer for device connections
    	//Impostando -1 apre il primo dispositivo disponibile
    	CPhidget_open((CPhidgetHandle)accel, -1);
    
    	//get the program to wait for an accelerometer device to be attached
    	printf("Waiting for accelerometer to be attached.... \n\n");
    
    	//Aspetta 10 secondi che venga connesso il dispositivo
    	if((result = CPhidget_waitForAttachment((CPhidgetHandle)accel, 10000)))
    	{
    		CPhidget_getErrorDescription(result, &err);
    		printf("Problem waiting for attachment: %s\n", err);
    
    		//Interruzione, spingere invio per continuare
    		getchar();
    		return 0;
    	}
    
    	//Display the properties of the attached accelerometer device
    	display_properties((CPhidgetHandle)accel);
    
    	//read accelerometer event data
    	printf("Reading.....\n\n");
    
    	//get the number of available axes on the attached accelerometer
    	CPhidgetAccelerometer_getAxisCount(accel, &numAxes);
    
    	//Most accelerometers have 2 axes so we'll pre-set their sensitivity to make the data more readable
    	//Il primo valore è il dispositivo, il secondo l'indice degli assi e il terzo è la sensibilità
    	CPhidgetAccelerometer_setAccelerationChangeTrigger(accel, 0, 0.500);
    	CPhidgetAccelerometer_setAccelerationChangeTrigger(accel, 1, 0.500);
    
    	//If the accelerometer attached is a 3-axis, we'll set the sensitivity of the 3rd axis
    	if(numAxes > 2)
    	{
    		CPhidgetAccelerometer_setAccelerationChangeTrigger(accel, 2, 0.500);
    	}
    
    	//Registers a callback that will run if the acceleration changes by more than the Acceleration trigger.
    	//Requires the handle for the Accelerometer, the function that will be called, 
    	//and an arbitrary pointer that will be supplied to the callback function (may be NULL)
    	CPhidgetAccelerometer_set_OnAccelerationChange_Handler(accel, accel_AccelChangeHandler, NULL);
    
    	//Interruzione
    	getchar();
    
    	//wait until user input is read
    	printf("Press any key to end\n");
    
    	//since user input has been read, this is a signal to terminate the program so we will close the phidget and delete the object we created
    	printf("Closing...\n");
    	CPhidget_close((CPhidgetHandle)accel);
    	CPhidget_delete((CPhidgetHandle)accel);
    
    	//Interruzione
    	getchar();
    
    	//all done, exit
    	return 0;
    }
    
    //main entry point to the program
    int _tmain(int argc, _TCHAR* argv[])
    {
    	accelerometer_simple();
    	return 0;
    }
    Qui c'è il file .pdf dove sono spiegate le API (http://www.phidgets.com/downloads.php?example_id=17)

    Praticamente sto cercando di accedere a Value dove credo vengano immagazzinati i dati, ma non ci riesco

  2. #2
    In rosso ho segnato la funzione di interesse.

  3. #3
    è possibile per esempio aprire in un progetto due file .cpp e spostare i dati contenuti in una variabile da un sorgente all'altro?

  4. #4
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,464
    Per accedere alla variabile di cui parli nella prima richiesta, lo fai esattamente come hai scritto (all'interno della funzione ...).

    La questione dei due sorgenti non l'ho capita. Forse pensi possa essere la soluzione ad un problema che non e' ancora chiaro e che probabilmente ha un'altra soluzione ....
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  5. #5
    Allora ho un altro progetto in c++ che prende dei dati li elabora e ne tira fuori un valore. Questi dati li prende leggendo un foglio .txt, invece dovrebbe prenderli direttamente dall'acelerometro. Quindi devo prendere i dati dall'accelerometro e farli lavorare dall'altro programma.
    Ora che faccio elaboro i programmi e ne tiro fuori solo uno? Come?
    Unisco i due programmi in qualche modo? Come?

    PS: un altro problema è che i dati di cui parlo sarebbero uno l'accelerazione e l'altro il tempo, ma non so come prendere il tempo, esiste una libreria che segna il tempo?

  6. #6
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,304

    Moderazione

    Regolamento, punto 6:

    Formattazione del codice sorgente.
    Quando incollate una porzione di codice all'interno di un messaggio, utilizzate l'apposito tag

    [CODE]
    codice
    [/CODE]

    che può essere inserito automaticamente premendo il pulsante CODE: questo renderà il codice leggibile e facilmente comprensibile a tutti, agevolando gli utenti che desiderano aiutarvi.
    Sistemo io.


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  7. #7
    grazie e scusa dell'errore

  8. #8
    riporto solo il codice essenziale:
    codice:
    #include "stdafx.h"
    #include "phidget21.h"
    
    //callback that will run if the acceleration changes by more than the Acceleration trigger.
    //Index - Index of the axis that is generating the event, Value - the value read by the accelerometer axis
    
    int __stdcall accel_AccelChangeHandler(CPhidgetAccelerometerHandle WGT, void *userptr, int Index, double Value)
    {
    	printf("Axis: %i   %6f\n", Index, Value);
        
    	return 0;
    }
    
    //main entry point to the program
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int result;
    	const char *err;
    
    	//Declare an accelerometer handle
    	CPhidgetAccelerometerHandle accel = 0;
       
    	//create the accelerometer object
    	CPhidgetAccelerometer_create(&accel);
    
    	//Impostando -1 apre il primo dispositivo disponibile
    	CPhidget_open((CPhidgetHandle)accel, -1);
    
    	//get the program to wait for an accelerometer device to be attached
    	printf("Waiting for accelerometer to be attached.... \n\n");
    
    	//Aspetta 10 secondi che venga connesso il dispositivo
    	if((result = CPhidget_waitForAttachment((CPhidgetHandle)accel, 10000)))
    		{	CPhidget_getErrorDescription(result, &err);
    			printf("Problem waiting for attachment: %s\n", err);
    			//Interruzione, spingere invio per continuare
    			getchar();
    			return 0;}
    
    	//Imposta la sensibilità
    	CPhidgetAccelerometer_setAccelerationChangeTrigger(accel, 0, 0.500);
    	CPhidgetAccelerometer_setAccelerationChangeTrigger(accel, 1, 0.500);
    	CPhidgetAccelerometer_setAccelerationChangeTrigger(accel, 2, 0.500);
    
    	//Registra un evento se l'accelerazione cambia oltre una certa soglia.
    	CPhidgetAccelerometer_set_OnAccelerationChange_Handler(accel, accel_AccelChangeHandler, NULL);
    
    	//Interruzione
    	getchar(); 
    
    	//Chiude il dispositivo e l'oggetto
    	printf("Closing...\n");
    	CPhidget_close((CPhidgetHandle)accel);
    	CPhidget_delete((CPhidgetHandle)accel);
    
    	//Interruzione
    	getchar();
    
    	return 0;
    }
    Il problema è che una volta che entra nella funzione accel_AccelChangeHandler non esce più e aspetta sempre una variazione di accelerazione per stamparla a video, e l'unico modo che ho di uscire da quella funzione è con il getchar() rosso.
    Inizialmente avevo pensato di usare fstream e salvare i dati su un file e infatti ci riesco, ma io devo accedere in real time ai dati e lavorarci subito e quindi non è il metodo migliore.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.