To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.robotics.rcx.nqcOpen lugnet.robotics.rcx.nqc in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / RCX / NQC / 1741
1740  |  1742
Subject: 
New NQC API for RCX-Spybot or RCX-RC communication
Newsgroups: 
lugnet.robotics, lugnet.robotics.rcx, lugnet.robotics.rcx.nqc, lugnet.robotics.spybotics
Date: 
Wed, 12 Jan 2005 03:05:01 GMT
Viewed: 
256 times
  
Recently I happened across the MindScript headers for the RCX in the SDK and saw
some interesting macros for RCX communication.  I decided to port them to NQC
and possibly add them to the official built-in NQC API.  I would be extremely
grateful if someone out there would be willing to try out some of the functions
defined below.  You can just paste this directly into your test .nqc program if
you want (unless you download the very latest swan_test.zip, in which case it's
already included).

Here's a very simple sample using the API functions below:


#pragma noinit

task main()
{
    InitRCComm();
    SetRCMessage(RC_CHANNEL_1, RC_CMD_FWD, RC_CMD_FWD);
    repeat(5)
    {
        SendRCMsg();
        Wait(100);
    }
    Wait(200);

    InitSpybotComm(); // switch to Spybot communication

    SetSpybotMessage(MSG_BROADCAST, ID_BOT_MIN+1, 0, CMD_FIRE_LASER, 1, 100);
    repeat(2)
    {
        SendSpybotMsg();
        Wait(100);
    }
    Wait(200);

    SetSpybotCtrlMessage(ID_BOT_MIN+1, SPY_CTRL_BTN_1);
    repeat(5)
    {
        SendSpybotCtrlMsg();
        Wait(100);
    }
    Wait(200);

    SetSpybotCtrlPingMessage(ID_BOT_MIN+1);
    SendSpybotCtrlPingMsg();

}




// API for interacting with Spybot and other RC components
#define MSG_BROADCAST 0x8a
#define MSG_LINKCAST 0x89
#define MSG_DIRECT 0x88

// base addresses for uart data
#define UART_BOT 0
#define UART_CTRL 7
#define UART_PING 9
#define UART_RC 11

__nolist void InitSpybotComm()
{
SetSerialPacket(SERIAL_PACKET_SPYBOT);
        SetSerialComm(SERIAL_COMM_SPYBOT);
}

__nolist void SendSpybotMsg() { SendSerial(UART_BOT, 7); }

__nolist void SetSpybotMessage(const int &nMode, const int &nMyID,
          const int &nAddress, const int &nCmd, const int &nHiByte, const int
&nLoByte)
{
SetSerialData(UART_BOT+0,nMode);
SetSerialData(UART_BOT+1,nMyID);
SetSerialData(UART_BOT+2,nAddress);
SetSerialData(UART_BOT+3,nCmd);
SetSerialData(UART_BOT+4,nHiByte);
SetSerialData(UART_BOT+5,nLoByte);
SetSerialData(UART_BOT+6,0-nMode-nMyID-nAddress-nCmd-nHiByte-nLoByte);
// SetSerialData(UART_BOT+6,0x100-((nMode+nMyID+nAddress+nCmd+nHiByte+nLoByte)&0xff));
}

__nolist void SendSpybotMessage(const int &nMode, const int &nMyID,
          const int &nAddress, const int &nCmd, const int &nHiByte, const int
&nLoByte)
{
InitSpybotComm();
SetSpybotMessage(nMode, nMyID, nAddress, nCmd, nHiByte, nLoByte);
SendSpybotMsg();
}

// Spybot controller commands
#define SPY_CTRL_BTN_1 0x01
#define SPY_CTRL_BTN_2 0x02
#define SPY_CTRL_BTN_3 0x03
#define SPY_CTRL_BTN_4 0x04
#define SPY_CTRL_BTN_5 0x05
#define SPY_CTRL_RCX_1 0x06 //RCX to Spybot specific
#define SPY_CTRL_RCX_2 0x07
#define SPY_CTRL_RCX_3 0x08
#define SPY_CTRL_RCX_4 0x09
#define SPY_CTRL_RCX_5 0x0a
#define SPY_CTRL_RCX_6 0x0b
#define SPY_CTRL_RCX_7 0x0c
#define SPY_CTRL_RCX_8 0x0d

#define __SPY_CTRL_CMD 0x90

__nolist void SendSpybotCtrlMsg() { SendSerial(UART_CTRL, 2); }

__nolist void SetSpybotCtrlMessage(const int &nMyID, const int &nMsg)
{
SetSerialData(UART_CTRL+0,__SPY_CTRL_CMD+nMyID);
SetSerialData(UART_CTRL+1,(nMsg * 16) + ((0 - ((__SPY_CTRL_CMD+nMyID) / 16) -
(__SPY_CTRL_CMD+nMyID) - nMsg) & 0x0f));
}

__nolist void SendSpybotCtrlMessage(const int &nMyID, const int &nMsg)
{
InitSpybotComm();
SetSpybotCtrlMessage(nMyID, nMsg);
SendSpybotCtrlMsg();
}

__nolist void SendSpybotCtrlPingMsg() { SendSerial(UART_PING, 2); }

__nolist void SetSpybotCtrlPingMessage(const int &nID)
{
SetSerialData(UART_PING+0,__SPY_CTRL_CMD+nID);
SetSerialData(UART_PING+1,(0 - ((__SPY_CTRL_CMD+nID) / 16) -
(__SPY_CTRL_CMD+nID)) & 0x0f);
}

__nolist void SendSpybotCtrlPingMessage(const int &nID)
{
InitSpybotComm();
SetSpybotCtrlPingMessage(nID);
SendSpybotCtrlPingMsg();
}

__nolist void SendSpybotPingMsg() { SendSerial(UART_PING, 4); }

__nolist void SetSpybotPing(const int &nLinkID, const int &nMyID, const int
&nInfo)
{
SetSerialData(UART_PING+0,0x80+(nLinkID&0x7));
SetSerialData(UART_PING+1,nMyID);
SetSerialData(UART_PING+2,nInfo);
SetSerialData(UART_PING+3,0-(0x80+(nLinkID&0x7))-nMyID-nInfo);
// SetSerialData(UART_PING+3,0x100-((0x80+(nLinkID&0x7)+nMyID+nInfo)&0xff));
}

__nolist void SendSpybotPing(const int &nLinkID, const int &nMyID, const int
&nInfo)
{
InitSpybotComm();
SetSpybotPing(nLinkID, nMyID, nInfo);
SendSpybotPingMsg();
}

// RC channels and commands
#define RC_CHANNEL_1 0x50
#define RC_CHANNEL_2 0x60
#define RC_CHANNEL_3 0x70

#define RC_CMD_FLOAT 0x00
#define RC_CMD_FWD 0x07
#define RC_CMD_OFF 0x08
#define RC_CMD_REV 0x0f

__nolist void InitRCComm()
{
SetSerialPacket(SERIAL_PACKET_RC);
        SetSerialComm(SERIAL_COMM_RC);
}

__nolist void SendRCMsg() { SendSerial(UART_RC, 2); }

__nolist void SetRCMessage(const int &nChannel, const int &nLeft, const int
&nRight)
{
SetSerialData(UART_RC+0,nChannel + nLeft);
SetSerialData(UART_RC+1,(nRight * 16) + ((0 - nRight - nLeft - (nChannel /
16)) & 0x0f));
}

__nolist void SendRCMessage(const int &nChannel, const int &nLeft, const int
&nRight)
{
InitRCComm();
SetRCMessage(nChannel, nLeft, nRight);
SendRCMsg();
}


  #endif

  // Link ID constants for RCX2/Swan SendSpybotPing and Spybot API
  #define ID_NONE 0
  #define ID_CTRL_1 1
  #define ID_CTRL_2 2
  #define ID_CTRL_3 3
  #define ID_CTRL_4 4
  #define ID_CTRL_5 5
  #define ID_CTRL_6 6
  #define ID_PC 7

  // my robot ID min and max values
  #define ID_BOT_MIN 8
  #define ID_BOT_MAX 255

  // commands
  #define CMD_GAME_START 0x10
  #define CMD_GAME_COMMAND 0x11
  #define CMD_GAME_GETVAR 0x12
  #define CMD_GAME_VALUE 0x13
  #define CMD_GAME_WIN 0x14
  #define CMD_GAME_LOSE 0x15
  #define CMD_GAME_BLINK 0x16
  #define CMD_GAME_GIVE 0x17
  #define CMD_GAME_TAKE 0x18

  #define CMD_FROM_RCX2 0x1f

//fire command protocol: <nBot>, 0x20 + <fireCommand>, <param1>, <param2>
// param1: target aspect (ie front, back, front left...)
// param2: strength
  #define CMD_FIRE_LASER 0x21
  #define CMD_FIRE_SPINNER 0x22
  #define CMD_FIRE_ELECTRONET 0x23

//react to special ability command protocol: <nBot>, 0x30 + <species>, <param1>,
<param2>
// param1: range
// param2: strength/time
  #define CMD_REACT_TO_GIGAMESH 0x31
  #define CMD_REACT_TO_SNAPTRAX 0x32
  #define CMD_REACT_TO_SHADOWSTRIKE 0x33
  #define CMD_REACT_TO_TECHNOJAW 0x34

//activate token in another bot tell protocol: <nBot>, 0x40 + <species>,
<param1>, <param2>
// param1: 0
// param2: time
  #define CMD_TELL_MAGNET 0x41
  #define CMD_TELL_REPULSE 0x42
  #define CMD_TELL_FLASHBLIND 0x43
  #define CMD_TELL_FREEZE 0x44
  #define CMD_TELL_SLOW 0x45
  #define CMD_TELL_REVERSE 0x46
  #define CMD_TELL_DIZZY 0x47
  #define CMD_TELL_TAKECONTROL 0x48

  #define CMD_TYPE_USER 0x50
  #define CMD_TYPE_SPARE 0x60
  #define CMD_TYPE_VLL 0x70



Message has 1 Reply:
  Re: New NQC API for RCX-Spybot or RCX-RC communication
 
(...) I forgot to include a few #defines: #define SERIAL_PACKET_RC 0 // default #define SERIAL_PACKET_SPYBOT 0 #define SERIAL_COMM_RC 7 // 4800/76KHZ/25% #define SERIAL_COMM_SPYBOT 7 // was SERIAL_COMM_4800 + SERIAL_COMM_76KHZ The RC function, by (...) (20 years ago, 12-Jan-05, to lugnet.robotics, lugnet.robotics.rcx, lugnet.robotics.rcx.nqc, lugnet.robotics.spybotics)

4 Messages in This Thread:

Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    
Active threads in Robotics

 
Verified and Trusted Team of Hackers
24 hours ago
Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR