Example #1
0
static void* run_master(void * arg) {
	
	ni = init_ec((char*)arg);
	init_slave_db();
	if(ni != 0) {
		printf("Attach netif \n\n");
		EtherCAT_DataLinkLayer::instance()->attach(ni);
		printf("Master initializing \n\n");
		EtherCAT_Master * EM = EtherCAT_Master::instance();
		printf("Getting slave handler\n");
		EtherCAT_SlaveHandler * sh_ek1100 = EM->get_slave_handler(0x03e9);
		//sleep(1);
		printf("Setting EK1100 to OP STATE\n");
		if(sh_ek1100->to_state(EC_OP_STATE))
			printf("EK1100 succesfully set in OP STATE\n");
		else
			printf("\nfailed to set EK1100 in OP STATE\n");
		printf("Getting slave handler\n");
		EtherCAT_SlaveHandler * sh_el4102 = EM->get_slave_handler(0x03ea);
		printf("Setting EL4102 to OP STATE\n");
		if(sh_el4102->to_state(EC_OP_STATE))
			printf("EL4102 succesfully set in OP STATE\n");
		else
			printf("\nfailed to set EL4102 in OP STATE!!\n");
		
		printf("AL initializing \n\n");
		EtherCAT_AL * AL = EtherCAT_AL::instance();
		
		///Set Channel 1 to 5V
		unsigned char msg[2] = {0xff, 0x3f};
		if(AL->isReady()) {
			printf("Test: Set Channel 1 to 5V: \n\n");
			int count = 0;
			while(count<100000) {
				EM->txandrx_PD(sizeof(msg),msg);
				count++;
			}
			printf("Test done.\n");
		}
		
		close_socket(ni);
		
	}
}
Example #2
0
		void EthercatDemoIO::update() {
			EtherCAT_Master *EM = EtherCAT_Master::instance();
			//printf("Got master\n");
			unsigned char msg[10] = {0};		//Data to Send
			int samples = (int)(1000.0 / this->engine()->getActivity()->getPeriod() / sinus_freq);
			if(cnt > samples)
				cnt = 0;
			time = M_PI * 2 * cnt / samples;
			voltage_f = (sin(time) * 5 + 5) * 32767 / 10;
			voltage_i = (int) voltage_f;
			msg[0] = voltage_i;	msg[1] = voltage_i>>8;
			//msg[4] = cnt_dig & 0x01;
			//printf("mdg[4] = %d\n",msg[4]);
			//printf("samples = %d cnt = %d   time = %f    voltage = %f  msg[0] =0x%x     msg[1] = 0cx%x \n", samples,cnt,time, voltage_f,msg[0], msg[1]);							// Digital Output Channel 1
			cnt++;	cnt_dig++;
			EM->txandrx_PD(sizeof(msg),msg);
			int volt = msg[6];
			volt = volt<<8;
			volt = volt | msg[5];

			//printf("voltage at channel0: 0x%x	0x%x	%d\n",msg[5],msg[6], volt);
		}