HOWTO create OpenGL ES 2.0 SkyBox? - dictionary

Can you give me hint to any good SkyBox example in OpenGL ES 2.0? I have found only OpenGL and does not work for me.
I am doing it this way:
Initialization:
glUseProgram(m_programSkyBox.Program);
glGenBuffers(1, &skyBoxVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, skyBoxVertexBuffer);
float vertices[24] = {
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glGenBuffers(1, &skyBoxIndexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, skyBoxIndexBuffer);
GLubyte indices[14] = {0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 2, 4, 0, 1};
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
Drawing the skybox:
glClearColor(0.5f, 0.5f, 0.5f, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(m_programSkyBox.Program);
glDisable(GL_DEPTH_TEST); // skybox should be drawn behind anything else
glBindTexture(GL_TEXTURE_CUBE_MAP, m_textures.Cubemap);
glBindBuffer(GL_ARRAY_BUFFER, skyBoxVertexBuffer);
glVertexAttribPointer(m_programSkyBox.Attributes.Position, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(m_programSkyBox.Attributes.Position);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, skyBoxIndexBuffer);
glDrawElements(GL_TRIANGLE_STRIP, 14, GL_UNSIGNED_BYTE, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glEnable(GL_DEPTH_TEST);
Texture loading is working. Shader is correctly compiled and looks like this:
Vertex shader:
attribute vec4 position;
varying mediump vec3 texCoord;
void main() {
texCoord.xyz = position.xyz;
}
Fragment shader:
uniform samplerCube Sampler;
varying mediump vec3 texCoord;
void main() {
mediump vec3 cube = vec3(textureCube(Sampler, texCoord.xyz));
gl_FragColor = vec4(cube, 1.0);
}
But I cant get cube visible. Am I doing anything wrong?
Thank you

My fault, I have found the problem. I have not "create" vertex in vertex shader. It should looks this way:
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
attribute vec4 position;
varying mediump vec4 texCoord;
void main() {
texCoord = position;
gl_Position = projectionMatrix * modelViewMatrix * position;
}

Related

How to send string commands via WiFiSever to an ESP32?

I am currently working on a project where the provided code for an ESP32 requires HTTP commands, however, what's provided doesn't include any sort of HTML code for webserver display. I am really confused if I am interpreting the code wrong or if I should be including the HTML code myself.
When I am connected to the access point established by the code it just shows me a blank web page.
I am very new to ESP32, so any guidance/advice would be greatly appreciated. Thank you!
#define ULTRATACT_VERSION "2.1"
const char* ssid = "UltraTactile Master";
const char* ssidMaster = "UltraTactile Master";
const char* password = "12345678";
#define BUTTON_PIN 12
#define MOD_PERIODS_DEFAULT 10
WiFiServer server(80);
#define N_TRANS 256
const float TRANS_POS[N_TRANS*3] = { -0.078749985, 0.0, 0.07874999, -0.05774999, 0.0, 0.07874999, -0.04724999, 0.0, 0.07874999, -0.068249986, 0.0, 0.06824999, -0.078749985, 0.0, 0.06824999, -0.04724999, 0.0, 0.06824999, -0.05774999, 0.0, 0.06824999, -0.068249986, 0.0, 0.07874999, -0.078749985, 0.0, 0.057749998, -0.05774999, 0.0, 0.057749998, -0.04724999, 0.0, 0.057749998, -0.068249986, 0.0, 0.04725, -0.078749985, 0.0, 0.04725, -0.04724999, 0.0, 0.04725, -0.05774999, 0.0, 0.04725, -0.068249986, 0.0, 0.057749998, -0.078749985, 0.0, 0.036750004, -0.05774999, 0.0, 0.036750004, -0.04724999, 0.0, 0.036750004, -0.068249986, 0.0, 0.026250003, -0.078749985, 0.0, 0.026250003, -0.04724999, 0.0, 0.026250003, -0.05774999, 0.0, 0.026250003, -0.068249986, 0.0, 0.036750004, -0.078749985, 0.0, 0.015750004, -0.05774999, 0.0, 0.015750004, -0.04724999, 0.0, 0.015750004, -0.068249986, 0.0, 0.0052500046, -0.078749985, 0.0, 0.0052500046, -0.04724999, 0.0, 0.0052500046, -0.05774999, 0.0, 0.0052500046, -0.068249986, 0.0, 0.015750004, -0.078749985, 0.0, -0.005249995, -0.05774999, 0.0, -0.005249995, -0.04724999, 0.0, -0.005249995, -0.068249986, 0.0, -0.015749995, -0.078749985, 0.0, -0.015749995, -0.04724999, 0.0, -0.015749995, -0.05774999, 0.0, -0.015749995, -0.068249986, 0.0, -0.005249995, -0.078749985, 0.0, -0.026249994, -0.05774999, 0.0, -0.026249994, -0.04724999, 0.0, -0.026249994, -0.068249986, 0.0, -0.036749996, -0.078749985, 0.0, -0.036749996, -0.04724999, 0.0, -0.036749996, -0.05774999, 0.0, -0.036749996, -0.068249986, 0.0, -0.026249994, -0.078749985, 0.0, -0.04724999, -0.05774999, 0.0, -0.04724999, -0.04724999, 0.0, -0.04724999, -0.068249986, 0.0, -0.05774999, -0.078749985, 0.0, -0.05774999, -0.04724999, 0.0, -0.05774999, -0.05774999, 0.0, -0.05774999, -0.068249986, 0.0, -0.04724999, -0.078749985, 0.0, -0.068249986, -0.05774999, 0.0, -0.068249986, -0.04724999, 0.0, -0.068249986, -0.068249986, 0.0, -0.078749985, -0.078749985, 0.0, -0.078749985, -0.04724999, 0.0, -0.078749985, -0.05774999, 0.0, -0.078749985, -0.068249986, 0.0, -0.068249986, -0.036749996, 0.0, 0.07874999, -0.015749995, 0.0, 0.07874999, -0.005249995, 0.0, 0.07874999, -0.026249994, 0.0, 0.06824999, -0.036749996, 0.0, 0.06824999, -0.005249995, 0.0, 0.06824999, -0.015749995, 0.0, 0.06824999, -0.026249994, 0.0, 0.07874999, -0.036749996, 0.0, 0.057749998, -0.015749995, 0.0, 0.057749998, -0.005249995, 0.0, 0.057749998, -0.026249994, 0.0, 0.04725, -0.036749996, 0.0, 0.04725, -0.005249995, 0.0, 0.04725, -0.015749995, 0.0, 0.04725, -0.026249994, 0.0, 0.057749998, -0.036749996, 0.0, 0.036750004, -0.015749995, 0.0, 0.036750004, -0.005249995, 0.0, 0.036750004, -0.026249994, 0.0, 0.026250003, -0.036749996, 0.0, 0.026250003, -0.005249995, 0.0, 0.026250003, -0.015749995, 0.0, 0.026250003, -0.026249994, 0.0, 0.036750004, -0.036749996, 0.0, 0.015750004, -0.015749995, 0.0, 0.015750004, -0.005249995, 0.0, 0.015750004, -0.026249994, 0.0, 0.0052500046, -0.036749996, 0.0, 0.0052500046, -0.005249995, 0.0, 0.0052500046, -0.015749995, 0.0, 0.0052500046, -0.026249994, 0.0, 0.015750004, -0.036749996, 0.0, -0.005249995, -0.015749995, 0.0, -0.005249995, -0.005249995, 0.0, -0.005249995, -0.026249994, 0.0, -0.015749995, -0.036749996, 0.0, -0.015749995, -0.005249995, 0.0, -0.015749995, -0.015749995, 0.0, -0.015749995, -0.026249994, 0.0, -0.005249995, -0.036749996, 0.0, -0.026249994, -0.015749995, 0.0, -0.026249994, -0.005249995, 0.0, -0.026249994, -0.026249994, 0.0, -0.036749996, -0.036749996, 0.0, -0.036749996, -0.005249995, 0.0, -0.036749996, -0.015749995, 0.0, -0.036749996, -0.026249994, 0.0, -0.026249994, -0.036749996, 0.0, -0.04724999, -0.015749995, 0.0, -0.04724999, -0.005249995, 0.0, -0.04724999, -0.026249994, 0.0, -0.05774999, -0.036749996, 0.0, -0.05774999, -0.005249995, 0.0, -0.05774999, -0.015749995, 0.0, -0.05774999, -0.026249994, 0.0, -0.04724999, -0.036749996, 0.0, -0.068249986, -0.015749995, 0.0, -0.068249986, -0.005249995, 0.0, -0.068249986, -0.026249994, 0.0, -0.078749985, -0.036749996, 0.0, -0.078749985, -0.005249995, 0.0, -0.078749985, -0.015749995, 0.0, -0.078749985, -0.026249994, 0.0, -0.068249986, 0.0052500046, 0.0, 0.07874999, 0.026250003, 0.0, 0.07874999, 0.036750004, 0.0, 0.07874999, 0.015750004, 0.0, 0.06824999, 0.0052500046, 0.0, 0.06824999, 0.036750004, 0.0, 0.06824999, 0.026250003, 0.0, 0.06824999, 0.015750004, 0.0, 0.07874999, 0.0052500046, 0.0, 0.057749998, 0.026250003, 0.0, 0.057749998, 0.036750004, 0.0, 0.057749998, 0.015750004, 0.0, 0.04725, 0.0052500046, 0.0, 0.04725, 0.036750004, 0.0, 0.04725, 0.026250003, 0.0, 0.04725, 0.015750004, 0.0, 0.057749998, 0.0052500046, 0.0, 0.036750004, 0.026250003, 0.0, 0.036750004, 0.036750004, 0.0, 0.036750004, 0.015750004, 0.0, 0.026250003, 0.0052500046, 0.0, 0.026250003, 0.036750004, 0.0, 0.026250003, 0.026250003, 0.0, 0.026250003, 0.015750004, 0.0, 0.036750004, 0.0052500046, 0.0, 0.015750004, 0.026250003, 0.0, 0.015750004, 0.036750004, 0.0, 0.015750004, 0.015750004, 0.0, 0.0052500046, 0.0052500046, 0.0, 0.0052500046, 0.036750004, 0.0, 0.0052500046, 0.026250003, 0.0, 0.0052500046, 0.015750004, 0.0, 0.015750004, 0.0052500046, 0.0, -0.005249995, 0.026250003, 0.0, -0.005249995, 0.036750004, 0.0, -0.005249995, 0.015750004, 0.0, -0.015749995, 0.0052500046, 0.0, -0.015749995, 0.036750004, 0.0, -0.015749995, 0.026250003, 0.0, -0.015749995, 0.015750004, 0.0, -0.005249995, 0.0052500046, 0.0, -0.026249994, 0.026250003, 0.0, -0.026249994, 0.036750004, 0.0, -0.026249994, 0.015750004, 0.0, -0.036749996, 0.0052500046, 0.0, -0.036749996, 0.036750004, 0.0, -0.036749996, 0.026250003, 0.0, -0.036749996, 0.015750004, 0.0, -0.026249994, 0.0052500046, 0.0, -0.04724999, 0.026250003, 0.0, -0.04724999, 0.036750004, 0.0, -0.04724999, 0.015750004, 0.0, -0.05774999, 0.0052500046, 0.0, -0.05774999, 0.036750004, 0.0, -0.05774999, 0.026250003, 0.0, -0.05774999, 0.015750004, 0.0, -0.04724999, 0.0052500046, 0.0, -0.068249986, 0.026250003, 0.0, -0.068249986, 0.036750004, 0.0, -0.068249986, 0.015750004, 0.0, -0.078749985, 0.0052500046, 0.0, -0.078749985, 0.036750004, 0.0, -0.078749985, 0.026250003, 0.0, -0.078749985, 0.015750004, 0.0, -0.068249986, 0.04725, 0.0, 0.07874999, 0.06824999, 0.0, 0.07874999, 0.07874999, 0.0, 0.07874999, 0.057749998, 0.0, 0.06824999, 0.04725, 0.0, 0.06824999, 0.07874999, 0.0, 0.06824999, 0.06824999, 0.0, 0.06824999, 0.057749998, 0.0, 0.07874999, 0.04725, 0.0, 0.057749998, 0.06824999, 0.0, 0.057749998, 0.07874999, 0.0, 0.057749998, 0.057749998, 0.0, 0.04725, 0.04725, 0.0, 0.04725, 0.07874999, 0.0, 0.04725, 0.06824999, 0.0, 0.04725, 0.057749998, 0.0, 0.057749998, 0.04725, 0.0, 0.036750004, 0.06824999, 0.0, 0.036750004, 0.07874999, 0.0, 0.036750004, 0.057749998, 0.0, 0.026250003, 0.04725, 0.0, 0.026250003, 0.07874999, 0.0, 0.026250003, 0.06824999, 0.0, 0.026250003, 0.057749998, 0.0, 0.036750004, 0.04725, 0.0, 0.015750004, 0.06824999, 0.0, 0.015750004, 0.07874999, 0.0, 0.015750004, 0.057749998, 0.0, 0.0052500046, 0.04725, 0.0, 0.0052500046, 0.07874999, 0.0, 0.0052500046, 0.06824999, 0.0, 0.0052500046, 0.057749998, 0.0, 0.015750004, 0.04725, 0.0, -0.005249995, 0.06824999, 0.0, -0.005249995, 0.07874999, 0.0, -0.005249995, 0.057749998, 0.0, -0.015749995, 0.04725, 0.0, -0.015749995, 0.07874999, 0.0, -0.015749995, 0.06824999, 0.0, -0.015749995, 0.057749998, 0.0, -0.005249995, 0.04725, 0.0, -0.026249994, 0.06824999, 0.0, -0.026249994, 0.07874999, 0.0, -0.026249994, 0.057749998, 0.0, -0.036749996, 0.04725, 0.0, -0.036749996, 0.07874999, 0.0, -0.036749996, 0.06824999, 0.0, -0.036749996, 0.057749998, 0.0, -0.026249994, 0.04725, 0.0, -0.04724999, 0.06824999, 0.0, -0.04724999, 0.07874999, 0.0, -0.04724999, 0.057749998, 0.0, -0.05774999, 0.04725, 0.0, -0.05774999, 0.07874999, 0.0, -0.05774999, 0.06824999, 0.0, -0.05774999, 0.057749998, 0.0, -0.04724999, 0.04725, 0.0, -0.068249986, 0.06824999, 0.0, -0.068249986, 0.07874999, 0.0, -0.068249986, 0.057749998, 0.0, -0.078749985, 0.04725, 0.0, -0.078749985, 0.07874999, 0.0, -0.078749985, 0.06824999, 0.0, -0.078749985, 0.057749998, 0.0, -0.068249986};
const byte PHASES_FOR_LINE[N_TRANS] = {14,6,20,15,3,10,27,24,20,14,29,22,4,14,31,5,16,14,28,14,25,6,26,4,0,2,14,25,3,18,7,21,3,7,18,21,0,14,2,25,25,26,6,4,16,28,14,14,4,31,14,5,20,29,14,22,3,27,10,24,14,20,6,15,31,0,2,28,21,25,22,3,9,11,14,3,26,0,29,17,9,12,15,29,20,26,23,18,27,31,2,9,31,6,3,5,31,3,6,5,27,2,31,9,20,23,26,18,9,15,12,29,26,29,0,17,9,14,11,3,21,22,25,3,31,2,0,28,3,9,0,5,25,22,31,14,14,19,10,11,0,27,5,25,15,19,10,5,26,21,31,26,2,6,29,17,6,0,10,13,6,10,0,13,2,29,6,17,26,31,21,26,15,10,19,5,0,5,27,25,14,10,19,11,25,31,22,14,3,0,9,5,21,3,17,8,11,5,24,17,0,10,22,11,19,5,26,27,4,6,17,3,16,27,16,25,25,23,2,14,29,5,26,10,29,26,5,10,25,2,23,14,16,16,27,25,4,17,6,3,19,26,5,27,0,22,10,11,11,24,5,17,21,17,3,8};
byte bufferToSend[N_TRANS + 2];
unsigned long millisFromLastButtonPress = 0;
byte buttonPresses = 0;
/* Array control functions */
void swichOffArray() {
bufferToSend[0] = 254; //start phases command
bufferToSend[N_TRANS + 1] = 253; //swap double buffer command (like a commit)
for (int i = 1; i < N_TRANS + 1; i += 1)
bufferToSend[i] = 32; //32 is the value for switching of the transducer
Serial2.write( bufferToSend, N_TRANS + 2 );
Serial2.flush();
}
void changeModulation(byte periods){
if (periods < 1) periods = 1;
else if(periods > 31) periods = 31;
Serial2.write( 0b10100000 | periods);
}
const float WAVELENGTH = 0.00865f;
const float PHASE_DIV = 32.0f;
void focusArrayAt(const float posX, const float posY, const float posZ) { //positions are in meters in respect to the center of the array
bufferToSend[0] = 254; //start phases command
bufferToSend[N_TRANS + 1] = 253; //swap double buffer command (i.e. commit)
for (int i = 1; i < N_TRANS+1; i += 1) {
const float diffX = TRANS_POS[(i-1)*3 + 0] - posX;
const float diffY = TRANS_POS[(i-1)*3 + 1] - posY;
const float diffZ = TRANS_POS[(i-1)*3 + 2] - posZ;
const float distInWavelengths = sqrt(diffX * diffX + diffY * diffY + diffZ * diffZ) / WAVELENGTH;
const byte phase = (byte)((ceil(distInWavelengths) - distInWavelengths) * PHASE_DIV);
bufferToSend[i] = phase;
}
Serial2.write( bufferToSend, N_TRANS + 2 );
Serial2.flush();
}
void makeBeeps(int nBeeps, int modulation=7){
if (nBeeps < 1) nBeeps = 1;
else if(nBeeps > 10) nBeeps = 10;
swichOffArray();
changeModulation( modulation );
for (int i = 0; i < nBeeps; i+=1 ){
focusArrayAt(0,0.1,0);
delay(250);
swichOffArray();
delay(250);
}
changeModulation( MOD_PERIODS_DEFAULT );
}
void sendPhasePattern(const byte* pattern){
Serial2.write( 254 );
Serial2.write( pattern, N_TRANS);
Serial2.write( 253 );
Serial2.flush();
}
/* Network functions*/
void configWifiAsAccessPoint(const char* ssidName){
Serial.println("Configuring as access point...");
WiFi.mode(WIFI_AP);
WiFi.softAP( ssidName, password );
Serial.println("Wait 100 ms for AP_START...");
delay(100);
Serial.println("Set softAPConfig");
IPAddress Ip(192, 168, 1, 1);
IPAddress NMask(255, 255, 255, 0);
WiFi.softAPConfig(Ip, Ip, NMask);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
}
boolean configWifiAsSlave(const byte lastIPDigit){
Serial.println("Configuring as station...");
WiFi.mode(WIFI_STA);
IPAddress local_IP(192, 168, 1, lastIPDigit);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}
Serial.println("Conecting to master");
WiFi.begin(ssidMaster, password);
for (int tries = 0; WiFi.status() != WL_CONNECTED; tries+= 1) {
delay(250);
Serial.print(".");
if (tries >= 4*5){ //after 5 seconds trying to connect
Serial.println("failure connecting to the master SSID");
return false;
}
}
IPAddress myIP = WiFi.localIP();
Serial.print("Station IP address: ");
Serial.println(myIP);
return true;
}
void setup() {
Serial.begin(230400); // this serial is for receiving commands and sending debug info to the computer
Serial2.begin(230400, SERIAL_8N1, 17, 16); //RX2 TX2 This serial communicates with the array
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.println(ULTRATACT_VERSION);
Serial.println( ssid );
configWifiAsAccessPoint( ssid ); //start as access point with regular ssid
server.begin();
delay(200);
Serial.println("Switching off the array...");
makeBeeps( 1 );
}
int previousButton = HIGH;
int itersButtonHigh = 0;
bool randomMode = false;
unsigned long randomModeTriggerMillis = 0;
void loop() {
const unsigned long currentMillis = millis();
/* Button pres input */
if ( currentMillis - millisFromLastButtonPress > 1000 && buttonPresses > 0){
randomMode = false;
swichOffArray();
if (buttonPresses == 1){ //switch off the emitter
Serial.println("Button pressed once, switching off the array");
}else if (buttonPresses >= 2){ //try to connect to the master ssid,
const int lastIPDigit = 98 + buttonPresses; //with increasing IP numbers (.101 .102 ...)
Serial.print("Button pressed >=2, connecting with ip ");
Serial.println(lastIPDigit);
const boolean conectedToMaster = configWifiAsSlave(lastIPDigit);
if (conectedToMaster == false ){ //we failed connecting to the master, so we become the master
configWifiAsAccessPoint(ssidMaster);
makeBeeps( 1, 5 );
}else{
makeBeeps( buttonPresses );
}
}
delay(500);
buttonPresses = 0;
}
const int currentButtonState = digitalRead(BUTTON_PIN);
if (previousButton == HIGH && currentButtonState == LOW && itersButtonHigh > 100){ //when the button is pressed. last part is for avoid bouncing
buttonPresses += 1;
millisFromLastButtonPress = currentMillis;
}
if ( currentButtonState == HIGH && itersButtonHigh < 0x0FFF){
itersButtonHigh += 1;
}else{
itersButtonHigh = 0;
}
previousButton = currentButtonState;
/* Serial control, mainly to debug*/
if (Serial.available() > 1) {
byte letter = Serial.read();
Serial.read(); //read the newline
if (letter == 's') {
swichOffArray();
} else if (letter == 'f') {
focusArrayAt( 0, 0.1f, 0);
} else {
Serial.println("Yeah, I am still alive");
}
}
/* random mode*/
if (randomMode && randomModeTriggerMillis <= currentMillis){
randomModeTriggerMillis = currentMillis + random(20,150);
focusArrayAt( random(-40,40) / 1000.0f, random(90,110) / 1000.0f, random(-40,40) / 1000.0f);
}
/* Commands comming from HTTP */
WiFiClient client = server.available();
if (client) {
String request = client.readStringUntil('\r');
Serial.print( currentMillis );
Serial.print(' ');
Serial.println( request );
client.println("HTTP/1.1 200 OK\nContent-type:text/html\nConnection: close\n\n");
// /focus/x_y_z
// /off
// /mode/number number 1 to 4, mainly affects the modulation frequency
// /line at 10cm, 0 degrees
// /noise at 10cm, sparks (random mode)
int sep1 = request.indexOf('/');
if (sep1 == -1)
return;
//disable randomMode (noise pop...)
randomMode = false;
//command
if ((sep1 = request.indexOf("focus", sep1+1)) != -1){
const int sep2 = request.indexOf('/', sep1 + 1);
const int sep3 = request.indexOf('_', sep2 + 1);
const int sep4 = request.indexOf('_', sep3 + 1);
const int sep5 = request.indexOf(' ', sep4 + 1);
if (sep2 == -1 || sep3 == -1 || sep4 == -1 || sep5 == -1)
return;
const float posX = request.substring(sep2 + 1, sep3).toFloat();
const float posY = request.substring(sep3 + 1, sep4).toFloat();
const float posZ = request.substring(sep4 + 1, sep5).toFloat();
Serial.printf("Focusing at %f %f %f\n", posX, posY, posZ);
focusArrayAt( -posX, posY, posZ);
Serial.println("Focused");
client.printf("Focused at %f %f %f\n", posX, posY, posZ);
}else if((sep1 = request.indexOf("off", sep1+1)) != -1){
Serial.println("Switching off the array...");
swichOffArray();
}else if((sep1 = request.indexOf("mode", sep1+1)) != -1){
const int sep2 = request.indexOf('/', sep1 + 1);
const int sep3 = request.indexOf(' ', sep2 + 1);
const int mod = request.substring(sep2 + 1, sep3).toInt();
if (mod == 1){
changeModulation( MOD_PERIODS_DEFAULT );
}else if (mod == 2){
changeModulation( 7 );
}else if (mod == 3){
changeModulation( 15 );
}else if (mod == 4){
changeModulation( 8 );
}else{
changeModulation( MOD_PERIODS_DEFAULT );
}
}else if((sep1 = request.indexOf("line", sep1+1)) != -1){
Serial.println("Emitting a line...");
sendPhasePattern( PHASES_FOR_LINE );
}else if((sep1 = request.indexOf("sparks", sep1+1)) != -1){
Serial.println("Emitting noise...");
randomMode = true;
randomModeTriggerMillis = currentMillis;
}else if((sep1 = request.indexOf("version", sep1+1)) != -1){
Serial.println("Version is");
client.printf("Version is %s\n", ULTRATACT_VERSION);
}
client.stop();
}
}

Problems with initialize char array in a struct. Arduion Project in VS2019

My last c++ Project was about 3 years ago and know I have some problems with basics.
I have a class stuff. In my .h file i have a struct:
typedef struct
{
float fX;
float fY;
float fZ;
float fRx;
float fRy;
float fRz;
char aName[14] = "";
} stpoint;
and a private variable stpoint _stpoints[].
In my.cpp I have a function:
void stuff::getstuff()
{
stpoint _stpoints[] = {
{ 164.5, 0.0, 241.0, 90.0, 180.0, -90.0,"Home"},
{ 164.5, 0.0, 141.0, 90.0, 180.0, -90.0,"X1"},
{ 164.5 + 14.7, 35.4, 141.0, 90.0, 180.0, -90.0 ,"X11"},
{ 164.5 + 50.0, 50.0, 141.0, 90.0, 180.0, -90.0,"X12"},
{ 164.5 + 85.3, 35.4, 141.0, 90.0, 180.0, -90.0,"X13"},
{ 164.5 + 100.0, 0.0, 141.0, 90.0, 180.0, -90.0,"X14"},
{ 164.5 + 85.3, -35.4, 141.0, 90.0, 180.0, -90.0,"X15"},
{ 164.5 + 50.0, -50.0, 141.0, 90.0, 180.0, -90.0,"X16"},
{ 164.5 + 14.7, -35.4, 141.0, 90.0, 180.0, -90.0,"X17"},
{ 164.5 + 50.0, 0.0, 141.0, 90.0, 180.0, -90.0,"X18"},
{ 264.5, 0.0, 141.0, 0.0, 90.0, 0.0 ,"X2"},
{ 164.5, 100.0, 141.0, 90.0, 90.0, 0.0,"X3"},
{ 164.5, -100.0, 141.0, 90.0, -90.0, 0.0,"X4"}
};
}
Its a project for Arduino. I use VS2019.
I get error code (just one of them):
stuff.cpp: 37:2: error: could not convert '{1.645e+2, 0.0, 2.41e+2, 9.0e+1, 1.8e+2, -9.0e+1, "Home"}' from '' to 'stuff::stpoint
Error compiling project sources
Debug build failed for project 'Roboter'
It something about the char array in the struct but i dont get it :(
struct stpoint _stpoints2 = { 164.5, 0.0, 241.0, 90.0, 180.0, -90.0,"Home"} gives the same error.
#πάντα ῥεῖ
typedef struct
{
float fX;
float fY;
float fZ;
float fRx;
float fRy;
float fRz;
char aName[14];
} stpoint;
that solved the problem. thanks
Initialize with aName = { "" }.

MethodError: no method matching -(::VariableRef, ::Nothing)

I am new to julia and I am still trying to learn about it.
I tried to use a user-defined function in my constraint.
Basically, the lookup function returns the value of the same index as the input in the array:
function lookup(scenario,zw_set,sn_set)
for i= 1:length(sn_set)
if sn_set[i] == scenario
return zw_set[i]
end
end
end
Thus I tried to apply this function in my optimization model:
m =Model(with_optimizer(CPLEX.Optimizer))
T = map(Int64, collect(1:1:5))
R = [1; 2]
#variable(m, sn[R,T] >=0,Int)
#variable(m,zw1[T]>=0, Int)
#constraint(m, [t in T], zw1[t]==lookup(sn[1,t],w1,snx1))
and I got this error:
ERROR: LoadError: MethodError: no method matching -(::VariableRef, ::Nothing)
Closest candidates are:
-(::Union{MathOptInterface.ScalarAffineFunction{T}, MathOptInterface.ScalarQuadraticFunction{T}}, ::T) where T at C:\Users\alkin\.julia\packages\MathOptInterface\DmQBj\src\Utilities\functions.jl:1125
-(::AbstractVariableRef) at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\operators.jl:58
-(::AbstractVariableRef, ::Union{Number, LinearAlgebra.UniformScaling}) at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\operators.jl:62
...
Stacktrace:
[1] sub_mul(::VariableRef, ::Nothing) at C:\Users\alkin\.julia\packages\MutableArithmetics\h76UA\src\MutableArithmetics.jl:31
[2] operate(::typeof(MutableArithmetics.sub_mul), ::VariableRef, ::Nothing) at C:\Users\alkin\.julia\packages\MutableArithmetics\h76UA\src\interface.jl:88
[3] operate_fallback!(::MutableArithmetics.NotMutable, ::Function, ::VariableRef, ::Nothing) at C:\Users\alkin\.julia\packages\MutableArithmetics\h76UA\src\interface.jl:275
[4] operate!(::typeof(MutableArithmetics.sub_mul), ::VariableRef, ::Nothing) at C:\Users\alkin\.julia\packages\MutableArithmetics\h76UA\src\rewrite.jl:67
[5] macro expansion at C:\Users\alkin\.julia\packages\MutableArithmetics\h76UA\src\rewrite.jl:224 [inlined]
[6] macro expansion at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\macros.jl:380 [inlined]
[7] (::var"#5653#5654")(::Int64) at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\Containers\macro.jl:183
[8] #28 at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\Containers\container.jl:85 [inlined]
[9] iterate at .\generator.jl:47 [inlined]
[10] collect(::Base.Generator{JuMP.Containers.VectorizedProductIterator{Tuple{Array{Int64,1}}},JuMP.Containers.var"#28#29"{var"#5653#5654"}}) at .\array.jl:622
[11] map(::Function, ::JuMP.Containers.VectorizedProductIterator{Tuple{Array{Int64,1}}}) at .\abstractarray.jl:2099
[12] container(::Function, ::JuMP.Containers.VectorizedProductIterator{Tuple{Array{Int64,1}}}, ::Type{JuMP.Containers.DenseAxisArray}) at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\Containers\container.jl:85
[13] container(::Function, ::JuMP.Containers.VectorizedProductIterator{Tuple{Array{Int64,1}}}) at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\Containers\container.jl:65
[14] top-level scope at C:\Users\alkin\.julia\packages\JuMP\Sp4sR\src\macros.jl:45
w1 and snx1 has the following data:
julia> print(w1)
[0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,1.0, 1.0, 0.0, 1.0]
julia> print(snx1)
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0]
I realized that it might have to do something with the data types and array stuff?
Please help
Can you elaborate on what you are trying to do in the lookup function?
Your scenario in the lookup function is a JuMP variable, i.e. a VariableRef. And you are trying to do a comparison with a Julia Int.
if scenario == sn_set[i] # this is always `false`
return zw_set[i]
end
This will always not enter the if block. And your lookup function will always return nothing.
When JuMP tries to create the constraint, it does
#constraint(m, [t in T], zw1[t] == nothing)
which causes your error.
See example below that throws an error in the lookup:
using JuMP
function lookup(scenario, zw_set, sn_set)
for i in 1:length(sn_set)
if scenario == sn_set[i]
return zw_set[i]
end
end
error("Cannot find $scenario in $sn_set.")
end
w1 = [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,1.0, 1.0, 0.0, 1.0]
snx1 = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0]
m = Model()
T = map(Int64, collect(1:1:5))
R = [1; 2]
#variable(m, sn[R,T] >=0, Int)
#variable(m, zw1[T] >= 0, Int)
#constraint(m, [t in T], zw1[t] == lookup(sn[1,t], w1, snx1))

Random walks on directed graph in R

I am new in R programming. I have a directed graph which has 6 nodes and also provided a probability matrix of 6 rows and 6 columns. If a random walker walk 100,000 steps on the graph should end up the output vector like the following:
0.1854753, 0.1301621,0.0556688, 0.1134808, 0.15344649, 0.3617481
corresponding to the probabilities of 6 nodes being visited in this random walk experiment(counts divided by the total number of steps, in this case, 100,000).
I need to create a function for this task and to demonstrate how to use it. The function takes a graph and number of steps as input.
The provided matrix as follows:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.0 0.5 0.3 0.0 0.0 0.2
[2,] 0.1 0.2 0.0 0.4 0.1 0.2
[3,] 0.5 0.0 0.0 0.0 0.0 0.5
[4,] 0.0 0.1 0.0 0.0 0.6 0.3
[5,] 0.0 0.0 0.0 0.4 0.0 0.6
[6,] 0.4 0.0 0.0 0.0 0.2 0.4
Can someone help me to solve the problem?
Here is a step-by-step implementation using a Markov chain (through R library markovchain).
We start by loading the library.
library(markovchain);
We define the transition matrix and states (here simply 1...6 for the graph nodes)
mat <- matrix(c(
0.0, 0.5, 0.3, 0.0, 0.0, 0.2,
0.1, 0.2, 0.0, 0.4, 0.1, 0.2,
0.5, 0.0, 0.0, 0.0, 0.0, 0.5,
0.0, 0.1, 0.0, 0.0, 0.6, 0.3,
0.0, 0.0, 0.0, 0.4, 0.0, 0.6,
0.4, 0.0, 0.0, 0.0, 0.2, 0.4), ncol = 6, byrow = T)
states <- as.character(1:6);
We define a Markov chain object.
mc <- new(
"markovchain",
states = states,
byrow = TRUE,
transitionMatrix = mat,
name = "random_walk");
We now simulate a random walk consisting of nSteps (here 1e6) and obtain asymptotic probabilities for every state (node) with prop.table(table(...))
nSteps <- 1e6;
random_walk <- markovchainSequence(nSteps, mc, t0 = "1");
prop.table(table(random_walk));
#random_walk
# 1 2 3 4 5 6
#0.185452 0.129310 0.055692 0.113410 0.153787 0.362349
Note that asymptotic probabilities might change slightly if you re-run the code.
Wrapping this in a single function is straight-forward and I'll leave that up to you.
Assuming you are giving probability matrix (prob_mat) for the directed graph and no of steps (no_of_steps) as input. This should do:
set.seed(150)
find_pos_prob <- function(prob_mat, no_of_steps){
x <- c(1:nrow(prob_mat)) # index for nodes
position <- 1 # initiating from 1st Node
occured <- rep(0,nrow(prob_mat)) # initiating occured count
for (i in 1:no_of_steps) {
# update position at each step and increment occurence
position <- sample(x, 1, prob = prob_mat[position,])
occured[position] <- occured[position] + 1
}
return (occured/no_of_steps)
}
find_pos_prob(prob_mat, 100000)
#[1] 0.18506 0.13034 0.05570 0.11488 0.15510 0.35892
Data:
prob_mat <- matrix( c(0.0, 0.5, 0.3, 0.0, 0.0, 0.2,
0.1, 0.2, 0.0, 0.4, 0.1, 0.2,
0.5, 0.0, 0.0, 0.0, 0.0, 0.5,
0.0, 0.1, 0.0, 0.0, 0.6, 0.3,
0.0, 0.0, 0.0, 0.4, 0.0, 0.6,
0.4, 0.0, 0.0, 0.0, 0.2, 0.4), byrow = TRUE, ncol = 6)
Note: Simulation results will differ from analytical solutions. Ideally you should remove the seed, run the function 15-20 times and take the average of probabilities over the runs

Write a program in mathematica whith Givens rotation

How can I write a program which can solve a system of linear equations with Givens rotation in mathematica .
Code attempt:
ar = CoefficientArrays[
{x + y + z == 2, 3 x - 2 y + z == 4, x - y + 5 z == 6},
{x, y, z}];
an = Normal[ar];
b = an[[2]];
n = Length[b];
Do[Do[a[i, j] = b[[i]][[j]], {j, 1, n}], {i, 1, n}];
r1 = (a[1, 1]^2 + a[1, 2]^2)^(1/2);
c = a[1, 1]/r1;
s = a[1, 2]/r1;
Are you looking for something like this?
Following your code.
{r1, c, s}
{Sqrt[2], 1/Sqrt[2], 1/Sqrt[2]}
{a, b} = LinearSolve[{{c, -s}, {s, c}}, {r1, 0}]
{1, -1}
algorithm from wikipedia
GivensRotation[a_, b_] := Which[
b == 0, c = Sign[a]; s = 0; r = Abs[a],
a == 0, c = 0; s = -Sign[b]; r = Abs[b],
Abs[a] > Abs[b],
t = b/a; u = Sign[a]*Abs[Sqrt[1 + t*t]];
c = 1/u; s = -c*t; r = a*u,
True,
t = a/b;
u = Sign[b]*Abs[Sqrt[1 + t*t]];
s = -1/u; c = -s*t; r = b*u
]
GivensRotation[a, b];
{r, c, s}
{Sqrt[2], 1/Sqrt[2], 1/Sqrt[2]}
Edit
I'm not familiar with solving with Givens rotation. Here are other methods for solving the simultaneous equations, just for interest.
Solve[{
x + y + z == 2,
3 x - 2 y + z == 4,
x - y + 5 z == 6},
{x, y, z}]
{{x -> 1, y -> 0, z -> 1}}
also
LinearSolve[{{1, 1, 1}, {3, -2, 1}, {1, -1, 5}}, {2, 4, 6}]
{1, 0, 1}
or
Inverse[{{1, 1, 1}, {3, -2, 1}, {1, -1, 5}}].{2, 4, 6}
{1, 0, 1}

Resources