void InternalHandleCommand(SRobotCommand const& cmd) { switch(cmd.m_ecmd) { case ecmdCONNECT: OnConnection(); break; case ecmdRESET: OnDisconnection(); break; case ecmdMOVE: { bool bReverse = false; for(unsigned int i=0; i<countof(g_amotors); ++i) { int const nSpeed = i%2==0 ? cmd.m_nSpeedLeft : cmd.m_nSpeedRight; bReverse = bReverse || ((nSpeed < 0) != g_amotors[i].m_bReverse); } if(bReverse) { // stop all motors and reset PID // Serial.println("STOP Motors"); for(unsigned int i=0; i<countof(g_amotors); ++i) { g_amotors[i].Stop(g_apid[i]); } delay(200); } for(unsigned int i=0; i<countof(g_amotors); ++i) { int const nSpeed = i%2==0 ? cmd.m_nSpeedLeft : cmd.m_nSpeedRight; if(0==nSpeed) { g_amotors[i].Stop(g_apid[i]); } else { g_amotors[i].SetSpeed(constrain(nSpeed, -MAX_SPEED, MAX_SPEED)); } } } } }
void loop() { if(g_bConnected) { g_servo.loop(); if(0<Serial.available()) { auto readcmd = ReadCommand(); if(readcmd.m_bValid) { HandleCommand(readcmd.m_cmd); if(!g_bConnected) return; } } else if(c_nTIMETODISCONNECT < millis()-g_nLastCommand) { OnDisconnection(); return; } else if(c_nTIMETOSTOP < millis()-g_nLastCommand) { InternalHandleCommand(SRobotCommand::stop()); } for(unsigned int i=0; i<countof(g_amotors); ++i) { g_amotors[i].ComputePID(g_apid[i]); } SendSensorData(); } else { auto readcmd = ReadCommand(); if(readcmd.m_bValid && readcmd.m_cmd.m_ecmd==ecmdCONNECT) { HandleCommand(readcmd.m_cmd); } } }
void CNetPTCPConnection::Destory() { if(IsConnected()) { m_Socket.Close(); OnDisconnection(); } else { m_Socket.Close(); } if(m_pEpollEventRouter) { m_pEpollEventRouter->SetEventHander(NULL); GetServer()->DeleteEventRouter(m_pEpollEventRouter); m_pEpollEventRouter=NULL; } CEpollEventObject * pEpollEventObject; while(m_SendQueue.PopFront(pEpollEventObject)) { GetServer()->DeleteEventObject(pEpollEventObject); } }
void CNetConnection::Destory() { if(IsConnected()) { OnDisconnection(); } m_Socket.Close(); if(m_pEpollEventRouter) { m_pEpollEventRouter->SetEventHander(NULL); GetServer()->DeleteEventRouter(m_pEpollEventRouter); m_pEpollEventRouter=NULL; } CEpollEventObject * pEpollEventObject; while(m_RecvQueue.PopFront(pEpollEventObject)) { GetServer()->DeleteEventObject(pEpollEventObject); } while(m_SendQueue.PopFront(pEpollEventObject)) { GetServer()->DeleteEventObject(pEpollEventObject); } m_WantClose=FALSE; }
void CNetPTCPConnection::Disconnect() { //PrintNetLog(0xffffffff,"(%d)Connection关闭",GetID()); //PrintNetLog(0xffffffff,"%s连接关闭",GetName()); m_Socket.Close(); CEpollEventObject * pEpollEventObject; while(m_SendQueue.PopFront(pEpollEventObject)) { GetServer()->DeleteEventObject(pEpollEventObject); } OnDisconnection(); }
void setup() { Serial.begin(230400); delay(3000); //3 seconds delay for enabling to see the start up comments on the serial board #ifdef SERIAL_TRACE Serial.println("Hello!"); #endif // Show we are in setup pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, HIGH); // Port setup pinMode(RELAY_PIN, OUTPUT); digitalWrite(RELAY_PIN, HIGH); // Servo & Lidar g_servo.setup(); g_lidar.begin(); // TODO: Does this increase speed of LIDAR readings // g_lidar.beginContinuous(); // Motor setup for(unsigned int i=0; i<countof(g_amotors); ++i) { g_amotors[i].setup(); attachInterrupt(g_amotors[i].ENCODER_IRQ, c_afnInterrupts[i], CHANGE); } // Initialise the IMU (connection is optional) g_bBNO = g_bno.begin(); delay(1000); if(g_bBNO) g_bno.setExtCrystalUse(true); OnDisconnection(); }
void SSGameLobby::Startup() { // Seed the randomizer const unsigned int randomSeed = static_cast<const unsigned int>( SDL_GetPerformanceCounter() ); g_Randomizer.Seed( randomSeed ); // Register subscriber g_SSMail.RegisterSubscriber( this ); m_UserInterests = MessageTypes::COLOUR_CHANGE | MessageTypes::TEAM_CHANGE | MessageTypes::SPAWN_POINT_CHANGE | MessageTypes::CHANGE_SPAWN_COUNT | MessageTypes::RESERVE_AI_PLAYER; // Set all player slots to not ready for ( int i = 0; i < MAX_PLAYERS; ++i ) m_PlayersReady[i] = false; // Register callback to network engine m_NetworkCallbackHandle = g_NetworkEngine.RegisterCallback( NetworkCallbackEvent::Disconnection, std::bind( &SSGameLobby::OnDisconnection, this, std::placeholders::_1 ) ); // Player colours m_ColourEntries.push_back( ColourEntry { "Green", Colours::KINDA_GREEN, Colours::KINDA_GREEN , -1 } ); m_ColourEntries.push_back( ColourEntry { "Red", Colours::KINDA_RED , Colours::KINDA_RED , -1 } ); m_ColourEntries.push_back( ColourEntry { "Blue", Colours::KINDA_BLUE , Colours::KINDA_BLUE , -1 } ); m_ColourEntries.push_back( ColourEntry { "Pink", Colours::KINDA_PINK , Colours::KINDA_PINK , -1 } ); m_ColourEntries.push_back( ColourEntry { "Yellow", Colours::VERY_YELLOW, Colours::VERY_YELLOW , -1 } ); m_ColourEntries.push_back( ColourEntry { "Cyan", Colours::LTBLUE , Colours::LTBLUE , -1 } ); // Text colour modified m_ColourEntries.push_back( ColourEntry { "Black", Colours::BLACK , glm::vec4(0.4f, 0.4f, 0.4f, 1.0f), -1 } ); g_GameData.SetPlayerColour( 0, Colours::KINDA_GREEN ); g_GameData.SetPlayerColour( 1, Colours::KINDA_RED ); g_GameData.SetPlayerColour( 2, Colours::KINDA_BLUE ); g_GameData.SetPlayerColour( 3, Colours::KINDA_PINK ); // Set up GUI g_GUI.UseFont( FONT_ID_LEKTON_16 ); m_ConnectionState = ConnectionState::EnteringIP; m_ConnectionAttemptFailed = false; g_NetworkEngine.RegisterCallback( NetworkCallbackEvent::ConnectionAttemptSuccess, std::bind( &SSGameLobby::OnConnectionSuccess, this, std::placeholders::_1 ) ); // TODODB: Check against this variable already being set eller nånting. g_NetworkEngine.RegisterCallback( NetworkCallbackEvent::ConnectionAttemptFail, std::bind( &SSGameLobby::OnConnectionFail, this, std::placeholders::_1 ) ); // TODODB: Check against this variable already being set eller nånting. // Button scripts g_Script.Register( m_ScriptNameReady.c_str(), [&] ( IScriptEngine* ) -> int { OnReadyButtonClicked(); return 0; } ); g_Script.Register( m_ScriptNameStartGame.c_str(), [&] ( IScriptEngine* ) -> int { StartGame(); return 0; } ); g_Script.Register(m_ScriptChangeGameSetting.c_str(), [this](IScriptEngine* script) -> int { m_NumberOfSpawns += script->PopInt(); if (m_NumberOfSpawns < 1) m_NumberOfSpawns = 1; if (m_NumberOfSpawns > SQUAD_MAXIMUM_UNIT_COUNT) m_NumberOfSpawns = SQUAD_MAXIMUM_UNIT_COUNT; ChangeSpawnCount( m_NumberOfSpawns ); return 0; } ); GUI::Window* parentWindow = g_GUI.GetWindow( m_WindowNameParent ); glm::ivec2 middleParent = glm::ivec2(parentWindow->GetBoundingBoxRef().X, parentWindow->GetBoundingBoxRef().Y) + parentWindow->GetSize() / 2; glm::ivec2 sizeParent = parentWindow->GetSize( ); { // Enter ip m_WindowEnterIP = g_GUI.AddWindow( m_WindowNameEnterIP, GUI::Rectangle( middleParent.x - m_WidthEnterIP / 2, middleParent.y - m_HeightEnterIP / 2, m_WidthEnterIP, m_HeightEnterIP ), m_WindowNameParent, true ); m_TextBoxEnterIP = g_GUI.AddTextBox( "", GUI::Rectangle( m_WidthButton, m_HeightEnterIP - m_HeightTextBox, m_WidthTextBox, m_HeightTextBox) , m_WindowNameEnterIP); // Connect m_ButtonConnect = g_GUI.AddButton( "", GUI::Rectangle( m_WidthEnterIP - m_WidthButton * 2, m_HeightEnterIP - m_HeightButton, m_WidthButton, m_HeightButton), m_WindowNameEnterIP ); m_ButtonConnect->SetText( "Connect" ); g_Script.Register( m_ScriptNameConnect.c_str(), [this](IScriptEngine*){ ConnectToGame(m_TextBoxEnterIP->GetText()); return 0; } ); m_ButtonConnect->SetClickScript( m_ScriptNameConnect + "()" ); m_ButtonConnect->GetBackgroundRef().Texture = m_TextureNameButtons; // Host m_ButtonHost = g_GUI.AddButton( "", GUI::Rectangle( m_WidthEnterIP - m_WidthButton, m_HeightEnterIP - m_HeightButton, m_WidthButton, m_HeightButton), m_WindowNameEnterIP ); m_ButtonHost->SetText( "Host" ); g_Script.Register( m_ScriptNameHost.c_str(), [this](IScriptEngine*){ HostGame(); return 0; } ); m_ButtonHost->SetClickScript( m_ScriptNameHost + "()" ); m_ButtonHost->GetBackgroundRef().Texture = m_TextureNameButtons; // Text GUI::Text* infoText = g_GUI.AddText( "", GUI::TextDefinition( "Please enter an IP adress to connect to", 0, m_TopPadding, m_WidthEnterIP, m_HeightInfoText ), m_WindowNameEnterIP ); infoText->GetTextDefinitionRef().Alignment = GUI::ALIGNMENT_TOP_CENTER; m_TextError = g_GUI.AddText( "", GUI::TextDefinition( "", 0, m_TopPadding + m_HeightInfoText, m_WidthEnterIP, m_HeightErrorText ), m_WindowNameEnterIP ); // Back button m_ButtonBackEnterIP = g_GUI.AddButton( "", GUI::Rectangle( 0, m_WindowEnterIP->GetSize( ).y - m_HeightButton, m_WidthButton, m_HeightButton ), m_WindowNameEnterIP ); g_Script.Register( m_ScriptNameBackEnterIP.c_str(), [this](IScriptEngine*){ BackFromEnterIP( ); return 0; } ); m_ButtonBackEnterIP->SetClickScript( m_ScriptNameBackEnterIP + "()" ); m_ButtonBackEnterIP->SetText( "Back" ); m_ButtonBackEnterIP->GetBackgroundRef().Texture = m_TextureNameButtons; // Enter ip is default window m_WindowEnterIP->ToggleOpen(); // Default IP CallbackConfig* networkConfig = g_ConfigManager.GetConfig( "network.cfg" ); int defaultPort = networkConfig->GetInt( "DefaultListeningPort", 40606, "Default port for establishing connections" ); rString savedIP = networkConfig->GetString( "SavedIP", "", "The IP of the last successfull connection attempt" ); m_TextBoxEnterIP->SetText( savedIP ); } { // Lobby m_WindowLobby = g_GUI.AddWindow( m_WindowNameLobby, parentWindow->GetBoundingBoxRef(), m_WindowNameParent ); glm::ivec2 sizeLobby = m_WindowLobby->GetSize( ); // Start game (host) m_ButtonStartGame = g_GUI.AddButton( "", GUI::Rectangle( sizeLobby.x - m_WidthButton * 2, sizeLobby.y - m_HeightButton, m_WidthButton, m_HeightButton ), m_WindowNameLobby ); m_ButtonStartGame->SetText( "Start" ); m_ButtonStartGame->SetClickScript( m_ScriptNameStartGame + "()" ); m_ButtonStartGame->SetVisible( false ); m_ButtonStartGame->SetEnabled( false ); m_ButtonStartGame->GetBackgroundRef().Texture = m_TextureNameButtons; // Back button m_ButtonBackLobby = g_GUI.AddButton( "", GUI::Rectangle( 0, sizeLobby.y - m_HeightButton, m_WidthButton, m_HeightButton ), m_WindowNameLobby ); g_Script.Register( m_ScriptNameBackLobby.c_str(), [this](IScriptEngine*) { BackFromLobby(); return 0; } ); m_ButtonBackLobby->SetClickScript( m_ScriptNameBackLobby + "()" ); m_ButtonBackLobby->SetText( "Back" ); m_ButtonBackLobby->GetBackgroundRef().Texture = m_TextureNameButtons; // Ready (client) m_ButtonReady = g_GUI.AddButton( "", GUI::Rectangle( sizeLobby.x - m_WidthButton, sizeLobby.y - m_HeightButton, m_WidthButton, m_HeightButton ), m_WindowNameLobby ); m_ButtonReady->GetBackgroundRef().Texture = m_TextureNameButtons; m_ButtonReady->SetText( "Ready" ); m_ButtonReady->SetClickScript( m_ScriptNameReady + "()" ); { // Connected clients m_WindowConnectedClients = g_GUI.AddWindow( m_WindowNameConnectedClients, GUI::Rectangle( 0, 0, sizeParent.x / 2, sizeParent.y / 2 ), m_WindowNameLobby, true ); m_WindowConnectedClients->ToggleOpen( ); g_GUI.UseFont( FONT_ID_LEKTON_20 ); g_Script.Register( m_ScriptNameColourChange.c_str(), [this](IScriptEngine* script) -> int { int playerID = script->PopInt( ); assert( playerID >= 0 && playerID < MAX_PLAYERS ); int selectedIndex = m_ConnectedPlayerEntries[playerID].Colour->GetSelectedIndex( ); TryChangeColour( selectedIndex, playerID, false ); return 0; } ); g_Script.Register( m_ScriptNameTeamChange.c_str(), [this](IScriptEngine* script) -> int { int playerID = script->PopInt( ); assert( playerID >= 0 && playerID < MAX_PLAYERS ); int selectedIndex = m_ConnectedPlayerEntries[playerID].Team->GetSelectedIndex( ); TryChangeTeam( selectedIndex, playerID, false ); return 0; } ); g_Script.Register( m_ScriptNameKick.c_str( ), [this](IScriptEngine* script) -> int { int playerID = script->PopInt( ); if ( m_ConnectedPlayerEntries[ playerID ].IsAI ) { ChangeReserveAI( playerID, false ); OnDisconnection( &playerID ); } else g_NetworkEngine.RequestDisconnection( playerID ); return 0; } ); g_Script.Register( m_ScriptNameReserveForAI.c_str( ), [this](IScriptEngine* script) -> int { int playerID = script->PopInt( ); if ( !m_ConnectedPlayerEntries[playerID].IsAI ) { OnNewClientConnect( playerID ); } ChangeReserveAI( playerID, true ); g_NetworkEngine.RequestDisconnection( playerID ); return 0; } ); // Players int entryY = m_HeightPlayerEntry * (MAX_PLAYERS - 1) + m_TopPadding; for ( int i = MAX_PLAYERS -1 ; i >= 0; --i ) { int x = m_WindowConnectedClients->GetSize().x - m_WidthReady; ConnectedPlayer& playerEntry = m_ConnectedPlayerEntries[i]; playerEntry.Name = g_GUI.AddText( "", GUI::TextDefinition( "TODOJM", m_LeftPadding, entryY, m_WidthName, m_HeightPlayerEntry ), m_WindowNameConnectedClients ); playerEntry.Ready = g_GUI.AddSprite( "", GUI::SpriteDefinition( m_TextureNameReadySprite, m_WindowConnectedClients->GetSize().x - m_WidthReady, entryY, m_WidthReady, m_HeightPlayerEntry ), m_WindowNameConnectedClients ); x -= m_WidthTeam; playerEntry.Team = g_GUI.AddComboBox( "", GUI::Rectangle( x, entryY, m_WidthTeam, m_HeightPlayerEntry ), m_WindowNameConnectedClients ); playerEntry.Team->AddItem( "Team 1" ); playerEntry.Team->AddItem( "Team 2" ); playerEntry.Team->AddItem( "Team 3" ); playerEntry.Team->AddItem( "Team 4" ); playerEntry.Team->SetClickScript( m_ScriptNameTeamChange + "(" + rToString( i ) + ")"); playerEntry.Team->GetBackgroundRef().Texture = m_TextureNameComboBox; playerEntry.Team->SetItemBackgroundImage( m_TextureNameComboBoxItem ); x -= m_WidthColour; playerEntry.Colour = g_GUI.AddComboBox( "", GUI::Rectangle( x, entryY, m_WidthColour, m_HeightPlayerEntry ), m_WindowNameConnectedClients ); for( auto& colourEntry : m_ColourEntries ) { playerEntry.Colour->AddItem( colourEntry.Text ); playerEntry.Colour->GetItem( playerEntry.Colour->GetNumItems() - 1 ).Text.Colour = colourEntry.TextColour; } playerEntry.Colour->SetClickScript( m_ScriptNameColourChange + "(" + rToString( i ) + ")" ); playerEntry.Colour->GetBackgroundRef().Texture = m_TextureNameComboBox; playerEntry.Colour->SetItemBackgroundImage( m_TextureNameComboBoxItem ); x -= m_WidthKick; playerEntry.Kick = g_GUI.AddButton( "", GUI::Rectangle( x, entryY, m_WidthKick, m_HeightPlayerEntry ), m_WindowNameConnectedClients ); playerEntry.Kick->SetClickScript( m_ScriptNameKick + "(" + rToString( i ) + ")" ); playerEntry.Kick->GetBackgroundRef().Texture = m_TextureNameKickSprite; x -= m_WidthReserveForAI; playerEntry.ReserveForAIBtn = g_GUI.AddButton( "", GUI::Rectangle( x, entryY, m_WidthReserveForAI, m_HeightPlayerEntry ), m_WindowNameConnectedClients ); playerEntry.ReserveForAIBtn->SetClickScript( m_ScriptNameReserveForAI + "(" + rToString( i ) + ")" ); playerEntry.ReserveForAIBtn->GetBackgroundRef().Texture = m_TextureNameButtons; playerEntry.ReserveForAIBtn->GetTextDefinitionRef().Text = "AI"; playerEntry.PlayerID = i; entryY -= m_HeightPlayerEntry; } } { // Select level window m_WindowSelectLevel = g_GUI.AddWindow( m_WindowNameSelectLevel, GUI::Rectangle( sizeLobby.x / 2, 0, sizeLobby.x / 2, sizeLobby.y / 2 ), m_WindowNameLobby, true ); glm::ivec2 slSize = m_WindowSelectLevel->GetSize(); m_WindowSelectLevel->ToggleOpen( ); m_ComboBoxSelectLevel = g_GUI.AddComboBox( "", GUI::Rectangle( 0, m_WindowSelectLevel->GetSize().y - m_HeightComboBoxSelectLevel, m_WindowSelectLevel->GetSize().x, m_HeightComboBoxSelectLevel ), m_WindowNameSelectLevel ); m_ComboBoxSelectLevel->SetBackgroundImage( m_TextureNameSelectLevel ); m_ComboBoxSelectLevel->SetItemBackgroundImage( m_TextureNameSelectLevel ); g_Script.Register( m_ScriptClickLevelChange.c_str(), [this](IScriptEngine*){ SetLevel(m_ComboBoxSelectLevel->GetTextDefinitionRef().Text); return 0; } ); m_ComboBoxSelectLevel->SetClickScript( m_ScriptClickLevelChange + "()" ); m_SizeLevelWindow = std::min(slSize.x, slSize.y - m_HeightComboBoxSelectLevel); m_WindowLevel = g_GUI.AddWindow( m_WindowNameLevel, GUI::Rectangle( (slSize.x / 2) - (m_SizeLevelWindow / 2), 0, m_SizeLevelWindow, m_SizeLevelWindow ), m_WindowNameSelectLevel ); m_WindowLevel->ToggleOpen( ); m_SpriteLevelSelected = g_GUI.AddSprite( "", GUI::SpriteDefinition( "", 0, 0, m_SizeLevelWindow, m_SizeLevelWindow), m_WindowNameLevel ); CreateSpawnPointButtons( ); LoadLevels( ); } { // Gameplay settings int gpsX = sizeLobby.x / 2; int gpsY = sizeLobby.y / 2; int gpsW = sizeLobby.x / 2; int gpsH = sizeLobby.y / 2; int gpsDiff = m_HeightComboBoxSelectLevel; int gpsStartCountW = gpsW - gpsDiff * 4 - gpsDiff * 3; m_WindowGamePlaySettings = g_GUI.AddWindow(m_WindowNameGamePlaySettings, GUI::Rectangle(gpsX, gpsY, gpsW, gpsH -m_HeightButton), m_WindowNameLobby, true); m_WindowGamePlaySettings->ToggleOpen(); // | Starting units count | m_BtnSpawnCount = g_GUI.AddButton("GamePlaySettingStartCount", GUI::Rectangle(gpsDiff, gpsDiff, gpsStartCountW, gpsDiff), m_WindowNameGamePlaySettings); m_BtnSpawnCount->GetBackgroundRef().Texture = m_TextureNameButtons; m_BtnSpawnCount->SetText("Starting units count"); m_BtnSpawnCount->SetEnabled(false); // | Starting units count | - | m_BtnSpawnCountDec = g_GUI.AddButton("GamePlaySettingStartCountModDec", GUI::Rectangle(gpsStartCountW + gpsDiff, gpsDiff, gpsDiff, gpsDiff), m_WindowNameGamePlaySettings); m_BtnSpawnCountDec->GetBackgroundRef().Texture = m_TextureNameButtons; m_BtnSpawnCountDec->SetText("-"); m_BtnSpawnCountDec->SetClickScript(m_ScriptChangeGameSetting + "(-1)"); // | Starting units count | - | X | m_BtnSpawnCountVal = g_GUI.AddButton("GamePlaySettingStartCountVal", GUI::Rectangle(gpsStartCountW + gpsDiff * 2, gpsDiff, gpsDiff, gpsDiff), m_WindowNameGamePlaySettings); m_BtnSpawnCountVal->GetBackgroundRef().Texture = m_TextureNameButtons; m_BtnSpawnCountVal->SetText("X"); m_BtnSpawnCountVal->SetEnabled(false); // | Starting units count | - | X | + | m_BtnSpawnCountInc = g_GUI.AddButton("GamePlaySettingStartCountModInc", GUI::Rectangle(gpsStartCountW + gpsDiff * 3, gpsDiff, gpsDiff, gpsDiff), m_WindowNameGamePlaySettings); m_BtnSpawnCountInc->GetBackgroundRef().Texture = m_TextureNameButtons; m_BtnSpawnCountInc->SetText("+"); m_BtnSpawnCountInc->SetClickScript(m_ScriptChangeGameSetting + "(1)"); } } }