📄 usb.c
字号:
//Generate message
buffout.Int[0] = 0xFFA5; //Start word
buffout.Int[1] = 0; //provis for diff message types
buffout.Int[2] = 0; //spare
buffout.Int[3] = Word((char*)&time); //sequence number
buffout.Int[4] = Word((char*)&Analog0); //Analog inputs (IR 0 - 7)
buffout.Int[5] = Word((char*)&Analog1);
buffout.Int[6] = Word((char*)&Analog2);
buffout.Int[7] = Word((char*)&Analog3);
buffout.Int[8] = Word((char*)&Analog4);
buffout.Int[9] = Word((char*)&Analog5);
buffout.Int[10] = Word((char*)&Analog6);
buffout.Int[11] = Word((char*)&Analog7);
buffout.Int[12] = Word((char*)&Analog8); // battery voltage
buffout.Int[13] = Word((char*)&Analog9); // no connection
buffout.Int[14] = Word((char*)&Analog10); // no connection
buffout.Int[15] = Word((char*)&Analog11); // no connection
buffout.Int[16] = Word((char*)&Analog12); // no connection
buffout.Int[17] = Word((char*)&RC_Sonar[0]); // timer values or est. RC posn.
buffout.Int[18] = Word((char*)&RC_Sonar[1]); // RC servo position(deg*10)
buffout.Int[19] = Word((char*)&RC_Sonar[2]); // or sonar distance(mm)
buffout.Int[20] = Word((char*)&RC_Sonar[3]);
buffout.Int[21] = Word((char*)&RC_Sonar[4]);
buffout.Int[22] = Word((char*)&RC_Sonar[5]);
buffout.Int[23] = Word((char*)&RC_Sonar[6]);
buffout.Int[24] = Word((char*)&PitchDeg); //pitch attitude in deg *10
buffout.Int[25] = Word((char*)&RollDeg); //roll attitude in deg *10
buffout.Int[26] = Word((char*)&AccelX); //accel forward (mm/sec^2)
buffout.Int[27] = Word((char*)&AccelY); //accel sideways (mm/sec^2)
buffout.Int[28] = Word((char*)&YRatedps); //yaw rate in deg/sec *10
buffout.Int[29] = Word((char*)&hdgdeg); //inertial hdg deg*10 (+/- 180)
buffout.Int[30] = Word((char*)&Compass); //mag hdg in deg*10 (0 - 359.9)
buffout.Int[31] = Word((char*)&headingDR); //heading based on encoders
buffout.Long[16] = Long((char*)&DistActL); //Distance of L wheel travel (mm)
buffout.Long[17] = Long((char*)&DistActR); //Distance of R wheel travel (mm)
buffout.Int[36] = Word((char*)&SpeedL); //left wheel speed (mm/sec)
buffout.Int[37] = Word((char*)&SpeedR); //right wheel speed (mm/sec)
buffout.Int[38] = Word((char*)&DIOdata); //discrete i/o status
buffout.Int[39] = 0; //spare
buffout.Int[40] = 0; //spare
buffout.Int[41] = 0; //spare
buffout.Long[21] = Long((char*)&Xcoord); //X coordinate
buffout.Long[22] = Long((char*)&Ycoord); //Y coordinate
buffout.Int[46] = 0; //spare
buffout.Int[47] = 0; //spare
buffout.Int[48] = 0; //spare
buffout.Int[49] = 0; //spare
buffout.Int[50] = 0; //spare
buffout.Int[51] = 0; //spare
buffout.Int[52] = 0; //spare
buffout.Int[53] = 0; //spare
buffout.Int[54] = 0; //spare
buffout.Int[55] = Word((char*)&ucStatus); //status message
buffout.Int[56] = 0; //spare
buffout.Int[57] = Word((char*)&FwdCmdCurrent); //Forward command in progress
buffout.Int[58] = Word((char*)&FwdModeDone); //Status of current fwd cmd
buffout.Int[59] = Word((char*)&TurnCmdCurrent); //Turn command in progress
buffout.Int[60] = Word((char*)&TurnModeDone); //Status of current turn cmd
buffout.Int[61] = Word((char*)&seqnumCurrent); //Seqnum of last message rec'd
//calculate CRC16 (for buffout[0] through buffout[BUFFSIZEout-1])
buffptr = (char*) buffout.Int;
CRC16 = 0;
for(i=0;i<((BUFFSIZEout-1)*2);i++)
{ CRC16 = addCRC(CRC16,*buffptr++);
}
buffout.Int[62] = CRC16;
USBsendidx = 0; //set indicating message ready to be sent
}
//--------------------------------------------------------------------------
void USBsendmsg() //transmit contents of buffout
/* Transfers contents of buffout to USB module using emulated parallel port.
This function is non-blocking, but requires the calling code to control
the USBsendidx pointer and generate multiple calls if necessary.
*/
{ DDRB = 0xFF; //set data port as output
buffptr = (char*) buffout.Int;
while(USBsendidx < BUFFSIZEout*2)
{
if(PORTA & 0x04) return; //return if transmit buffer full
PORTA |= 0x02; //WR strobe on
PORTB = *buffptr++; //send output data to port
PORTA &= ~0x02; //WR strobe off
USBsendidx +=1;
}
}
//--------------------------------------------------------------------------
void USBreadmsg()
/* Reads data from USB module until buffer is full through emulated parallel
port. This function is non blocking.
*/
{
char chr;
DDRB = 0x00; //set data port as input
while(USBreadidx < BUFFSIZEin*2) //read all bytes
{
if(USBreadidx == 0)
{
do //read bytes til find start byte "FF"
{
if(PORTA & 0x08) return; //return if receive buffer empty
PORTA &= ~0x01; //RD strobe on
chr = PORTB; //get data byte
PORTA |= 0x01; //RD strobe off
} while (chr != 0xFF);
//check for 2nd start byte????
USBreadidx = 1;
}
//read next 123 bytes
if(PORTA & 0x08) return; //return if buffer empty
PORTA &= ~0x01; //RD strobe on
buffin[USBreadidx] = PORTB; //get data byte
PORTA |= 0x01; //RD strobe off
USBreadidx += 1;
}
}
//---------------------------------------------------------------------------
void USBupdate(void)
{
int i; //loop counter
//Send output message
if(time%4 == 0) //if time for a new output message (every 64 msec.)
{
if(USBsendidx == BUFFSIZEout*2) //if last message was sent
{ USBmsgfill(); //load buffer with new data
USBsendidx = 0; //set index to first byte
}
else
{ //if(BiteMode == 0)printf("USB output message missed\n");
}
}
if(USBsendidx != BUFFSIZEout*2) //if message not fully sent
{ USBsendmsg();
}
//Read input message
USBtimeout += 1; //increment timeout counter each frame
if(USBtimeout >= USBtimeoutLimit) USBtimeout = USBtimeoutLimit;
if(USBreadidx < BUFFSIZEin*2) USBreadmsg(); //if buffer not full, read more
if(USBreadidx == BUFFSIZEin*2) //if buffer is full
{ for(i=1; i<BUFFSIZEin*2; i++) //transfer data for export
{ BuffIn[i] = buffin[i];
}
seqnumUSB = Word(&BuffIn[2]);
USBreadidx = 0; //reset to buffer empty
USBtimeout -= 12; //decrement timeout counter when receive
if(USBtimeout <=0) USBtimeout = 0;
}
} //end USBupdate()
// OPEN SOURCE SOFTWARE LICENSE
/* Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -