Esempio n. 1
0
Power::Power(QWidget *parent)
    : QWidget(parent)
{
    /*setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint
               | Qt::WindowCloseButtonHint);
    setAttribute(Qt::WA_TranslucentBackground);*/

    checked = false;
    mPress = false;
    hover = false;
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(timerout()),Qt::DirectConnection);
    //按钮尺寸自己调
    setFixedSize(60,30);
    framed = 6;//阴影框与按钮框的间距
    resizePainter();

   //字体设置和文字设置 自己DIY
    QFont font1 = this->font();
    font1.setBold(true);
    font1.setPointSize(13);
    setFont(font1);
    setText();
    setspeed(50);//设置滑动速度
}
Esempio n. 2
0
int main(void)
{
	PORTG=255;
	_delay_ms(2000);
	
	#define F_CPU 16000000UL
	unsigned char temp;
	//Init_LED();  
	uart_1_ini();
	
	while (temp=='P')
	{
	temp=USART1_Recieve();	
	}
	temp=0;
	while (temp<5)
	{
		uart_1_Transmit('o');
	temp++;
	 }
	
	
		
		UCSR1B=((1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1));
	PORTG=0;	
	TWI_init_master(); 
	//kalmaninit();
	PORTG=255;
	uart_1_Transmit('B');
	_delay_ms(200);     
	cli();                //Initialize Gyro
	InitHMC();
	PORTG=0;    //Enable Global Interrupt
	DDRA=0;
	DDRD=0b00001011;
	PORTA=255;
	DDRF=0;
	//ADCSRA=(1<<ADEN);//|(1<<ADFR);	
	_delay_ms(5);	
	cleargyro(); 
	zero_error(); 	
	sei();
	
	PORTG=0;
	//uart_1_Transmit('C');

	//uart_1_Transmit('L');	
    //init_timer();
	//_delay_ms(500);
	
	/*_delay_ms(2000);
	
	setspeed(front,100);
	setspeed(left,0);
	setspeed(right,0);
	setspeed(back,0);
	
	_delay_ms(500);
	
	setspeed(front,0);
	setspeed(left,100);
	setspeed(right,0);
	setspeed(back,0);
	
	_delay_ms(1000);
	
	setspeed(front,0);
	setspeed(left,0);
	setspeed(right,100);
	setspeed(back,0);
	
	_delay_ms(1000);
	
	setspeed(front,0);
	setspeed(left,0);
	setspeed(right,0);
	setspeed(back,100);
	
	_delay_ms(2000);
	
	setspeed(front,0);
	setspeed(left,0);
	setspeed(right,0);
	setspeed(back,0);
	*/
	//OCR0=18;
	int i,countera=0;
	
	
	//
	//while(1)
	//{
			//PORTG=255;
			//_delay_ms(1000);
		//	PORTG=0;
			//_delay_ms(1000);
	//}
	while(1)
	{
		
	
		
		/*	  PORTG=!(PORTG);
				frontmotor=ontime_1;
				backmotor=ontime_1;
				rightmotor=ontime_1;
				leftmotor=ontime_1;
		*/
				
		 //if (X_angle>4 || X_angle < (-4))
		//{
		//
		//leftmotor=leftmotor-Y_angle*0.3;
		//rightmotor=rightmotor+Y_angle*0.3;
		//} 
		
		
		
				if(X_angle>X_angle_prev)
				X_error=X_angle-X_angle_prev;
				else
				X_error=X_angle_prev-X_angle;
				
				X_integ =X_integ+Xangle;
				if (X_integ>=5000)
					X_integ=5000;
				if (X_integ<=-5000)
				X_integ=-5000;
				countera++;
				
				
		if (X_angle>2)
		{
		float kp=0.8;
		float kd=0.6;
		float ki=0.005;
		leftmotor=ontime_1+kp*X_angle+kd*X_error+ki*X_integ;
		if(leftmotor<0)
			leftmotor=0;
		rightmotor=ontime_1-kp*X_angle-kd*X_error-ki*X_integ;	
		if(rightmotor<0)
			rightmotor=0;
		X_angle_prev=X_angle;
		}
		else if (X_angle<-2)
		{
		float kp=0.8;
		float kd=0.6;
		float ki=0.005;  // 0.0012
		leftmotor=ontime_1+kp*X_angle-kd*X_error+ki*X_integ;
		if(leftmotor<0)
			leftmotor=0;
		rightmotor=ontime_1-kp*X_angle+kd*X_error-ki*X_integ;	
		if(rightmotor<0)
			rightmotor=0;
		X_angle_prev=X_angle;
		}
		else
		{
			rightmotor=ontime_1;
			leftmotor=ontime_1;
		}			
				
			leftmotor=ontime_1;
			rightmotor=ontime_1;
		//setspeed(front,frontmotor);
		setspeed(left,leftmotor);
		setspeed(right,rightmotor);
		////setspeed(back,backmotor);	
		//
		
		//uart_1_Transmit('Y');
		//uartWriteInt(Y_angle,4);
		//uart_1_Transmit('y');
		//uartWriteInt(Yangle,4);
		//uart_1_Transmit('X');
		//uartWriteInt(Xangle,4);
		uart_1_Transmit('X');
		uartWriteInt(Xangle,4);
		uart_1_Transmit('x');
		uartWriteInt(yangle,4);
		
		//uart_1_Transmit('Y');
		//uartWriteInt(Yangle,4);
		uart_1_Transmit('Y');
		uartWriteInt(Yangle,4);
		uart_1_Transmit('y');
		uartWriteInt(xangle,4);
		//uart_1_Transmit('Z');
		//uartWriteInt(zacc*100,4);
		//uart_1_Transmit('I');
        //uartWriteInt(X_integ,6);
		//uart_1_Transmit('C');
        //uartWriteInt(countera,6);
		
		uart_1_Transmit(13);
		//uart_1_Transmit('Y');
		//uartWriteInt(yacc*100,4);
		//uart_1_Transmit('X');
		//uartWriteInt(xacc*100,4);
		
	////	*/
        /*uart_1_Transmit('F');
	    uartWriteInt(frontmotor,4);
		uart_1_Transmit('B');
	    uartWriteInt(backmotor,4);
		uart_1_Transmit('R');
		uartWriteInt(load,1);
////		*/
		/*
		uart_1_Transmit('L');
		uartWriteInt(load,1);
		uart_1_Transmit('L');
		uart_1_Transmit('&');
		uartWriteInt(mean_1,3);
		uart_1_Transmit('&');
		uartWriteInt(mean_2,3);	
		uart_1_Transmit('&');
		uartWriteInt(ontime_1,4);
		uart_1_Transmit('&');
		uartWriteInt(ontime_2,4);
		uart_1_Transmit('&');
		uartWriteInt(ontime_3,4);
		*/
		
	}
}
/*****************************************************************************
  Function:
	void GenericTCPServer(void)

  Summary:
	Implements a simple ToUpper TCP Server.

  Description:
	This function implements a simple TCP server.  The function is invoked
	periodically by the stack to listen for incoming connections.  When a 
	connection is made, the server reads all incoming data, transforms it
	to uppercase, and echos it back.
	
	This example can be used as a model for many TCP server applications.

  Precondition:
	TCP is initialized.

  Parameters:
	None

  Returns:
  	None
  ***************************************************************************/
void GenericTCPServer(void)
{
	BYTE i,j;
	WORD w, w2;
	BYTE AppBuffer[32];
	WORD wMaxGet, wMaxPut, wCurrentChunk;
	static TCP_SOCKET	MySocket;
	static enum _TCPServerState
	{
		SM_HOME = 0,
		SM_LISTENING,
        SM_CLOSING,
	} TCPServerState = SM_HOME;

	switch(TCPServerState)
	{
		case SM_HOME:
			// Allocate a socket for this server to listen and accept connections on
			MySocket = TCPOpen(0, TCP_OPEN_SERVER, SERVER_PORT, TCP_PURPOSE_GENERIC_TCP_SERVER);
			if(MySocket == INVALID_SOCKET)
				return;
			tcptxbuffer[0]=0;tcptxbufferpoint=0;

			TCPServerState = SM_LISTENING;
			break;

		case SM_LISTENING:
			// See if anyone is connected to us
			if(!TCPIsConnected(MySocket))
			{
				setspeed(0,0);
				LED0_IO=1; LED1_IO=0;
				return;
			}

			// Figure out how many bytes have been received and how many we can transmit.
			wMaxGet = TCPIsGetReady(MySocket);	// Get TCP RX FIFO byte count
			wMaxPut = TCPIsPutReady(MySocket);	// Get TCP TX FIFO free space

			// Make sure we don't take more bytes out of the RX FIFO than we can put into the TX FIFO
//			if(wMaxPut < wMaxGet)
//				wMaxGet = wMaxPut;

			// Process all bytes that we can
			// This is implemented as a loop, processing up to sizeof(AppBuffer) bytes at a time.  
			// This limits memory usage while maximizing performance.  Single byte Gets and Puts are a lot slower than multibyte GetArrays and PutArrays.
			wCurrentChunk = sizeof(AppBuffer);
			if (!wMaxGet)
				{
					w=tcptxbufferpoint;
					if (w>wMaxPut)
                        {
                          w=wMaxPut;
                        }
					if (w)
						{
						TCPPutArray(MySocket,  (BYTE*)&tcptxbuffer[0], w);
						if (tcptxbufferpoint!=w)
							{
		                        tcptxbufferpoint-=w;
								memcpy((BYTE*)&tcptxbuffer[0],(BYTE*)&tcptxbuffer[w],tcptxbufferpoint);
							}
							else   tcptxbufferpoint=0;

						}
				}
			for(w = 0; w < wMaxGet; w += sizeof(AppBuffer))
			{
				// Make sure the last chunk, which will likely be smaller than sizeof(AppBuffer), is treated correctly.
				if(w + sizeof(AppBuffer) > wMaxGet)
					wCurrentChunk = wMaxGet - w;

				// Transfer the data out of the TCP RX FIFO and into our local processing buffer.
				TCPGetArray(MySocket, AppBuffer, wCurrentChunk);
				
				j=0;
				for(w2 = 0; w2 < wCurrentChunk; w2++)
				{
					i = AppBuffer[w2];
					putcomsdata(i);
				}
				
				// Transfer the data out of our local processing buffer and into the TCP TX FIFO.
//				TCPPutArray(MySocket, AppBuffer, wCurrentChunk);
			}
TCPFlush(MySocket);

			// No need to perform any flush.  TCP data in TX FIFO will automatically transmit itself after it accumulates for a while.  If you want to decrease latency (at the expense of wasting network bandwidth on TCP overhead), perform and explicit flush via the TCPFlush() API.

			break;

		case SM_CLOSING:
			// Close the socket connection.
            TCPClose(MySocket);

			TCPServerState = SM_HOME;
			break;
	}
}
Esempio n. 4
0
int main(int argc, char *argv[])

{
  speed_t baud = B57600;  // default baudrate
  stdinfd = fileno(stdin);
  stdoutfd = fileno(stdout);
  serialfd = -1;
  erlpipefd = -1;
  struct termios oldserial, oldstdin;
 
 
 //saveterminal(stdinfd,oldstdin);
 // check(tcgetattr(stdinfd,&oldstdin) == 0, "Failed to backup stdin");

 // if erlang mode is set then redirect stderr to erlerr.txt

   

  //read command line arguments
  check(parsecmdargs(argc, argv) != -1,"Command Line Argument Failure");


  if(erlang == TRUE)
     {
       freopen("erlerr.txt", "w", stderr);
     }  
 debug("pid is %d", getpid());
  //setup pipe if require doesn't work yet 
   if(pipedebug == 1) //
   {
  
     check(makepipe(ERLPIPE) == 0, "Pipe creation or opening failed for: ");
     debug("Pipe created:");
     debug("Process %d opening erlpipe", getpid());
     erlpipefd = open(ERLPIPE, O_WRONLY|O_NONBLOCK); 
     check(erlpipefd > 0, "Erlpipe failed to open pipe: %s\nSetup reader in second terminal\n tail -f erlpipe",ERLPIPE);
     debug("Erlpipe opened");
     /*
     check(makepipe(ERLPIPEIN) == 0, "Erlpipein  creation or opening failed for: "); 
     debug("Pipe created:");
     debug("Process %d opening erlpipein", getpid());
     erlpipeinfd = open(ERLPIPEIN);
     check(erlpipeinfd > 0, "Erlpipein: Failed to open pipe: %s\nSetup reader in second terminal\n tail -f erlpipe",ERLPIPEIN);
     debug("Erlpipe opened"); */
    }
    
   // save terminal settings
 
 
  //get baud rate from command line arguments
  baud = getbaud(baudrate);
  check(baud != B0, "Illegal baud rate"); 
  
  if(serial == TRUE){

  //open serial port
  serialfd = open(serialport, O_RDWR);
  check(serialfd > 0,"Can't open serial port\n Try dmesg | grep tty from shell \n");    
  debug("Opened serial port: %s",serialport);
 
  //set serial port to raw mode
  check(makeraw(serialfd)==0,"Failed to set serial port raw mode");
 //set baudrate serial
  check(setspeed(serialfd,baud)==0,"Failed to set baud rate");
  debug("Baud rate set to: %d", baudrate);
  }

  //set stdin to raw mode
  // check(makeraw(stdinfd)==0, "Failed to set stdin to raw mode");
  // debug("Not Entered raw mode:");

 
  
  //send acknowledge to erlang???

 /* Watch stdinfd and serialfd
       if input is available process then output
       Serial -> Erlang
       Erlang -> Serial
    */ 
  
  /*
  int numbytes;
  while(1){
  numbytes = read_erlang(erlmesg.mesg);
  check(numbytes > 0, "Read error from erlang");
	debug("Read %d bytes from Erlang", numbytes);
	write_serial(numbytes, erlmesg.mesg);
	debug("Wrote %d bytes to serial:", numbytes);
  }
  //main loop
  */
	
  {
    fd_set readfds;
    int maxfd;
    int is, numbytes;
    FD_ZERO(&readfds);
    
    maxfd = (stdinfd > serialfd) ? stdinfd : serialfd;
    maxfd = (maxfd > erlpipeinfd) ? maxfd : erlpipeinfd;

      
    while(1){
      if(serial){
      FD_SET(serialfd, &readfds);
      }
      
      if(pipedebug){
	  FD_SET(erlpipeinfd, &readfds);
	}

      FD_SET(stdinfd, &readfds);
      
      
      is = select(maxfd+1, &readfds, NULLFDS, NULLFDS, NULLTV);
      check(is>0,"Select error -"); 
      
      if (serial && FD_ISSET(serialfd,&readfds)){ // Serial porth ready to read
	FD_CLR(serialfd,&readfds);
	

	numbytes = read(serialfd, erlmesg.mesg, MAXMESG);
	check(numbytes > 0, "Read error from serial port");
	write_erlang(numbytes, erlmesg.mesg);

      }

      if (FD_ISSET(stdinfd,&readfds)){  //Stdin-Erlang ready to read
 	FD_CLR(stdinfd, &readfds);
	
        numbytes = read_erlang(erlmesg.mesg);
	check(numbytes > 0, "Read error from erlang");
	debug("Read %d bytes from Erlang", numbytes);
	write_serial(numbytes, erlmesg.mesg);
	debug("Wrote %d bytes to serial:", numbytes);

      }
      // input pipe
      /*
      if (pipedebug && (FD_ISSET(erlpipeinfd,&readfds)){  
	FD_CLR(erlpipeinfd, &readfds);

    }
      */
    }
  
}	  
  //hanldle input from erlang
  //handle input from serial port
  // restoreterminal(stdinfd,&oldstdin);

	//  tcsetattr(stdinfd,TCSANOW,&oldstdin);
    return 0;

 error:
    //restoreterminal(stdinfd,&oldstdin);
    //    tcsetattr(stdinfd,TCSANOW,&oldstdin);;
    debug("Restored terminal");
    exit(1);
   
    }
Esempio n. 5
0
/*
 * Gather serial mouse data and put it into hd_data->ser_mouse.
 */
void get_serial_mouse(hd_data_t *hd_data)
{
  hd_t *hd;
  int j, fd, fd_max = 0, sel, max_len;
  unsigned modem_info;
  fd_set set, set0;
  struct timeval to;
  ser_device_t *sm;
  struct termios tio;

  FD_ZERO(&set);

  for(hd = hd_data->hd; hd; hd = hd->next) {
    if(
      hd->base_class.id == bc_comm &&
      hd->sub_class.id == sc_com_ser &&
      hd->unix_dev_name &&
      !hd->tag.ser_skip &&
      !has_something_attached(hd_data, hd)
    ) {
      if((fd = open(hd->unix_dev_name, O_RDWR | O_NONBLOCK)) >= 0) {
        if(tcgetattr(fd, &tio)) continue;
        sm = add_ser_mouse_entry(&hd_data->ser_mouse, new_mem(sizeof *sm));
        sm->dev_name = new_str(hd->unix_dev_name);
        sm->fd = fd;
        sm->tio = tio;
        sm->hd_idx = hd->idx;
        if(fd > fd_max) fd_max = fd;
        FD_SET(fd, &set);

        /*
         * PnP COM spec black magic...
         */
        setspeed(fd, 1200, 1, CS7);
        modem_info = TIOCM_DTR | TIOCM_RTS;
        ioctl(fd, TIOCMBIC, &modem_info);
      }
    }
  }

  if(!hd_data->ser_mouse) return;

  /*
   * 200 ms seems to be too fast for some mice...
   */
  usleep(300000);		/* PnP protocol */

  for(sm = hd_data->ser_mouse; sm; sm = sm->next) {
    modem_info = TIOCM_DTR | TIOCM_RTS;
    ioctl(sm->fd, TIOCMBIS, &modem_info);
  }

  /* smaller buffer size, otherwise we might wait really long... */
  max_len = sizeof sm->buf < 128 ? sizeof sm->buf : 128;

  to.tv_sec = 0; to.tv_usec = 300000;

  set0 = set;
  for(;;) {
   to.tv_sec = 0; to.tv_usec = 300000;
    set = set0;
    if((sel = select(fd_max + 1, &set, NULL, NULL, &to)) > 0) {
      for(sm = hd_data->ser_mouse; sm; sm = sm->next) {
        if(FD_ISSET(sm->fd, &set)) {
          if((j = read(sm->fd, sm->buf + sm->buf_len, max_len - sm->buf_len)) > 0)
            sm->buf_len += j;
          if(j <= 0) FD_CLR(sm->fd, &set0);	// #####
        }
      }
    }
    else {
      break;
    }
  }

  for(sm = hd_data->ser_mouse; sm; sm = sm->next) {
    chk4id(sm);
    /* reset serial lines */
    tcflush(sm->fd, TCIOFLUSH);
    tcsetattr(sm->fd, TCSAFLUSH, &sm->tio);
    close(sm->fd);
  }
}