Does anyone have an example of how to use IQueue in codesys? - codesys

I'm trying to get the median value of an array in codesys, however the array is fifo, so as a new value is added to the array the first value is deleted and the median is rechecked. The array is always going to be odd in size and I think IQueue will be useful but the codesys sp16 documentation are not very descriptive about how to use it. Any ideas?

The functionblock Queue implements the interface iqueue. See here for more documentation: https://content.helpme-codesys.com/en/libs/Element%20Collections/Current/ElementCollections/Function-Blocks/Queue/Queue.html
In my opinion the Queue could be used to manage adding and removing values with fifo. However to calculate the median value a seperate function is needed that just iterates over all the values in my opinion. The library 'ElementCollections' does not facilitate automatic median value calculation.

Related

How can I keep record (or see) of what value was given by the distribution each time the process took place?

I have beeen constructing a model in Anylogic in the last weeks, and I am currently simulating the time a truck takes to deliver its products, so I used a delay for this in which different parameters are multiplied to several distributions. Is there any was I can keep track of the value of the distribution each time the process takes place. The following is in example:
normal(2, 8, 4.67, 1.96)*DropSize
The DropSize is my parameter, but I wish to know what value was generated for the normal distribution, and keep track of this.
Sure, several ways (as usual with AnyLogic :-) ). Here is one:
create a collection of type ArrayList:
Then, create a function that draws the random value, stores it in the collection and returns it as below:
Last, replace your code creating the random value with calling that function. Now, whenever you pull a value from the distribution, it is also stored in the collection.
cheers

Determine Remaining Bytes

I'm working on a project where I need to send a value between two pieces of hardware using CoDeSys. The comms system in use is CAN and is only capable of transmitting in Bytes, making the maximum value 255.
I need to send a value higher than 255, I'm capable of splitting this over more than one byte and reconstructing it on the receiving machine to get the original value.
I'm thinking I can divide the REAL value by 255 and if the result is over 1 then deconstruct the value in to one byte holding the remainders and one byte holding the amount of 255's in the whole number.
For example 355 would amount to one byte of 100 and another of 1.
Whilst I can describe this, I'm having a really hard time figuring out how to actually write this in logic.
Can anyone help here?
This is all handled for you in CoDeSys if I understand you correctly.
1. CAN - Yes it's in byte but you must not be using CANopen you are using the low level FB that ask you to send a CAN frame of an 8 byte array?
If it is your own two custom controllers ( you are programming both of them in CoDeSys) just use netvariables. Netvariables allows you to transfer any type of variable and you can take the variable list from one controller and import it to another controller and all the data will show up. You don't have to do any variable manipulation it's handle under the hood for you. But I don't know the specifics of your system and what you are trying to do.
If you are trying to deconstruct construct variables from one size to another that is easy and I can share that code with you.

Efficient way to get a random element in Scala?

What is an efficient way to get a random element from a collection in Scala? There's a related question here, but like one of the comments pointed out, "[that] question does not specify any efficiency needs".
An arbitrary collection cannot be accessed in constant time. So you need some special collection with the desired property. For instance — Vector or Array. See Performance Characteristics of collections for others.
util.Random.shuffle(List.range(1,100)) take 3
Use a collection with a constant-time size() and get() method.
If you need random order of all collection elements, then Random.shuffle is what you need. (You'd better convert the original collection to array to avoid forward and backward conversion.)

best way of handling self-changing array of information

This question is about handling arrays of information, there's are many ways I could do this, but I would like some input from programmers with more experience, I know what I want to do just not how to organize the information the best way, and objective-C is really making me ponder this, I don't want to get 100 hours into work a decide, oops this wasted the beast way to do this. So here goes:
I have a grid where I'm simulating a playing field, each piece of the grid I call a cell. The cells have around 20 different values each, all integers, nothing fancy. A change to a cell will be either by player input, or occur or by surrounding cells through different algorithms.
The changes to cells will occur once a turn is complete, so it's not real time. Now, I'm not even sure about doing this with a MutableArrays, a plain Array, or just a plain matrix. Arrays are good at keeping such info for one dimension, but I would imagine would become quite cumbersome if you have to address a batch of 10,000 of these cells. On the other hand a simple matrix might not be so elegant, but probably easier to work with.
Any insight would be greatly appreciated.
You have two options here that I see:
1) Use standard containers
Assuming that the playing field is of constant size, then you can create a mutable array of x*y size, and populate it with mutable dictionaries. By giving everything in the second mutable dictionary keys, you can query and set their properties (all objects of course, so wrap ints in NSNumbers etc). For indexing use a macro INDEX_FROM_ROW_COL(row, col) and apply the appropriate code to multiply/add.
2) Create a helper object subclassed from NSObject. It would manage mutable objects as above, but you could load it with functionality specific to your application. You could provide methods that have parameters of "row:" and "col:". Methods that change or set properties of each cell based on some criteria. Personally, I think this is a better idea as you can incapsulate logic here and make the interface to it more high level. It will make it easier to log whats going on too.

Lisp Code Unexpected Results

I am trying to solve a homework problem where I have to return a selected users' grades in
order by course number (not allowed to use built-in sort function). I don't understand the results: the first entry isn't sorted, and some extra students seem to be returned. I don't know why and I spent over three hours trying to solve this one problem. Thanks.
A good start would be to get rid of functions like car, cdr, cadar, ...
Write access functions for the data records. Use first, second and third.
For accessing the list's first element use the function FIRST.
For accessing the rest of the elements use the function REST.
This makes the code easier to read and understand.