|
Nice! What are their mocking abilities?
|
|
|
|
|
you can mock and fake almost everything, any kind of
methods,
fields,
behaviors,
interfaces,
dependencies,
Instances, and more.
|
|
|
|
|
|
I have this code, which works on CodeBlocks, but when I try to run it in visual studio code, I get and error saying:
invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char, char_traits<char> >') and 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >'))
this is the code:
#include <iostream>
using namespace std;
class field{
public:
void Create_Area(int area){
string field[area][area];
for(int y = 0; y < area; y++){
for(int x = 0; x < area; x++){
field[y][x] = "+ ";
cout << field[y][x];
}
cout << endl;
}
}
};
What to do to fix this?
|
|
|
|
|
You cannot create an array on the stack using values that are not known at compile time. You need to change your code to:
void Create_Area(int area){
string field[][] = new string[area][area];
Also, I would suggest not using the class name for an array in that way. And the field variable is not available outside of the method that creates it.
|
|
|
|
|
I have changed that, but now how do I use it in this part?
Field[y][x] = "+ ";
cout << Field[y][x];
also, I don't really understand what you have done in the new string[area][area] part. Why use string instead of field?
|
|
|
|
|
Member 13802912 wrote: how do I use it in this part? Exactly the same as before.
Member 13802912 wrote: the new string[area][area] part. Why use string instead of field? Because you are trying to create an array of string objects. See new Operator (C++)[^].
|
|
|
|
|
fatal error: invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>') and 'void')
this is the error i am getting.What should i do?
#include <iostream>
void Find(int arr[], int n, int sum)
{
unordered_set<int> s;
int temp;
for (int i = 0; i < n; i++)
{
temp = sum - arr[i];
if (s.find(temp) != s.end())
cout<< arr[i]<<"+"<< temp<<" ";
s.insert(arr[i]);
}
}
int main()
{
int arr[] = {1,2,3,4,5,6,6,5,56,7,11,13} ;
int n = sizeof(arr)/sizeof(arr[0]);
int sum = 12;
cout<<Find(arr,n,sum);
return 0;
}
|
|
|
|
|
You have declared:
void Find(int arr[], int n, int sum)
cout<<Find(arr,n,sum);
But you cannot print something that is void , i.e something that does not exist.
|
|
|
|
|
So what should i do
Instead of void i used
int Find()
{....
return 0;
}
But at the end of my input extra zero is coming which i dont want.
Thank you!
|
|
|
|
|
Then don't return a value. Leave it as void and remove the cout call at the end of your main method.
BTW in future please open a new question if you have a problem, rather than reopening a thread that is over two years old.
|
|
|
|
|
I hope that someone can help me.
I am currently working on developing a flight simulator with force feedback. To do the calculations of the force feedback, we need to know the position of vertical yoke that we are using. We use two potentiometers to detect the position of the yoke for roll and pitch. The potentiometers are connected with Arduino Uno that will change analog values to digital values:
Digital value of roll will be between [450: 650]
Digital value of pitch will be between [100: 340]
These values then will be sent to Raspberry Pi where we do the calculations of force feedback. I’m using the RS232 protocol (library developed by Teunis van Beelen [link]) to receive the data from Arduino.I'm using a usb cable to send data from Arduino To RPi.
I’m developing the code in Qt Creator in C++
My problem: I received the data packets from Arduino every 10ms, BUT, sometimes the packets are cut into two parts (see figure 1). I can say that for every 100 data packets sent, 6 will be cut. So I will receive 6 times incomplete data packets for the calculations. The "incomplete part" will come together with the next data packet.
For the time being, if the data was cut, the code will just "jump" the data packet and will not do any calculation. But, for optimisation purpose, I need to resolve this problem. By the way, the baudrate is the same for Arduino and RPi that is 115200. The whole program is being developed in C++, so it is not advisable to use pyserial because the program need to receive and calculate the force feedback within 10ms.
[figure1]
Can someone explains why and how did these happen? What do I need to do to resolve this problem?
My code in Arduino
int pinpotRoulis=A0; int valpotRoulis=0;
int valmancheRoulis=0;
int mapRoulis =0;
int pinpotTangage=A5; int valpotTangage=0;
int valmancheTangage=0;
int mapTangage =0;
int voltage = 8;
void setup() {
Serial.begin(115200);
pinMode(voltage,OUTPUT);
digitalWrite(voltage,HIGH);
}
void loop() {
char dataToSend[20];
valpotRoulis = analogRead(pinpotRoulis);
valpotTangage = analogRead(pinpotTangage);
sprintf(dataToSend, "R%dE T%dE", valpotRoulis, valpotTangage);
Serial.println(dataToSend);
printf("%s\n", dataToSend);
delay(10);
}
My code in QtCreator in RPi :
void MainWindow::getPotentiometreFFB(){
do{
n = RS232_PollComport(cport_nr, (unsigned char *) str_recv, (int)BUF_SIZE);
i++;
}while(n <= 0 && i < 1000000);
if(n > 0){
str_recv[n] = 0;
printf("Received %i bytes: '%s'\n", n, str_recv);
position = -1;
for (int j = 0; j < n ; j++)
{
if(str_recv[j]=='R') {
position = j;
break;
}
position = -1;
}
if (position != -1) {
for (int k = 0;k<5;k++)
{
valeur_R[k] = str_recv[position+k];
}
char * ptrsuite_1Chaine = NULL;
ptrsuite_1Chaine = strchr(valeur_R, 'E');
if (ptrsuite_1Chaine != NULL)
{
sscanf(valeur_R,"R%dE",&valRoulis);
valRoulis = minMaxPotentiometre(450, 650, valRoulis);
m_data.roll_cmd = mappingPotentiometreRoulis(valRoulis);
}
}
position = -1;
for (int j = 0; j < n ; j++)
{
if(str_recv[j]=='T') {
position = j;
break;
}
position = -1;
}
if (position != -1) {
for (int k = 0;k<5;k++)
{
valeur_T[k] = str_recv[position+k];
}
char * ptrsuite_2Chaine = NULL;
ptrsuite_2Chaine = strchr(valeur_T, 'E');
if (ptrsuite_2Chaine != NULL)
{
sscanf(valeur_T,"T%dE",&valTangage);
valTangage = minMaxPotentiometre(100, 340, valTangage);
m_data.pitch_cmd = -mappingPotentiometreTangage(valTangage);
}
}
cout<< "Val roulis :" << m_data.roll_cmd << endl;
cout<< "Val tangage :" << m_data.pitch_cmd << endl;
ui->roll_cmd->setValue(m_data.roll_cmd);
ui->pitch_cmd->setValue(m_data.pitch_cmd);
}else{
cout << "No data Roulis tangage" << endl;
}
}
Feel free to ask if you guys need more info from me.
Thank you very much
|
|
|
|
|
The answer is obvious:
You have to rewrite your code to be able to handle incomplete packages.
You got incomplete packets because you stop receiving if any number of data has been received (even a single byte/character). That would be the normal behaviour. The only reason why that happens not very often is that the system has to service also other tasks. And that is not done while waiting (see end of my post)!
Because you are sending only a single kind of data, you can change the data being send using a fixed length format by using leading zeroes:
sprintf(dataToSend, "R%03dE T%03dE", valpotRoulis, valpotTangage); That makes parsing much simpler (e.g. using scanf() ) and you can wait for the known number of characters (11 + line feed):
const int CMD_LENGTH = 12;
while(n < CMD_LENGTH && i < 1000000); By the way: what is the purpose of the undefined variable i here?
But when doing so you will run more into the already existing performance problem of using a polling loop. Waiting for the data wastes a lot of time (10 ms) that is not available for other tasks.
The solution is to put the receiving into an own thread and use the fine Qt signaling features. Because there are only two event data values, there is even no need to use shared variables.
|
|
|
|
|
Jochen Arndt wrote: That makes parsing much simpler (e.g. using scanf() ) and you can wait for the known number of characters (11 + line feed):
const int CMD_LENGTH = 12;
while(n < CMD_LENGTH && i < 1000000); By the way: what is the purpose of the undefined variable i here?
I don't really understand what you are doing here? Can you please explain it a bit more and detailed? And where do i need to put this code, is it after I received the data from Arduino (after printf to see how many data in the string?). By the way, i just started coding two months ago. I'm sorry if I asked a lot of questions.
do{
n = RS232_PollComport(cport_nr, (unsigned char *) str_recv, (int)BUF_SIZE);
i++;
}while(n <= 0 && i < 1000000);
if(n > 0){
str_recv[n] = 0;
printf("Received %i bytes: '%s'\n", n, str_recv);
Jochen Arndt wrote: But when doing so you will run more into the already existing performance problem of using a polling loop. Waiting for the data wastes a lot of time (10 ms) that is not available for other tasks.
The solution is to put the receiving into an own thread and use the fine Qt signaling features. Because there are only two event data values, there is even no need to use shared variables.
Like you said, I would prefer to not use polling because the program needs to be really dynamic(10 ms).
When you said "put the receiving into an own thread", do you mean put the received data in another string?
I don't really know about the Qt signaling features. I just took a hold in this project two months ago and just started doing Qt 2 weeks ago. Would you give me a link that would help me to understand the features and why and how to use them?
Tqvm
|
|
|
|
|
When you send data with the format "R%03dE T%03dE" it will be of fixed length like in "R450E T030E". The length is 11 characters plus the the line termination appended Serial.println() . Assuming that it appends a single LF and not a CR-LF pair, the length will be 12 bytes.
Knowing that you can implement reading until all bytes of a package has been received (untested from scratch):
const int packetLength = 12;
int packetRead = 0;
while (packetRead < packetLength)
{
int read = RS232_PollComport(cport_nr, (unsigned char *)str_recv + packetRead, (int)BUF_SIZE - packetRead);
if (read < 0)
{
return;
}
packetRead += read;
}
str_recv[packetRead - 1] = 0;
char cmd1, cmd2;
int val1, val2
sscanf(str_recv, "%c%03dE %c%03dE", &cmd1, &val1, &cmd2, &val2);
if ('R' == cmd1)
position = val1;
else
position = -1;
An application can be splitted into multiple threads. You typically have the main thread that is doing the user interaction and screen outout (often also called GUI thread) and background threads that perform asnychronous operations like networking or serial communication as in your case.
If you execute such polling operations (or calling sleep() ) within the main thread that will be blocked for long times resulting in lags and stuttering screen update. Note also the error check in the above code. Without that your application would stuck forever when the serial connection is interrupted by unplugging the cable or switching the Arduino off.
The solution is to create a worker thread that receives the data and uses events to signal the main (GUI) thread when new data has been received. The main thread then handles the event and can update the screen with the new data.
However, using threads is an advance programming topic so you might proceed with the polling for testing the communication. But sooner or later you would have to use threads. The thread itself can be implemented as pthread or QThread Class | Qt Core 5.10[^]. Inside the worker thread use a QEvent Class | Qt Core 5.10[^] to signal new data to the main thread.
In any case read the Qt documentation for the used functions which includes example code. You might also search the web for additional information. The Terminal Example | Qt Serial Port 5.10[^] and Blocking Slave Example | Qt Serial Port 5.10[^] might be for example of interest.
|
|
|
|
|
This is a common issue with communication lines. When you read the incoming data you cannot assume that every message will contain everything that was sent from the other end. You need to read however many bytes are presented in a loop, and build the message as you go. How you check for a complete message depends on the protocol you are using.
|
|
|
|
|
The problem is simple Serial.println won't necessarily clear the buffer in your small 10ms delay.
There is a guaranteed Line Feed character "\n" on the output with this line
Serial.println(dataToSend);
So you have a guaranteed packet end marker. On that poll routine you could simply also insert a timeout error handler.
However the only basic requirement is the read poll loops keeping all characters it see's before a LF character.
At the moment your crazy reading poll runs around a million times instead of just looking for the LF character
In vino veritas
modified 25-Apr-18 23:58pm.
|
|
|
|
|
This is a Linux question, hope it is OK to ask here.
I am asking here since I am getting no response from original article author nor Linux forum.
I am "discovering" variety of Linux ways to interface with the world.
I do not particularly want to call these ways "modules".
I am into using "ioctl" and having some success coding "dev" individually.
Each "dev" has decent Linux documentation which I am using.
The real question - how does "dev" framebuffer gets to output to "dev" SPI?
I need some general comments , not particularity code samples.
Appreciate any help, but if Linux questions are not appropriate here , just ignore me.
Cheers.
|
|
|
|
|
Quote: The real question - how does "dev" framebuffer gets to output to "dev" SPI? By using a framebuffer driver for your SPI connected display. That driver will create the framebuffer device and use SPI for the communication with the display. This requires that the SPI driver has been loaded first so that the framebuffer driver can access the SPI interface.
If you want to know how it works, have a look at the sources of the driver of your display. Most SPI displays for the Raspberry Pi use the generic Home · notro/fbtft Wiki · GitHub[^] driver which has been meanwhile merged to the upstream kernel tree and is part of recent Raspbian versions.
|
|
|
|
|
Does that mean that I cannot use ioctl ?
I have a code for each ioctl "dev" individually and it sort of works.
I am having little problem verifying that the device is actually being physically accessed using ioctl - and my scope just broke!
|
|
|
|
|
You can use ioctl() .
But if you have a framebuffer device for an SPI display it is much easier to use that instead of communicating directly with the display using SPI.
|
|
|
|
|
That is the issue.
I do not know - all I am using now is what this call returns
system("ls -l /dev/fb*"); which is only fb0.
I think I need to go back to Raspberry OS to make sure where is "fb0" connected.
That part is still not clear - to what device fb0 outputs.
I have only one monitor I can physically connect at a time.
I suspect this fb0 is sending the output to HDMI port on RPi.
Thanks for your inputs, appreciate that.
|
|
|
|
|
fb0 is the HDMI port on RaspberryPi. If there is no fb1 you have to check your setup for the SPI display. It should be part of the documentation for your display. Start by loading the modules for SPI and the display manually. Once that works use the device tree to load the modules.
The above applies to displays using fbtft module. Otherwise you have to use a compiled module supplied by the display manufacturer (which is always for a specific kernel / Raspbian version) or build it for your kernel.
|
|
|
|
|
Been thinking same way, so far do not see much of configuring RPi for fb1.
Will keep looking for "how to " add more fbx to RPi.
Just found an interesting tidbit - there is a real driver (fb?) called fbtft.
However after more reading I also found that this "driver" is part of the latest Linux and is no longer actively developed. It does "reroute" framebuffer from fb0 to fb1.
That is NOT what I am after - I want to use fbx and SPI on selected fbx.
Back to research.
-- modified 23-Apr-18 20:08pm.
|
|
|
|
|
That is what I told you in my first post:
There is the fbtft driver that supports most of the SPI displays. It is part of the kernel tree since 2015 and included with recent Rasbian versions.
It is still maintained but now as part of the kernel. I provided the original GitHub link because it does not only contain the sources (the actual sources can be found in the kernel sources) but also some documentation.
It does not reroute any fb. It creates /dev/fb1 when properly configured for an attached display. But there are options to make it the default display when booting.
So you have the RPi display. The name RPi was initially used by Watterott. If you have that display, see RPi-Display | Watterott electronic[^] and select FBTFT Installation in the menu on the left side.
Use the provided script method or do it manually with a recent Raspbian version. For manual install see the sections FBTFT compiled into Kernel (BRANCH=builtin) (when using such a kernel) and FBTFT Device Tree enabled Kernel (works always with recent Raspbian versions).
|
|
|
|
|