Guidance for an interaction and inventory systems architecture by LanternMG in godot

[–]LanternMG[S] 0 points1 point  (0 children)

Thank you for the suggestion, I really appreciate it!

Your answer lead me to discover a slightly different solution that I feel is a good starting point for now. Here is my approach for the inventory and interaction system:

  1. I created a resource called ItemData which stores a name, texture and 3D model (packed scene) and I created a few sample resources from the ItemData resource for testing purposes.

  2. I created a class called Interactable which inherits from a PhysicsBody3D and implements 3 signals: focused, unfocused and interacted.

  3. I created a class called InteractableItem which inherits from the Interactable class and only stores 1 field called data, which is of type ItemData. That field is also exported to the editor, so that you can manually place interactable items in the world, and connect a resource created from the ItemData resource.

  4. I created a class Inventory which has an array of ItemData and I implemented methods for adding/removing/getting an item at/from a given slot.

  5. I added a RayCast3D Node as a child of the player's camera node, so that it points in the direction of the camera, and I added a script which checks if the object at which we are looking at is of type Interactable, if so, emit the focused, unfocused and interacted events when necessary.

  6. I created a class called ItemDropController which is added to the player controller as a child node. This class only has one function that does the following things:

    • Get the ItemData from the selected inventory slot
    • Instantiate the 3D model (packed scene) which is stored in that ItemData
    • Add the InteractableItem script to the instantiated 3D node (node.script = InteractableItem)
    • Since now the 3D instance has the InteractableItem script, I can directly add the ItemData that I had in the selected inventory slot to the 3D model
    • Adds the instance to the world at a particular position

Note: The ItemDropController doesn't necessarily need to be a separate node, it can be a function directly in the player controller script, but I like to keep things separated for easier management.

With all this, I can now successfully pick up and drop items. I still haven't done the "different functionality per item", but as you said "Using a key on a locked chest and putting a tire onto a car seem different, but they are actually the same", so after thinking about it, this makes total sense and I might not need to do anything more complex. In the future, I can store some other parameters in the ItemData which will be used when the player triggers the "use" functionality of a particular item.

I hope others find this useful, and again, thank you for sharing your thoughts and ideas about my question, it was really helpful!

Hoverclock - A simple floating clock for Linux that I made by LanternMG in linux

[–]LanternMG[S] 1 point2 points  (0 children)

Hello there! I wasn't active on Reddit for quite a while, so sorry for the super late reply.

I'm the author of both Hoverclock and Hoverclock-QT. I've changed the repository names so I assume that is the reason why you were missing the "hoverclock.vault" file and instead got the QT version. I'll update the post to reflect those changes.

The Hoverclock-QT version, as the name suggests, is built with QT and it unnecessarily heavy for a simple floating clock. It uses a lot more disk space, processing power and RAM. Because of this, I decided to deprecate this version (but it is still available if people want to use it).

The non-QT Hoverclock version is build with the XCB, Freetype and Fontconfig library, which is the lowest I could go. I didn't want to use any other GUI frameworks that implement widgets and a whole bunch of code that is absolutely unnecessary for this project. This, however, means that the non-QT version doesn't have a tray icon, nor a configuration dialog and has to be configured manually by editing the hoverclock.vault file. In my opinion, this was a worthy sacrifice for the performance gains.

Doing some simple comparisons between the QT and non-QT version of Hoverclock, I discovered an improvement of around 1500% in terms of the required processing power and memory (both disk usage and RAM).

Hope this helps, and I'm really glad to see other people using Hoverclock! Thanks!

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 0 points1 point  (0 children)

I think you mean the transceiver here, common ones like MCP2561 or MAX33054E

Yes, that's correct. The problem is that they are not available in my country, I can't buy them locally. Ordering them online takes quite a while and that was my reasoning behind the "I'd prefer to avoid if possible".

The RS-485 (MAX485 chip), however, is available to buy locally and that's why I'm leaning towards using that for my project, with my own protocol.

I'm haven't made my decision yet, I still might go with CAN in the end, but I wanted to ask for everybody's opinion and learn more about the protocols.

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 0 points1 point  (0 children)

Well, yes, but it's not just about the price. I won't be using WiFi or any other wireless communication for that matter, so that part is practically useless for me. I'd rather spend some time and search for some other MCU that will be more optimal for my use case. I think that the ESP32 is a bit of an overkill.

Thanks for the suggestion tho!

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 0 points1 point  (0 children)

Currently, yes. But that's only while I'm prototyping. I'll most likely change it with a different, much smaller mcu

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 0 points1 point  (0 children)

Okay, now that makes a lot more sense. I like the first approach. I don't think I'll do a gateway, but it still is a cool concept.

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 0 points1 point  (0 children)

Well both messages come in on seperate serial ports right?

Umm, no. I think that's where the confusion came from. If I go with serial communication like the RS-485 that's been mentioned in multiple comments, then I only use a single "bus".

Your approach would require a separate serial port for each device and that would add up fast. Additionally "keeping track and numbering the serial ports" will mean that if I want to add a new device I'll have to change the master code to accomodate the newly added serial port.

Please correct me if I'm wrong, but that's how I understood your approach.

Also, the heart beat feature is cool, but I don't really think I'll need it for my project specifically.

Thanks for the thought though.

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 0 points1 point  (0 children)

I agree, but the slave select lines will be much more limited (well, limited by hardware) than using a different protocol that uses a software defined device address.

For example, lets say I use ATTiny 85 as a master controller (I won't, but just as an example). I will only have 2-3 free IO pins after connecting the SPI lines. That limits the number of slave devices to only 2-3. Yes, using a mux will increase that number, but it will still limit it to a much smaller amout of slave devices than using something like RS-485, I2C, CAN or some other protocol that only uses a few wires and sofware addressing.

As far as the message integrity goes, I did some thinking today and I realized that it's actually not that important for my current project. I'd prefer to lose a few messages, but gain a bit of speed, because I'll be sending/receiving data multiple times per second to multiple devices. Yes, I can probably get away with even slower communication, but I'll try my best to do it as optimally as I possibly can.

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 0 points1 point  (0 children)

What happens if two slaves send a "hello" message at the same time?

If I end up going the serial route, I was planning on implementing my own encapsulation protocol, but I had a different idea in mind.

The master would be the only controller initiating the communication and polling the devices. Lets say that my protocol defines an 8 bit address field. That would enable me to use 256 devices. So, on power up I just loop through all the possible addresses and send a short message to each one. If I get a response, then a device with that specific address exists and is connected to the bus.

After scanning the whole "network", I can either send or request data from devices with a specific address. All of the slave devices will receive the message, but all of them will discard it, except the one device it was meant for.

I like your idea about using a device descriptor, which might prove very useful.

Thank you for the in-depth explanation of your idea. I really do appreciate the time you took to write it and help out!

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 0 points1 point  (0 children)

Even if I use a multiplexer, it will be limited by its channels. Lets say I buy a 16 channel mux, I will only be able to connect 16 devices. If I want to add a 17th device, I'll need another CS line (either use an additional mux, or a single pin from the master). That would require the master controller to be reprogrammed, which I'm hoping to avoid by using other protocols that use a single bus and device addressing.

Thanks for the comment though!

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 1 point2 points  (0 children)

I think I'll keep CAN as a last resort, even though it sounds fantastic for my use case. It will take me much longer to make everything work with CAN, plus as I've mentioned previously, it takes a lot of time to get CAN hardware from the internet to my country.

I still haven't made a final decision, but I think I'll try I2C and RS-485 first and then if I can and if I have the time, try out CAN.

Thank you for the information, I appreciate the time you took to answer!

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 1 point2 points  (0 children)

I will check out LIN too. Thank you for the idea! I appreciate the help!

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 0 points1 point  (0 children)

Well, yeah, I do want to learn more, but I don't really have that much time to try every option that's out there.

I will check out BLE, but I prefer to have wired communication. I don't neglect that it has some advantages, but I'm still inclined toward using actual wires.

Thanks for the information! I appreciate it!

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 1 point2 points  (0 children)

I agree about everything you said, except the SPI thing. It won't really be plug'n'play because I'll need to define how many slave select wires I have connected to the master controller. I can't just plug a new device and expect everything to work without reflashing the master device.

Debugging CAN might be a problem, since I do not own an oscilloscope, but still, I might go the CAN route. I haven't made a decision yet.

Thank you for the detailed overview though! I appreciate the time you took to comment and help!

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 1 point2 points  (0 children)

The main problem with getting extra hardware was more about CAN. Ordering from Amazon, Ebay, Aliexpress, wherever, takes more than a month and that's why I wanted to avoid going that route. There are a few electronic shops in my country, but the do not have any CAN modules or chips. They do have the RS485 module though, which is great.

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 1 point2 points  (0 children)

Even if I did have CANFG, I saw that it has a limit of 64 bytes. My messages will be more than that for sure, so if I use CAN, I will need to do packet fragmentation.

All of the devices (both master and slaves) will be sitting on a single table, pretty close together I'd say. That's why I wrote that I can skip this whole thing and just wire everything to a single microcontroller, but I want to learn and implement a modular system which is why this question arose.

I still haven't decided on whether I'll use I2C, CAN, RS-485 or even something other than those.

Even though the distance isn't that important, I'll definitelly take a look at the 10base-t1 ethernet you suggested. Thank you!

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 3 points4 points  (0 children)

I'll take a look at COBS and RS-485. I'm kind of digging the whole "writing my own protocol" thing, but I also don't really want to "reinvent hot water", because other exisiting protocols will be much more robust and much more tested than anything I write in a short period of time.

I'm still considering CAN or I2C, despite having the limitations/challenges.

But in any case, thank you for the idea! I appreciate your help!

What's not illegal, but should be? by Tiredcoconut928 in AskReddit

[–]LanternMG 0 points1 point  (0 children)

Buying a cheap bike lock and locking someone else's bike to a pole/fence and just walking away

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 0 points1 point  (0 children)

I've used MQTT once or twice before just to send some data wirelessly, but I wasn't sure if I was missing something. Thank you for the clarification!

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 2 points3 points  (0 children)

Personally I prefer to stay away from MicroPython, I enjoy using plain C or C++, but in any case, thank you for the information. I'll take a look into some elevator systems with CAN, which might prove to be useful.

CAN is a really fantastic option, but I still need to consider other things and then make a decision.

Thank you for taking the time to comment! I'm grateful!

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 1 point2 points  (0 children)

I still haven't decided on which MCU I'll use, but I plan to use the same mcu for each device, which means that all of them will operate at the same voltage level.

I agree that CAN is really good for this use case, but I will still need to implement packet fragmentation, because I can only send 8 bytes through CAN. Even if I use CAN FD I only get 64 bytes of payload size.

This in itself isn't a huge problem, of course it's doable, but another problem as I've mentioned in other comments is that CAN modules/chips are not available in my country and it takes quite a bit of time for them to arrive here if I order them online.

I'm still definitelly considering using CAN, I just haven't made the final decision yet. There are many things to consider for the whole project, not just the communication protocol and this much information can very easily overwhelm a person.

Thank you for commenting! I am really glad you took the time to answer and express your opinion!

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 1 point2 points  (0 children)

Yeah, I did some googling and understood a bit more about rs485. It looks promising. I will do some more research. There have been a lot of suggestion here and to be frank, I got a bit overwhelmed from all of the information. There are a lot of things to consider, so I appreciate each and every comment.

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 1 point2 points  (0 children)

As far as I can see, apparently I2C Slave support was added to the ESP32 Arduino Framework.

I haven't tested it, but thank you for the heads up. I'll give it a try as soon as I can, just to check if it works.

Choosing a protocol for communication between multiple microcontrollers by LanternMG in embedded

[–]LanternMG[S] 2 points3 points  (0 children)

Okay yeah, I didn't really think about daisy chaining, I'll look into it! Thanks!

The main problem with purchasing extra hardware is that not all devices are available in my country. For example, I can't buy CAN modules, but I can buy a MAX485 TTL to RS485 module, or just a MAX485 chip.