Jump to content

[C/C++] Hilfe für den C++ Anfänger


Recommended Posts

Ich bin gerade dabei mich mit Tinkerforge und C++ vertraut zu machen. Der plan ist folgender: ich würde gerne die Temperatur messen, und bei Überschreitung eines gewissen Wertes ein Relais schalten. Für ersteres gibt es ja auch ein schönes Beispiel in der Dokumentation mit der Funktion temperature_set_temperature_callback_threshold, welches bei mir auch funktioniert. Meine Frage wäre jetzt: wie verknüpfe ich das Auslösen des callbacks mit dem schalten des Relais?

 

Viele Grüße,

 

Andreas 

Link to comment
Share on other sites

Schreibe dir doch einfach eine Funktion in der du das Relais setzt.

Jaja ich weiß. Klingt schön daher gesagt.

 

Also.

Rufe die Funktion aus dem Callback auf.

Eventuell kannst du ja das Relais als Parameter mit übergeben.

 

Wenn du ein Dual Relai Brickled hast zum Beispiel.

 


#include "bricklet_dual_relay.h"

#define RUID "xyz"  ID von deinem Brickled

void Switch( uint8_t relay,bool state) 
{
    DualRelay dr;
    dual_relay_create(&dr, RUID, &ipcon); 
    dual_relay_set_selected_state(&dr, relay, state) //Schalte Relais 
}

Funktionsaufruf einfach mit Switch(1,true) für ein oder Switch(1,false) für aus  in deinem Callback wenn der Temperaturvergleich erreicht ist;

Link to comment
Share on other sites

@Doc: Du hast vergessen das Relais der IPConnection hinzuzufügen.

Das war auch der Punkt wo ich mir nciht mehr sicher war, wie das in C++ hübsch gelöst wird.

 

Idealerweise willst du entweder IPConnection oder gleich das Bricklet im Callback referenzieren können (anlegen vorher in der main), aber wie geht das (sauber)? Globale variable?

Link to comment
Share on other sites

Hier mal einen Ausschnitt aus meinem Programm.

Da erfrage ich bei Programmstart alle Komponenten.

 

Mal erweitert um den Dualbricklet.

 

#include "ip_connection.h"
#include "bricklet_dual_relay.h"
#include "bricklet_temperature.h"

char*	Programmpath= NULL;
char*	HOST		= NULL;
int		PORT		= 4223; // Demon Standardport
int		Periode		= 100;

IPConnection*	ipcon = NULL;
Temperature*	Temp  = NULL;
DualRelay*	dual_relay = NULL;
Master*			master				= NULL;
IMU*			imu					= NULL;
GPS*			gps					= NULL;
AnalogIn*		ai_Speed			= NULL;
AnalogIn*		ai_Altitude			= NULL;
AnalogIn*		ai_Voltage			= NULL;
RotaryPoti*		Rotary				= NULL;
Temperature*	Temp				= NULL;
Barometer*		Baro				= NULL;
IO16*			Io16_Brick			= NULL;


void Create_Temperature(const char* device_uid, uint8_t device_identifier, const char *connected_uid,char position, bool is_new)
{
	/****************************************************************************/
	/*						Temperature Bricklet einbinden						*/
	/****************************************************************************/
	UID = (char *)malloc((strlen(device_uid)) * sizeof(char));
	size_t n =	 (strlen(device_uid)) * sizeof(char);
	if(UID)
	{
		_strset( UID, 0 ); 
		strncpy_s(UID,strlen(device_uid),  device_uid, strlen(device_uid)-1);
		/****************************************************************************/
		/*						Temperature Bricklet erstellen						*/
		/****************************************************************************/
		if(!Temp)
		{
			Temp = new Temperature();
		}
		temperature_create(Temp, device_uid,ipcon); 

		/****************************************************************************/
		/*			Temperature Bricklet zur IP Connection hinzufügen				*/
		/****************************************************************************/
			int16_t temperature;
			if(temperature_get_temperature(Temp, &temperature) > -1)
			{
				window->Set_Temperature(temperature);
		    }
			/****************************************************************************/
			/*					Ausgabegeschwindigkeit einstellen in ms					*/
			/****************************************************************************/
//			printf("Periode %d\n",Periode);
			temperature_set_temperature_callback_period(Temp, Periode);
			/****************************************************************************/
			/*					Temperature Bricklet Callback erstellen					*/
			/****************************************************************************/
			temperature_register_callback(Temp,TEMPERATURE_CALLBACK_TEMPERATURE	, cb_temperature,Temp);
		}

}

void Create_DUAL_RELAY(const char* device_uid, uint8_t device_identifier, const char *connected_uid,char position, bool is_new)
{
	/****************************************************************************/
	/*						Dual Relay Bricklet einbinden						*/
	/****************************************************************************/
	UID = (char *)malloc((strlen(device_uid)) * sizeof(char));
	size_t n =	 (strlen(device_uid)) * sizeof(char);
	if(UID)
	{
		_strset( UID, 0 ); 
		strncpy_s(UID,strlen(device_uid),  device_uid, strlen(device_uid)-1);
		/****************************************************************************/
		/*						Dual Relay Bricklet erstellen						*/
		/****************************************************************************/
		if(!dual_relay)
		{
			dual_relay= new DualRelay();
		}
		temperature_create(dual_relay, device_uid,ipcon); 

		/****************************************************************************/
		/*			Dual Relay Bricklet zur IP Connection hinzufügen				*/
		/****************************************************************************/
			/****************************************************************************/
			/*					Ausgabegeschwindigkeit einstellen in ms					*/
			/****************************************************************************/
//			printf("Periode %d\n",Periode);
			temperature_set_temperature_callback_period(Temp, Periode);
			/****************************************************************************/
			/*					Temperature Bricklet Callback erstellen					*/
			/****************************************************************************/
			temperature_register_callback(Temp,TEMPERATURE_CALLBACK_TEMPERATURE	, cb_temperature,Temp);
		}

}

void cb_enumerate(const char *device_uid, const char *connected_uid,char position,uint8_t hardware_version[3],uint8_t firmware_version[3],uint16_t device_identifier,uint8_t enumeration_type,void *user_data)
{
/****************************************************************************/
/*				Antwort auf	Komponentenabfrage (Bricks,Bricklets) 			*/
/****************************************************************************/
//	printf("device_uid: %s\tdevice_name: %s\tdevice_stack_id: %d\t is_new: %d\n\n", device_uid, device_name, device_stack_id,is_new);


if(device_identifier==MASTER_DEVICE_IDENTIFIER)
{
//		printf("Temperature gefunden\n");
//		Create_Master(device_uid,device_identifier, connected_uid, position,true);
}
if(device_identifier==IMU_DEVICE_IDENTIFIER)
{
//		printf("IMU gefunden\n");
//		Create_IMU(device_uid,device_identifier, connected_uid, position,true);
}
if(device_identifier==ANALOG_IN_DEVICE_IDENTIFIER)
{
//		printf("IMU gefunden\n");
//		Create_AnalogIn(device_uid,device_identifier, connected_uid, position,true);
}

if(device_identifier==ROTARY_POTI_DEVICE_IDENTIFIER)
{
//		printf("Rotary gefunden\n");
//		Create_RotaryPoti(device_uid,device_identifier, connected_uid, position,true);
}

if(device_identifier==TEMPERATURE_DEVICE_IDENTIFIER)
{
//		printf("Temperature gefunden\n");
	Create_Temperature(device_uid,device_identifier, connected_uid, position,true);
}

if(device_identifier==BAROMETER_DEVICE_IDENTIFIER)
{
//		printf("Barometer gefunden\n");
//		Create_Barometer(device_uid,device_identifier, connected_uid, position,true);
}

if(device_identifier==IO16_DEVICE_IDENTIFIER)
{
//		printf("Temperature gefunden\n");
//		Create_IO16(device_uid,device_identifier, connected_uid, position,true);
}
if(device_identifier==GPS_DEVICE_IDENTIFIER)
{
//		printf("Temperature gefunden\n");
//		Create_GPS(device_uid,device_identifier, connected_uid, position,true);
}
if(device_identifier==DUAL_RELAY_DEVICE_IDENTIFIER)
{
//		printf("Dual Relay gefunden\n");
	Create_DUAL_RELAY(device_uid,device_identifier, connected_uid, position,true);
}

}



int main(int argc, char *argv[])
{
Programmpath = (char *)malloc((MAX_STRING) * sizeof(char));
strcpy(Programmpath, argv[0]);

HOST = (char *)malloc((MAX_STRING) * sizeof(char));
strcpy(HOST, "localhost");


    if( argc > 1 )
    {
	for( int i = 1; i < argc; i++ )
	{
		_strlwr( argv[i] ); 
		unsigned char* Test1;
		Test1 = (unsigned char*)argv[i];
		unsigned char* Test2 = (unsigned char*)"?";
		if(_mbsnbcmp(Test1,Test2,1)==0)
		{
			fprintf(stderr, "\nTest.exe -Host Hostname -Port XXX -Periode XXX\"\n\n\t-Host Hostname\tIP or computername on that of the BRICK Demaon runs ( Default localhost )\n\t-Port XXX\tPort with that of the BRICK Demaon runs ( Default 4223 )\n\t-Periode XXX\tCycle period ( Default 100 )\n");
//				exit( 1 );
		}

		Test2 = (unsigned char*)"-host";
		if(_mbsnbcmp(Test1,Test2,5)==0)
		{
			strcpy(HOST, argv[i+1]);
		}
		Test2 = (unsigned char*)"-port";
		if(_mbsnbcmp(Test1,Test2,5)==0)
		{
			PORT = atoi(argv[i+1]);
		}
		Test2 = (unsigned char*)"-period";
		if(_mbsnbcmp(Test1,Test2,5)==0)
		{
			Periode = atoi(argv[i+1]);
		}
	}
}
if(!ipcon)
{
	ipcon = new IPConnection();
}
ipcon_create(ipcon);
if(ipcon)
{
	if(ipcon_connect(ipcon, HOST, PORT) < 0) 
	{
		fprintf(stderr, "Could not create connection\n");
	}
//		exit(1);
}

ipcon_register_callback(ipcon,
                            IPCON_CALLBACK_ENUMERATE,
                            (void *)cb_enumerate,
                            NULL);
ipcon_enumerate(ipcon);

}

Link to comment
Share on other sites

Ich habe das jetzt so versucht, beim auslösen des Callbacks bekomme ich aber einen Ausnahmefehler beim Zugriff auf eine Speicheradresse:

 

#include "stdafx.h"
#include "ip_connection.h"
#include "bricklet_temperature.h"
#include "bricklet_dual_relay.h"

#define HOST "localhost"
#define PORT 4223
#define UID "6Hs" // Change to your UID
#define UID_Relay "76b"

// Callback for temperature greater than 25 °C
void cb_reached(int16_t temperature, void *user_data) {
    DualRelay *dr = (DualRelay *)user_data;

       printf("Temperatur: %f °C.\n", temperature/100.0);
       dual_relay_set_state(dr, true, false);

}


int main() {
    // Create IP connection
    IPConnection ipcon;
    ipcon_create(&ipcon);

    // Create device object for Temperature
    Temperature t;
    temperature_create(&t, UID, &ipcon); 

// Create device object for Dual Relay
DualRelay dr;
    dual_relay_create(&dr, UID_Relay, &ipcon);


    // Connect to brickd
    if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
        fprintf(stderr, "Could not connect\n");
        exit(1);
    }
    // Don't use device before ipcon is connected
    //
    // Get threshold callbacks with a debounce time of 10 seconds (10000ms)
    temperature_set_debounce_period(&t, 10000);

    // Register threshold reached callback to function cb_reached
    temperature_register_callback(&t,
                                  TEMPERATURE_CALLBACK_TEMPERATURE_REACHED,
                                  (void *)cb_reached,
                                  NULL);

    // Configure threshold for "greater than 25 °C" (unit is °C/100)
    temperature_set_temperature_callback_threshold(&t, '>', 25*100, 0);

    printf("Press key to exit\n");
    getchar();
    ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally
}

Link to comment
Share on other sites

FlyingDoc, warum schreibst du statt

 

		UID = (char *)malloc((strlen(device_uid)) * sizeof(char));
	size_t n =	 (strlen(device_uid)) * sizeof(char);
	if(UID)
	{
		_strset( UID, 0 ); 
		strncpy_s(UID,strlen(device_uid),  device_uid, strlen(device_uid)-1);

 

nicht einfach

 

		UID = strdup(device_uid);
	if (UID) {

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...