STM32 Read event from remoteControlEvent_t and parse data. Values are wrong with passing - stm32

I have small problem with adding and reading values.
Define variables
#define ADC_BIT_MASK 0x0FFF
static TaskHandle_t remoteControlTaskHandle = NULL;
typedef enum
{
...
rcEvent_FreshADC = 0x80000000,
...
}
Code for notify task. adc12bitVal_pedal value is for example 100 and adc12bitVal_lr value for example 1000. I am shifting adc12bitVal_lr to the left that I can pass params ...
static void remoteControl_FreshADC(uint16_t adc12bitVal_pedal, uint16_t adc12bitVal_lr)
{
if(remoteControlTaskHandle != NULL)
{
xTaskNotify(remoteControlTaskHandle,
(rcEvent_FreshADC | (adc12bitVal_pedal & ADC_BIT_MASK) | (adc12bitVal_lr & ADC_BIT_MASK)<<12),
eSetValueWithOverwrite);
}
}
and then remoteControlHandleEvent which handle an event. Here I have problem with adcVal_lr which should be 1000 but is for example 52123. I need to shift 12 to the left that I get correct value. Or this is wrong?
returnCode_t remoteControlHandleEvent(remoteControlEvent_t event)
{
if(event & rcEvent_motorControlACK)
{
uint8_t ackNum = (uint8_t) ( ((event >> MOTOR_CONTROL_ACK_BIT_POS) & MOTOR_CONTROL_ACK_BIT_MASK));
printf("CONF: %u\n", (unsigned int)ackNum);
}
if(event & rcEvent_FreshADC)
{
...
// Value is 100
uint16_t adcVal_pedal = (uint16_t)(event & ADC_BIT_MASK);
// DOESN'T WORK VALUE IS 52123 instead of 1000
uint16_t adcVal_lr = (uint16_t)((event & ADC_BIT_MASK)<<12);
...
}
}
I don't understand and know why wrong value for
uint16_t adcVal_lr = (uint16_t)((event & ADC_BIT_MASK)<<12);
Thnak you for all comments and help.

You are shifting left (up) twice. To extract a value that you shifted left you need to shift right (down). You also apply the mask before shifting, when it should be after.
uint16_t adcVal_lr = (uint16_t)((event >> 12) & ADC_BIT_MASK);

Related

Xilinx Echo Server Data Variable

I want to have my Zedboard return a numeric value using the Xilinx lwIP example as a base but no matter what I do I can't figure out what stores the data received or transmitted.
I have found the void type payload but I don't know what to do with it.
Snapshot of one instance of payload and a list of lwIP files
Below is the closest function to my goal:
err_t recv_callback(void *arg, struct tcp_pcb *tpcb,
struct pbuf *p, err_t err){
/* do not read the packet if we are not in ESTABLISHED state */
if (!p) {
tcp_close(tpcb);
tcp_recv(tpcb, NULL);
return ERR_OK;
}
/* indicate that the packet has been received */
tcp_recved(tpcb, p->len);
/* echo back the payload */
/* in this case, we assume that the payload is < TCP_SND_BUF */
if (tcp_sndbuf(tpcb) > p->len) {
err = tcp_write(tpcb, p->payload, p->len, 1);
//I need to change p->paylod but IDK where it is given a value.
} else
xil_printf("no space in tcp_sndbuf\n\r");
/* free the received pbuf */
pbuf_free(p);
return ERR_OK;
}
Any guidance is appreciated.
Thanks,
Turtlemii
-I cheated and just made sure that the function has access to Global_tpcb from echo.c
-tcp_write() reads in an address and displays each char it seems.
void Print_Code()
{
/* Prepare for TRANSMISSION */
char header[] = "\rSwitch: 1 2 3 4 5 6 7 8\n\r"; //header text
char data_t[] = " \n\r\r"; //area for storing the
data
unsigned char mask = 10000000; //mask to decode switches
swc_value = XGpio_DiscreteRead(&SWCInst, 1); //Save switch values
/* Write switch values to the LEDs for visual. */
XGpio_DiscreteWrite(&LEDInst, LED_CHANNEL, swc_value);
for (int i =0; i<=7; i++) //load data_t with switch values (0/1)
{
data_t[8+2*i] = '0' + ((swc_value & mask)/mask); //convert one bit to 0/1
mask = mask >> 1;//move to next bit
}
int len_header = *(&header + 1) - header; //find the length of the
header string
int len_data = *(&data_t + 1) - data_t; //find the length of the data string
tcp_write(Global_tpcb, &header, len_header, 1); //print the header
tcp_write(Global_tpcb, &data_t, len_data, 1); //print the data
}

Not getting decreasing gas in a C++ program

Ok, so I have this program that is suppose to simulate an "Uber" driver picking up and dropping off customers and informing him of how far he has driven and how much gas he should have felt. I basically have it all worked out but for the life of me I can't get the gas to go down it stays at a static number. I know it is probably something so simple its dumb but I can't see it.
in my FuelGauge.h
#ifndef FuelGauge_h
#define FuelGauge_h
#define MAXG 15
// Class FuelGauge definition
class FuelGauge
{
// Data member
static int gallons;
public:
// Overloading -- operator
void operator --()
{
--gallons;
}// End of function
// Function to return gallons
int getGallons()
{
return gallons;
}// End of function
};// End of class
// Initializes static data member
int FuelGauge::gallons = MAXG;
#endif /* FuelGauge_hpp */
in my Car.h
#ifndef Car_h
#define Car_h
#include "FuelGauge.h"
#include "Odometer.h"
// Class car definition
class Car
{
public:
// Declares object as data member using deligation
FuelGauge *fg;
Odometer *om;
// Overloads >> operator
friend std::istream & operator >>(std::istream &is, Car &c)
{
int no;
// Loops till valid mileage entered by the user
do
{
// Accepts mileage
std::cin>>no;
// Checks if the mileage is zero or negative show error message
if(no <= 0)
std::cout<<"\n Invalid response, mileage should be greater than 0. \n Please reenter data.";
// Otherwise come out of the loop
else
break;
}while(1);
// Loops till no
for(int x = 0; x < no; x++)
// Increase the mileage by one
++*c.om;
// Checks if the current mileage is greater than or equals to 24 and entered mileage is less than 24
if(c.om->getCurrentMileage() >= 24 && no < 24)
// Decrement by one
--*c.fg;
// Otherwise
else
{
// Calculate the remainder
int rem = (no / 24);
// Loops till remainder
for(int x = 0; x < rem; x++)
// Decrease by one
--*c.fg;
}// End of else
// return istream object
return is;
}// End of function
// Overload << operator
friend std::ostream & operator <<(std::ostream &os, Car &c)
{
// Checks if the current gallon is less than or equals to zero
if(c.fg->getGallons() <= 0)
{
// Display message and stop
std::cout<<"\n Drove "<<c.om->getCurrentMileage()<<" miles I'm out of gas.";
exit(0);
}// End of if condition
// Otherwise display total mileage traveled and fuel left
else
std::cout<<"\n Drove "<<c.om->getCurrentMileage()<<" miles now I have "<<c.fg->getGallons()<<" gallons left.";
return os;
}// End of function
};// End of class
#endif /* Car_hpp */
and lastly Main.cpp
#include<iostream>
#include<stdlib.h>
#include "FuelGauge.h"
#include "Odometer.h"
#include "Car.h"
using namespace std;
// main function definition
int main()
{
int customer = 1;
// Creates car object
Car cc;
// Loops till = 0 fuel available
do
{
// Accepts data and displays data using object
cout<<"\n How far away is customer #"<<customer<<"? ";
cin>>cc;
cout<<cc;
cout<<"\n How far does customer #"<<customer<<" need to go?";
cin>>cc;
cout<<cc;
}while(static_cast<void>(customer++),1);// End of do - while
}// End of main function
I believe that problem is in my FuelGauge.h but I'm not seeing it. If someone would be so kind to look it over and let me know if they see anything I would greatly appreciate it.

Simblee/Rfduino Gzll communcation failing

I am attempting to send data from device to host using the Gazelle protocol, however, when reading a time varying signal in on MATLAB the values continuously change elements in the array.
Here is the Simblee/Rfduino host code:
#include <SimbleeGZLL.h>
device_t role = HOST;
char array[5];
void setup() {
Serial.begin(9600);
SimbleeGZLL.begin(role);
timer_one(1); // 1 ms timer
}
void loop() {
Serial.flush();
printf(EMG);
}
void SimbleeGZLL_onReceive(device_t device, int rssi, char *data, int len)
{
if (len > 0) {
digitalWrite(2,HIGH);
array[0] = data[0];
array[1] = data[1];
array[2] = data[2];
array[3] = data[3];
array[4] = '\0';
} else SimbleeGZLL.sendToDevice(device, 'A');
}
And the device code:
include
device_t role = DEVICE1;
volatile int state;
char array[4];
void setup() {
SimbleeGZLL.begin(role);
Serial.begin(9600);
timer_one(1);
}
void loop() {
array[0] = analogRead(2);
array[1] = analogRead(3);
array[2] = analogRead(4);
array[3] = analogRead(5);
SimbleeGZLL.sendToHost(EMG,4);
}
Could someone please provide some assistance to identify where the issue may lie?
Thank you!
Matlab is not super reliable with serial communication. I actually had a similar issue with a serial device where the input values would be out of order. Are you signaling when to start and stop printing? What does your matlab code look like?
I would set up a ring buffer on the host and the device to deal with the asycn time issues.
You are going to get timing issues with the current method. What kind of frequency are you going for? The analogRead is super slow, and double multiple in a row seems to make things even slower. Could you try to set up an ADC interrupt?
Where is your timer code?

Simple OS kernel -- screen output long string array issue

I am following the steps in: "Writing a Simple Operating System from Scratch". I got the basic kernel up and running, but got a strange error when I try to output a very long string.
The following code output all "0"s. But when I use a shorter "msg" string, then the screen prints "x".
I wonder if anybody can help me, thanks much! (I am testing it in Bochs in windows 8).
Here is the kernel.c
void start ()
{
// Create a pointer to a char , and point it to the first text cell of
// video memory (i.e. the top - left of the screen )
unsigned char *video_memory = ( unsigned char*) 0xb8000;
// At the address pointed to by video_memory , store the character 'X'
// (i.e. display 'X' in the top - left of the screen ).
// this string: the outputs are "0"s.
const char msg[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzAbcdefghijkAbcdefghijklmnopqrstuvwxyzA";
// this string: output are "x"s.
//const char msg[] = "abcdefghijklmnopqrstuvwxyzabcdefghijk";
int i = 0;
int offset = 0;
while(i<85)
{
// Test if msg points to the right memory
if(msg[i]==0)
{
video_memory[offset] = '0';
}
else
{
video_memory[offset] = 'x';
}
video_memory[offset+1] = 0x02; // Green color
i = i + 1;
offset = offset + 2;
//set_cursor(offset);
}
}

GPS output being incorrectly written to file on SD card- Arduino

I have a sketch to take information (Lat, Long) from an EM-406a GPS receiver and write the information to an SD card on an Arduino shield.
The program is as follows:
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <SD.h>
TinyGPSPlus gps;
SoftwareSerial ss(4, 3); //pins for the GPS
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
void setup()
{
Serial.begin(115200); //for the serial output
ss.begin(4800); //start ss at 4800 baud
Serial.println("gpsLogger by Aaron McRuer");
Serial.println("based on code by Mikal Hart");
Serial.println();
//initialize the SD card
if(!card.init(SPI_FULL_SPEED, 9))
{
Serial.println("card.init failed");
}
//initialize a FAT volume
if(!volume.init(&card)){
Serial.println("volume.init failed");
}
//open the root directory
if(!root.openRoot(&volume)){
Serial.println("openRoot failed");
}
//create new file
char name[] = "WRITE00.TXT";
for (uint8_t i = 0; i < 100; i++){
name[5] = i/10 + '0';
name[6] = i%10 + '0';
if(file.open(&root, name, O_CREAT | O_EXCL | O_WRITE)){
break;
}
}
if(!file.isOpen())
{
Serial.println("file.create");
}
file.print("Ready...\n");
}
void loop()
{
bool newData = false;
//For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (ss.available())
{
char c = ss.read();
//Serial.write(c); //uncomment this line if you want to see the GPS data flowing
if(gps.encode(c)) //did a new valid sentence come in?
newData = true;
}
}
if(newData)
{
file.write(gps.location.lat());
file.write("\n");
file.write(gps.location.lng());
file.write("\n");
}
file.close();
}
When I open up the file on the SD card when the program is finished executing, I get a message that it has an encoding error.
I'm currently inside (and unable to get a GPS signal, thus the 0), but the encoding problem needs to be tackled, and there should be as many lines as there are seconds that the device has been on. There's only that one. What do I need to do to make things work correctly here?
Closing the file in the loop, and never reopening it, is the reason there's only one set of data in your file.
Are you sure gps.location.lat() and gps.location.lng() return strings, not an integer or float? That would explain the binary data and the "encoding error" you see.