I am using CANoe 10 with LIN bus.
I want to measure the time from sleep command to wakeup(the first frame from the slave).
Here is a sample of the code that I made soo far in a test node
void MainTest ()
{
linGotoSleep();
testWaitForTimeout(1000);
linWakeup(1, 1, 300);
ReceiveLinFrame(Status);
ReceiveLinFrame(Status);
}
void ReceiveLinFrame(linFrame *rxFrame)
{
int ret = 0;
rxFrame.rtr = 1;
output(rxFrame);
rxFrame.rtr = 0;
ret = TestWaitForMessage(rxFrame.id, 500);
if(ret==0)
{
TestStepFail("Receiving Message", "No answer received for:0x%02X",rxFrame.id);
}
else
{
TestGetWaitEventMsgData(rxFrame);
TestStepPass("Receiving Message", "Answer received for:0x%02X",rxFrame.id);
}
}
I don't understand why the first frame is not visible in trace.
Here is the report
Also, why needs to be a wait time from sleep to wakeup in order to see the frames in trace window?
Related
I created a program on STM8S103F3 to generate a delay in rage of micro seconds using TIM2 module, but the timer is not ticking as expected and when I tried to call 5 sec delay using it, it is giving around 3 sec delay. I'm using 16MHz HSI oscillator and timer pre-scalar is set to 16. please see my code below. Please help me to figure out what is wrong with my code.
void clock_setup(void)
{
CLK_DeInit();
CLK_HSECmd(DISABLE);
CLK_LSICmd(DISABLE);
CLK_HSICmd(ENABLE);
while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);
CLK_ClockSwitchCmd(ENABLE);
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI,
DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, DISABLE);
}
void delay_us(uint16_t us)
{
volatile uint16_t temp;
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_PRESCALER_16, 2000); //Prescalar value 8,Timer clock 2MHz
TIM2_Cmd(ENABLE);
do{
temp = TIM2_GetCounter();
}while(temp < us);
TIM2_ClearFlag(TIM2_FLAG_UPDATE);
TIM2_Cmd(DISABLE);
}
void delay_ms(uint16_t ms)
{
while(ms--)
{
//delay_us(1000);
delay_us(1000);
}
}
It is better to use a 10us time base to round the delays. Well in order to achive a 10us timebase, if you use 16MHz master clock and you prescale TIM2 by 16, then you get a 1 us increment time, right? But we want TIM2 to overflow to generate an event named 16us event. Since we know that the timer will increment every 1us, if we use a reload value 65536 - 10 = 65526, this will give us a 10us overflow hence, 10us time base. If we are ok until here in delay code we'll just check the TIM2 update flag to know whther it has overflowed or not. See the example code snippet below.
// Set up it once since our time base is a fixed 10us
void setupTIM2(){
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_PRESCALER_16, 65526); //Prescalar value 16,Timer clock 1MHz
}
void delay_us(uint16_t us)
{
volatile uint16_t temp;
TIM2_Cmd(ENABLE);
const uint16_t count = us / 10; //Get the required counts for 10us time base
// Loop until the temp reaches the required count value
do{
while(TIM2_GetFlagStatus(TIM2_FLAG_UPDATE) == RESET); //Wait for the TIM2 to overflow
TIM2_ClearFlag(TIM2_FLAG_UPDATE); // Clear the overflow flag
temp++;
} while(temp < count);
TIM2_Cmd(DISABLE);
}
void delay_ms(uint16_t ms)
{
while(ms--)
{
//delay_us(1000);
delay_us(1000);
}
}
void main(void){
...
setupTIM2();
...
delay_ms(5000);
}
Ive incorporated multiple features i want in a microcontroller program (ESP32 Wroom32) and needed some advice on the best way to keep the program running and receive messages while it is running.
Current code:
//includes and declarations
setup()
{
//setup up wifi, server
}
main(){
WiFiClient client = server.available();
byte new_command[40];
if (client) // If client object is created, a connection is setup
{
Serial.println("New wifi Client.");
String currentLine = ""; //Used to print messages
while (client.connected())
{
recv_byte = client.read();
new_command = read_incoming(&recv_byte, client); //Returns received command and check for format. If invalid, returns a 0 array
if (new_command[0] != 0) //Checks if message is not zero, None of valid messages start with zero
{
execute_command(new_command);
//new_command is set to zero
}
}//end of while loop
}//end of if loop
}
The downside of this is that the ESP32 waits till the command is finished executing before it is ready to receive a new message. It is desired that the ESP32 receive commands and store them, and execute it at its own pace. I am planning to change the current code to receive a messages while the code is running as follows:
main()
{
WiFiClient client = server.available();
byte new_command[40];
int command_count = 0;
byte command_array[50][40];
if (command_count != 0)
{
execute_command(command_array[0]);
//Decrement command_count
//Shift all commands in command_array by 1 row above
//Set last executed command to zero
}
}//end of main loop
def message_interrupt(int recv_byte, WiFiClient& running_client)
{
If (running_client.connected())
{
recv_byte = running_client.read();
new_command = read_incoming(&recv_byte, running_client); //Returns received command and check for format. If invalid, returns a 0 array
//add new command to command_array after last command
//increment command_count
}
}
Which interrupt do I use to receive the message and update the command_array ? https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/wifi.html Doesnt mention any receive/transmit events. I couldnt find any receive/transmit interrupt either or maybe I searched for the wrong term.
I am trying to run a task periodically every 10 ms. Before executing the task, I want to check the consistency of clock_nanosleep. I took 10 values to check the time clock_nanosleep is sleeping, but they are varying in between 19-22 ms, which should be 10 ms.
I am running this thread with SCHED_FIFO, pri-98, with HRTimers enabled in Linux. Currently, I am using 3.14.29 Linux Kernel with RT Patch. Does clock_nanosleep require any extra configuration in Linux apart from HRTIMERS?
Below is the code snippet that I am running:
struct timespec arr[20];
while(1) {
clock_gettime(CLOCK_MONOTONIC,&check1);
if(i<20) {
arr[i].tv_sec = check1.tv_sec;
arr[i].tv_nsec = check1.tv_nsec;
++i;
}
check1.tv_nsec += p_CT->period;
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &check1, NULL);
clock_gettime(CLOCK_MONOTONIC,&check);
if(i<20) {
arr[i].tv_sec = check.tv_sec;
arr[i].tv_nsec = check.tv_nsec;
++i;
}
}
check return values of clock_gettime and clock_nanosleep. Maybe there was interruption by signal
run two sequenced clock_gettime and calculate average execution time of clock_gettime. I don't think that it will be around 9-12ms, but let's check
Code:
int r1 = clock_gettime(CLOCK_MONOTONIC,&check1);
int r2 = clock_gettime(CLOCK_MONOTONIC,&check);
if(i<20) {
arr[i].tv_sec = check1.tv_sec;
arr[i].tv_nsec = check1.tv_nsec;
++i;
}
if(i<20) {
arr[i].tv_sec = check.tv_sec;
arr[i].tv_nsec = check.tv_nsec;
++i;
}
printf("%d %d\n", r1, r2);
I don't see this program having any practical usage, but while experimenting with c++ 11 concurrency and conditional_variables I stumbled across something I don't fully understand.
At first I assumed that using notify_one() would allow the program below to work. However, in actuality the program just froze after printing one. When I switched over to using notify_all() the program did what I wanted it to do (print all natural numbers in order). I am sure this question has been asked in various forms already. But my specific question is where in the doc did I read wrong.
I assume notify_one() should work because of the following statement.
If any threads are waiting on *this, calling notify_one unblocks one of the waiting threads.
Looking below only one of the threads will be blocked at a given time, correct?
class natural_number_printer
{
public:
void run()
{
m_odd_thread = std::thread(
std::bind(&natural_number_printer::print_odd_natural_numbers, this));
m_even_thread = std::thread(
std::bind(&natural_number_printer::print_even_natural_numbers, this));
m_odd_thread.join();
m_even_thread.join();
}
private:
std::mutex m_mutex;
std::condition_variable m_condition;
std::thread m_even_thread;
std::thread m_odd_thread;
private:
void print_odd_natural_numbers()
{
for (unsigned int i = 1; i < 100; ++i) {
if (i % 2 == 1) {
std::cout << i << " ";
m_condition.notify_all();
} else {
std::unique_lock<std::mutex> lock(m_mutex);
m_condition.wait(lock);
}
}
}
void print_even_natural_numbers()
{
for (unsigned int i = 1; i < 100; ++i) {
if (i % 2 == 0) {
std::cout << i << " ";
m_condition.notify_all();
} else {
std::unique_lock<std::mutex> lock(m_mutex);
m_condition.wait(lock);
}
}
}
};
The provided code "works" correctly and gets stuck by design. The cause is described in the documentation
The effects of notify_one()/notify_all() and
wait()/wait_for()/wait_until() take place in a single total order, so
it's impossible for notify_one() to, for example, be delayed and
unblock a thread that started waiting just after the call to
notify_one() was made.
The step-by-step logic is
The print_odd_natural_numbers thread is started
The print_even_natural_numbers thread is started also.
The m_condition.notify_all(); line of print_even_natural_numbers is executed before than the print_odd_natural_numbers thread reaches the m_condition.wait(lock); line.
The m_condition.wait(lock); line of print_odd_natural_numbers is executed and the thread gets stuck.
The m_condition.wait(lock); line of print_even_natural_numbers is executed and the thread gets stuck also.
Hi I'm using processing and want to know if there is a way I can automatically save my output every X amount of seconds?
Any help would be great!
You are looking for saveFrame() method.
Inside your draw() method, you can save a screenshot of your visual output.
void draw() {
// YOUR CODE
...
// Saves each frame as screenshot-000001.png, screenshot-000002.png, etc.
saveFrame("screenshot-######.png");
}
More info: https://processing.org/reference/saveFrame_.html
And for take screenshot every X seconds:
int lastTime = 0;
void draw(){
// YOUR CODE
...
// 1000 in milisecs, that's 1 sec
if( millis() - lastTime >= 1000){
saveFrame("screenshot-######.png");
lastTime = millis();
}
}