Using gazebo with drake for quadrotor - c++

My objective is to use finite horizon linear quadratic regulator(FHLQR) to follow a trajectory generated by a mathematical program and simulate it in Gazebo. As FHLQR needs a system as input, I'm using the QuadrotorPlant, but I'm not sure if this is ideal. My problems arise when I try to connect the state from Gazebo into Drake. In short, what would be the proper way of coupling state from Gazebo with a controller such as FHLQR?
I've thought about editing the context of QuadrotorPlant to mirror the state in Gazebo, but after this update I'm having trouble getting control output from the controller. I've also thought about coupling the Simulator between the output of the controller and input of the QuadrotorPlant, but haven't figured out how to modify the Simulator to mirror Gazebo.

And for your gazebo interface, and assuming you're all in c++, then I'd imagine it will look something like this:
// setup
auto regulator = MakeFiniteHorizonLinearQuadraticRegulator(...);
auto context = regulator.CreateDefaultContext();
// during execution
context->SetTime(time_from_gazebo);
context->FixInputPort(0, Eigen::VectorXd([state obtained from gazebo]));
Eigen::VectorXd u = regulator->get_output_port(0)->Eval(context);
// then apply u to your gazebo interface

I think we should be able to help, but probably need a few more details.
My mental model of your setup is that you use QuadrotorPlant to design the trajectory and the MakeFiniteHorizonLinearQuadraticRegulator methods in drake... the result of that is a drake System that has an input port expecting a vector of doubles representing the state of the quadrotor, and it outputs a vector of doubles that represent the thrust commands for the quadrotor.
Separately, you have a Gazebo simulator with a quadrotor, that is accepts commands and simulates the dynamics.
Are you trying to connect them via message passing (as opposed to a single executable)? Presumably with ROS1 messages? (If yes, then we have little systems in drake that can send/receive ROS messages, which we can point you to)
FWIW, I would definitely recommend trying the workflow with just running an LQR controller in drake before trying the trajectory version.

Related

UE4 Custom Dedicated Server (collision, hitboxes)

So I am currently developing a custom dedicated server in UE4 from scratch. I use RakNet as a networking engine and want to achieve the typical dedicated server that can manage adding players to the world, hitboxes, collision and verifying packets in general. What I mean by verifying here is e.g. if the server receives a movement packet it can decide whether the packet's sender can move there or not. I see the biggest trouble in verifying the e.g. movement packets and hitboxes because the server has to have access to the world and collision methods of unreal which I don't know how to do from scratch. Is it possible to get the base server from UE4 and equip it with your own networking engine and packet handling? Or is there a better way to achieve my goal?
AFAIK there is no out of the box solution for that. You need something custom. I can think of 3 solutions:
Just ignore collisions. Replicate what you get from clients, and that's it. Of course this is a cheap, low effort solution prone to hacking, but maybe it's sufficient for your case ?
Write custom collision handling on your server. This is a lot of work. Server would need to know all the colliders positions, dimensions, etc. You could then use a physics library ( i.e. Bullet Physics ) to check collisions or write a custom collision checks.
Compile UE4 as a dedicated server. You can find more info here:
https://wiki.unrealengine.com/Dedicated_Server_Guide_(Windows_%26_Linux)
Just use Raknet instead of a native UE4 networking.
PS. UE4 source code is over 2 milion lines of sophisticated C++ code so telling someone to "just download the source code and change anything you like" is not a viable advice.
PS.PS. I'm not sure if RakNet is a good choice at the present moment, this project was not updated since 5 years.

Tracking another drone with Opencv and Pixhawk2

I am working in a UAV-Team and my task is following and locking another UAV autonomously. I coded my opencv part and I used background substitution method and several filters. I can get absolute result from my code (like to forward go left , go right). My question is how can I send this result to the UAV's motors. How can I communicate with my UAV with C++? I've read lots of documentation from ardupilot, ardurov, opencv and pixhawk. But still couldn't figure it out.
Connect your microprocessor(i.e rasberry Pi) with pixhawk ans use mavlink communication protocols to send command here is link
And you can use dronekit also to do that.

How could components communicate effectively with each other?

I'm designing a component-based system and everything works well, but a critical feature is missing and that is to be able to obtain a type of component from within a class of type Object, whereof this class can be added/removed components. In the Object class there exists a vector of components thus:
vector<Component*> pComponents;
And in the Component class, a Component has to have a name. So, a component such as Drawable would be called like so:
pPlayer->addComponent(new Drawable("Drawable"));
And that's all that's required for the player to be drawable. Now this is the problem: when it comes to adding loads of components that rely on other components, is there a settlement in how components communicate with one another?
Currently, in my Game class (which is not of type Object, although I might have it derive from Object albeit not sure if that's a good design decision) I have this code in the update function:
void Game::update()
{
pPlayer->update(0);
pSpriteLoader->getSprite()->move(pPlayer->getVelocity());
}
I'm using SFML2 solely because it's easy to use for the 2D graphics. The player update function calls the components' respective update functions. And the sprite loader is also a component and it is in charge of handling the loading of sprites through textures/images that can be read from file or memory. If I were to omit this line of code then the sprite would not be able to appear moving on the screen. As you can see, it's odd that pPlayer has a getVelocity() function and it's because I haven't moved all the physics stuff into its own component. What is dreading is that once I have moved the physics stuff out of the Player class into a Physical component class, how can I get these components to communicate with each other without having to resort to the lines of code ascribed above?
My solution is to create a component manager which registers each component and any component that requires another component will have to consult with this component manager without having to do so directly. Would this be a viable solution, and how can I proceed with the logic of such a component manager then?
Well, I suppose you would start by designing a messaging system.
It appears that you want to heavily decouple code and create components as much as possible. This is fine, I suppose, but the answer to having dependencies without coupling is probably something among the lines of message passing.
Say you need an Achievement system. This is a perfect example of a system that needs to stick its hand into as many nooks and crannies as possible in order to allow for the greatest flexibility in designing achievements. But how would you be able to stick your hand into, say, the Physics, AI, and Input system all at the same time and not write spaghetti code? The answer would be to put listeners on event queues and then run them by certain criteria based on the contents of the messages.
So for each component, you might want to inherit a common message sending/receiving component, possibly with generics, in order to send data messages. For example, say you shoot a laser in a FPS game. The laser will most likely make a sound, and the laser will most likely need an animation. You will probably want to send a message to the sound system to play a certain sound, and then send a message to the physics system or such to simulate the effects of the laser.
If you're interested, I have a really, really crude library for modeling a event system based on queues, listeners, and generic messages here: https://github.com/VermillionAzure/Flexiglass You might get some insight from fellow newbie code.
I really suggest taking a look at Boost.Signals2 or Boost.Asio as well. Knowledge of signals/networking can often help when designing communication systems, even if the system is just between game components.
I've recently been working on an entity-component system in SFML and came across the same problem.
Instead of creating some sort of messaging system that allows components to communicate with each other I ended up adding 'System' objects to the mix instead. This is another popular method that is often used when implementing component systems and it's the most flexible one that I've used so far.
Entities are nothing more than collections of components
Components are POD structs and have no methods (e.g. a PositionComponent would have nothing more than X and Y values)
Systems update any entities that have the required components
For example, my 'MovementSystem' iterates through each of my entities and checks if they have a VelocityComponent and an InputComponent. If it does, it changes the velocity of the current entity according to the current key being pressed.
This removes the issue with components communicating with each other because now all you need to do is access/modify the data stored in the current entity's components.
There are a few different ways that you can work out whether or not the current entity has the required components - I'm using bitmasks. If you decide to do the same, I highly suggest you take a look at this post for a more thorough explanation of the method: https://gamedev.stackexchange.com/questions/31473/role-of-systems-in-entity-systems-architecture

Running pygame on a cloud server generates error regarding sound card even though my code doesn't implement sound [duplicate]

I need to do some basic networking for a Pygame project.
Basically, it's a 2D single player or cooperative game. The networking only needs to support two players, with one as a host.
The only information that needs to be sent is the positions of players, creeps and bullets.
I've been reading around and Twisted keeps coming up, but I haven't done networking before, and I'm not sure if that might be an overkill.
So, is it possible for a relative newbie to implement networking in Pygame?
This was asked recently on Reddit, so I'll more or less just copy my answer over from there. I apologize for not being able to provide more links, I have <10 rep so I can only post two at a time.
Twisted might work, but I don't have a whole lot of experience with it. I'd recommend going with sockets, as that's what Twisted uses in the background anyway. Beej's guide (google it) is pretty much the Holy Bible of sockets if you want to learn how they work (in C++, but the concepts extend everywhere). Python does abstract some of the complexity away, but it's still a good idea to know what's going on in the background.
For Python specific sockets, you can go ahead and just use the howto (user745294 posted a link above). Here's a nice article titled "What every programmer needs to know about Game Networking". It goes into the different types of major networking styles (client-server, p2p, udp v. tcp, etc.) and the history behind what some major games used for their networking.
Below is a link to a demo I did on making a networked "game" in Python 2.6/Pygame. It's not actually a game, but each client you create connects to the server and controls a character. You can move your character with the arrow keys and the character will move on all connected clients. I tried commenting the source code with some indication of what I'm sending back and forth, but you may need a little knowledge about sockets to understand it.
The source code is provided in the codepad links in the comment below this post. You will need to provide two images in the same directory as the scripts:
bg.png is the background sprite. It should be an image 400px wide and 300px tall (this can be changed in the GameClient class if needed)
sprite.png is the player character. It should be smaller than the background so that you can see it moving around.
You can use Twisted for networking with Pygame. The "game" project on Launchpad has some examples of how one might integrate the main loops together; basically, use twisted.internet.task.LoopingCall to draw Pygame frames and handle input, while letting the Twisted reactor of your choice run normally.
Since you are already using Pygame, I think this light networking library made for Pygame will do what you need and teach you, but not overwhelm you.
"Mastermind Networking Lib" via pygame.org
There is Pyro (Python remote objects) as another solution for networking in Python.
http://irmen.home.xs4all.nl/pyro/
Using raw sockets is low-level and full of danger. As said before, Twisted is complex and takes to time get up and running. To save yourself some headaches I'd try something like zerorpc.
You need the following solutions:
discovering other player(s) on the (local) network, you don't want player to enter some IP address
handle network errors
serialize messages containing your data (positions, player name, etc.)
handle threading as networking is asynchronous I/O
Above should still be called 'basic', you should really use some fancy networking library with idiomatic API.
Essentially you need to expose the network service (in its own thread) that will push messages to Python's Queue, and then access this same queue from your Pygame code, and if there is a message then you update whatever structures you use to store player's position and draw it on screen.
You shouldn't send stuff like bullet positions over the network as they can be easily (and faster) calculated locally. You just send an event like bullet_shot over the network with a source position and velocity vector.

creating realtime 2D maps of indoor environment

I am using ultrasonic sensors to get the data about the indoor environment i.e the distance of the from each wall . I want draw a realtime map of the surronding environment using that can u please guide that which software or which library is best suited for this purpose
Try OpenCV. It has everything you need for image processing. Could be an overkill tho, but I wouldn't mind it.
http://opencv.org/
If you want realtime map, I'd use Labview->embedded MATLAB code / C++. Labview has excellent hardware/peripheral support and operations, and you can embed custom code in the signal flow representation.
If you are using something like arduino or raspberry or any other such electronics kit to capture the ultrasound data, you can send the data to the serial port and then use Matlab to read from that serial port and use it to plot a live map. A good step by step example of how this can be done is available at Matlab Arduino org website